Sizeof looks at the type of the given expression; it does not evaluate the expression. Thus, you only need to make sure that the variables used in the expression are declared so that the compiler can infer their type.
In your example, st is already declared as pointer-to-struct-retValue. Therefore, the compiler can infer the type of the expression "* st".
Although it does not look like it is already declared in your code, the compiler has already taken care of this for you. All declarations in your code are moved to the beginning of the block in which they are executed by the compiler. Suppose you write
One way to illustrate the knowledge available to the compiler is to look at the intermediate output that it creates. Consider this code example ...
struct retValue {long int a, long int b}; ... printf("Hello World!\n"); struct retValue* st = malloc(sizeof(*st));
Using gcc as an example and above, the code in main () of the test.c function looks at the intermediate output by running ...
gcc -fdump-tree-cfg test.c
The compiler will generate a test.c.022t.cfg file - Look at it and you will see
[ ... removed internal stuff ...] ;; Function main (main) Merging blocks 2 and 3 main (argc, argv) { struct retValue * st; int D.3097; void * D.3096; # BLOCK 2 # PRED: ENTRY (fallthru) __builtin_puts (&"Hello World!"[0]); D.3096 = malloc (16); st = (struct retValue *) D.3096; D.3097 = 0; return D.3097; # SUCC: EXIT }
Notice how the declaration was moved to the beginning of the block, and the argument in malloc has already been replaced with the actual value indicating the size of the type by which the expression was evaluated. As noted in the comments, the fact that the ad was moved to the top of the block is a detail of the compiler implementation. However, the fact that the compiler is able to do this, as well as insert the correct size in malloc, indicates that the compiler was able to output the necessary information from the input.
I personally prefer to specify the actual type name as the sizeof parameter, but this is probably a coding style issue where I would say that consistency is superior to personal preference.
Voidpointer
source share