Strange case while using the sizeof() operator in C

安稳与你 提交于 2019-12-13 08:37:50

问题


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
100

As opposed to:

100
100

回答1:


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.




回答2:


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.




回答3:


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




回答4:


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



回答5:


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.




回答6:


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.




回答7:


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.



来源:https://stackoverflow.com/questions/7698885/strange-case-while-using-the-sizeof-operator-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!