Size of fixed size object?

• 04-07-2009
shwetha_siddu
Size of fixed size object?
Dear All,
How many bytes following peace of code will take?

Code:

``` char *num[10] = { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten" } ;```
Shwetha
• 04-08-2009
laserlight
Including a newline at the end, there are 101 characters in that piece of code, so the answer is likely to be 101 or 103.

You might want to phrase your question more accurately. For example, if you are asking for the size of num in bytes, then the answer would be sizeof(char*) * 10.
• 04-08-2009
matsp
It would not be possible to calculate EXACTLY how many bytes are used by the strings pointed to by the array members, since there is no good way to tell generically what (if any) padding the compiler produces between strings.

But the formula to calculate the minimum total size is 10 * sizeof(char *) + length of each member + 1. The compiler may, however, pad everything to even 2, 4, 8 or 16 bytes for it's own ideas of optimization and/or processor architecture requirements.

Further, how much ACTUAL space is used would depend on where this line is placed. If it's a local variable inside a function, the array needs to be initialized every time the function is called [or the block is entered, if it's inside another block within a function]. This means (at least) copying 10 pointers from one place to another, which may be anything from 3 instructions upwards (probably no more than 50 instructions, but certainly 40 is quite plausible for a 29K processor for example, since it needs 2 instructions to load a 32-bit address and one instruction to store the value and one move to the next location).

--
Mats