开发者

Strange case while using the sizeof() operator in C

I was testing the use of sizeof operator in C/C++ with this code:

#include <ctype.h>              /* Character types                       */
#include <stdio.h>              /* Standard buffered input/output        */
#include <stdlib.h>             /* Standard library functions            */
#include <string.h>             /* String operations                     */
#include <sys/types.h>          /* Data types                            */
#include <sys/wait.h>           /* Declarations for waiting              */
#include <unistd.h>   

void main()
{
  char a[100];
  char *des = malloc(100*sizeof(char));
  strcpy(des,"abcded\0");
  printf("%d\n",sizeof(des));
  printf("%d\n",sizeof(a));
  free(des);
}

Why does this program output:

4
1开发者_如何学编程00

As opposed to:

100
100


Because, despite the assertions of many people, pointers and arrays are NOT 100% interchangeable in C & C++. This is a clear example of one of the differences.

The size of a pointer is 4-bytes (or whatever platform specific size), regardless of how much allocated memory it may point to.

The size of an array is the size of the array.


sizeof(des) returns the size of the pointer - which is 4 on your system. It doesn't return the size of the allocated space it points to.


printf("%d\n",sizeof(des));
printf("%d\n",sizeof(a));

is equivalent to the following:

printf("%d\n",sizeof(char*));      //prints size of a pointer
printf("%d\n",sizeof(char[100]));  //size of a char array of size 100

In other words, sizeof is an operator which operates on the type of the expression which you passed to it. So when you write sizeof(des) the type of the expression is char*, hence it prints sizeof(char*) which is 4 on your system. But in case of sizeof(a), the type of the expression is char[100], hence it prints sizeof(char[100]) which is 100.

A more interesting case with sizeof is discussed here:

  • sizeof taking two arguments


Because it returns the size of the pointer des which points to the allocated memory.


The sizeof operator yields the no. of bytes for a type.

sizeof des gives 100, because the type of des is a char[100], a char always takes 1 byte, and there's 100 of them.

sizeof a gives 4 because the type of a is a char*, and a char pointer takes 4 bytes on your platform (which is typical on 32 bit platforms.)

So, sizeof just works on the type of it's operand (note, sizeof is an operator, it is not a function/macro). sizeof does not interact with malloc, and the fact that a points to the start of 100 dynamically allocated bytes is irrelevant.


I'll elaborate slightly upon abelenky's comment. Do you expect these two typedefs to product equivalent types?

typedef char *string;
typedef char string[100];

How about these two?

typedef struct { char *s; ... } header_t;
typedef struct { char s[100]; ... } header_t;

Your answer is clearly no in both cases. Yes, you could compile des=a because a yields a pointer, but a is actually an array.

In particular, you also know this last typedef reserves no memory for a pointer. As a result, a has roughly the type char * const meaning a is a constant pointer to non-constant characters, and thus a=des gives a type error. I donno if said error message will complain about arrays, const, or an lvalue though.


The answer to this is very simple.

The type of des is "char *" or the address of a character. So the question you are asking the compiler by using sizeof is "How much memory (in bytes) do I need to store the address of a character?" The compiler responds 4 because your program and compiler has 32bit (4 bytes) addressing. When you use sizeof on an array you are asking a different question which is "How much memory (in bytes) do I need to store a character array of 100 items?" The answer here is 100 items * 1 byte = 100 bytes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜