Connecting Tech Pros Worldwide Forums | Help | Site Map

array allocaton size

Keith S.
Guest
 
Posts: n/a
#1: Jul 22 '05
This may be a dumb question, but I've searched the FAQ
and can't find an obvious answer...

Let's say I have a class e.g.

class fred
{
public:

int x;
int y;
};

then I create an array of fred:

freds = new fred[2];

Now, I'd expect to have allocated 8 bytes * 2 i.e. 16 bytes.
However, if I step through the code with a debugger I see
that operator new is getting called with a size of 20 bytes.
Why is this? What's the extra 4 bytes coming from?

The platform is VC++ .NET 2003 in debug mode if it makes any
difference...

- Keith


Karl Heinz Buchegger
Guest
 
Posts: n/a
#2: Jul 22 '05

re: array allocaton size


"Keith S." wrote:[color=blue]
>
> This may be a dumb question, but I've searched the FAQ
> and can't find an obvious answer...
>
> Let's say I have a class e.g.
>
> class fred
> {
> public:
>
> int x;
> int y;
> };
>
> then I create an array of fred:
>
> freds = new fred[2];
>
> Now, I'd expect to have allocated 8 bytes * 2 i.e. 16 bytes.
> However, if I step through the code with a debugger I see
> that operator new is getting called with a size of 20 bytes.
> Why is this? What's the extra 4 bytes coming from?[/color]

The compiler needs to store somewhere how many elements are
in the array. This information is needed at destruction time
to call the correct number of destructors.

A simple way of doing this is: allocate a littel bit more memory
and store that information there.

--
Karl Heinz Buchegger
kbuchegg@gascad.at
Keith S.
Guest
 
Posts: n/a
#3: Jul 22 '05

re: array allocaton size


Karl Heinz Buchegger wrote:
[color=blue]
> The compiler needs to store somewhere how many elements are
> in the array. This information is needed at destruction time
> to call the correct number of destructors.[/color]

Thanks - I guess that makes sense, indeed the amount of memory
allocated is always one word more than the number of items.

- Keith

SaltPeter
Guest
 
Posts: n/a
#4: Jul 22 '05

re: array allocaton size



"Keith S." <false@ntlworld.com> wrote in message
news:2k2cefF16j5umU1@uni-berlin.de...[color=blue]
> This may be a dumb question, but I've searched the FAQ
> and can't find an obvious answer...
>
> Let's say I have a class e.g.
>
> class fred
> {
> public:
>
> int x;
> int y;
> };
>
> then I create an array of fred:
>
> freds = new fred[2];
>
> Now, I'd expect to have allocated 8 bytes * 2 i.e. 16 bytes.
> However, if I step through the code with a debugger I see
> that operator new is getting called with a size of 20 bytes.
> Why is this? What's the extra 4 bytes coming from?
>
> The platform is VC++ .NET 2003 in debug mode if it makes any
> difference...
>
> - Keith
>[/color]

Think of it like this:

class Fred
{
int x;
int y;
};

int main()
{
Fred* p_freds3 = new Fred[3];

delete [] p_freds3;

Fred* p_freds2 = new Fred[2];

delete [] p_freds2;

return 0;
}

Once you attempt to delete the arrays with:

delete [] p_freds2;
delete [] p_freds3;

How does the compiler know how many Fred destructors to invoke for each
pointer? You can also compare the difference in memory allocated between an
array of 2 and the array of 3 Freds to confirm your findings.



Victor Bazarov
Guest
 
Posts: n/a
#5: Jul 22 '05

re: array allocaton size


Keith S. wrote:[color=blue]
> This may be a dumb question, but I've searched the FAQ
> and can't find an obvious answer...
>
> Let's say I have a class e.g.
>
> class fred
> {
> public:
>
> int x;
> int y;
> };
>
> then I create an array of fred:
>
> freds = new fred[2];
>
> Now, I'd expect to have allocated 8 bytes * 2 i.e. 16 bytes.[/color]

Is that 'sizeof(fred) * 2'?
[color=blue]
> However, if I step through the code with a debugger I see
> that operator new is getting called with a size of 20 bytes.
> Why is this? What's the extra 4 bytes coming from?[/color]

Some compiler-/OS-specific dynamic memory allocation information
is stored there, perhaps. Why do you care?
[color=blue]
> The platform is VC++ .NET 2003 in debug mode if it makes any
> difference...[/color]

You might want to ask in microsoft.public.vc.language if you need
to know more about VC++'s memory manager.

V
Closed Thread