Jan 142013

How will you find the size of a structure in C/C++ language without using sizeof operator.

(Note: sum of sizes of all fields of a structure may not be equal to the exact memory allocated to the structure. See, Method-1 in solution). Continue reading »

Nov 062012

sizeof operator returns size in bytes. For example, In an implementation where 2 bytes (16bits) of memory is allocated to an int type, the below code will output 2

int x = 10;
printf("%d", sizeof(x)); // Output amount of memory allocated to int data type.

We talked about the sizeof operator in great detail in the last post.

If we use sizeof operator on arrays, as shown below:

int arr[10];
printf("%d", sizeof(arr)); //Will output 20

the output will be 20.

But sometimes, its not a good idea to use this operator on arrays.
Continue reading »

Nov 062012

sizeof is a unique operator in C/C++ language which returns the amount of memory (in bytes) allocated to its operand. For example, if an implementation stores an integer in 2 bytes then both the statements below will output 2.

printf("%d", sizeof(int));

int x = 10;
printf("%d", sizeof x);

Note than sizeof operator is used like a function when applied on data types.


Let’s understand some more facts about sizeof operator:
Continue reading »