sizeof() operator – not a trivial thing
February 28, 2010 Leave a comment
sizeof() operator … Remember it?
That one mostly used when dynamically allocating memory:
int *pointer; /*pointer to type int, used to reference our allocated data*/
pointer = malloc(sizeof(int) * 10);
Some things about it:
- it returns the size in bytes of any primitive or user-defined variable
- it always returns something positive, as it is defined it returns an unsigned size_t type
- if a type is used like in this example
sizeof(int)
its argument needs to be parenthesized
-if just variable’s name is used parenthesis can be omitted
;
char c;
sizeof c
- in the C standards preceding C99 it was a compile-time operator, meaning that it calculated only sizes defined at compilation, it could not calculate something like this
char c[];


