How can "size in bytes" of a array of some datatype be determined?
What makes me ask this is a question from this icse board question paper.
The question is as follows:
And the accepted answers by the board are (space occupied by one of those data types*no of elements in the array)
For example , one int
array of 10
objects is said to take 4*10=40Bytes
But my question is, wouldn't the array have to store it's length and indexes somehow(increasing it's size on memory), or am I misunderstanding the question?
Your understanding is indeed correct. The official answer adopts a simplistic view of how arrays are stored.
There is a fixed cost associated with every object. Additionally, arrays need to keep track of their size and possibly other things.
The answer also ignores issues such as padding, alignment, etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With