M
mikeyz9
Hi,
I understand that you can actually create object with zero length
array. Like example below:
struct Weirdness
{
int i;
Weirdness() : i(10) {std::cout << "im here" << std::endl;}
};
Weirdness* testing = new Weirdness[0];
My understanding is that new operator actually creates a new object of
the array size given from Heap through malloc/calloc at the end of the
line. Can anyone give me under the hood explanation on what is
actually going on with zero-length array, perhaps compiler
implementations on how it behaves under the hood?
My questions are the following:
1. Where does it allocate memory from?
2. Where does the zero-length array pointer points to? a valid address
but only readable?
3. What is the size being allocated?
5. Does it allocate several bytes randomly from the heap? or there is
a special section of memory dedicated for this weirdness?
6. Can a user use delete operator to de-allocate this memory?
When I tried the code above using GNU Compiler (gcc 4.3.3), It gives
me back valid address but I can not write anything to it (check the
code above, it will set i to 0 default value on new Weirdness[0];
while i to 10 for new Weirdness[1]
. It also did not print anything,
it looks like the constructor not even being called at all.
Thanks in advance for the help,
Tom
I understand that you can actually create object with zero length
array. Like example below:
struct Weirdness
{
int i;
Weirdness() : i(10) {std::cout << "im here" << std::endl;}
};
Weirdness* testing = new Weirdness[0];
My understanding is that new operator actually creates a new object of
the array size given from Heap through malloc/calloc at the end of the
line. Can anyone give me under the hood explanation on what is
actually going on with zero-length array, perhaps compiler
implementations on how it behaves under the hood?
My questions are the following:
1. Where does it allocate memory from?
2. Where does the zero-length array pointer points to? a valid address
but only readable?
3. What is the size being allocated?
5. Does it allocate several bytes randomly from the heap? or there is
a special section of memory dedicated for this weirdness?
6. Can a user use delete operator to de-allocate this memory?
When I tried the code above using GNU Compiler (gcc 4.3.3), It gives
me back valid address but I can not write anything to it (check the
code above, it will set i to 0 default value on new Weirdness[0];
while i to 10 for new Weirdness[1]
it looks like the constructor not even being called at all.
Thanks in advance for the help,
Tom