array allocaton size

K

Keith S.

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
 
K

Karl Heinz Buchegger

Keith S. said:
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 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.
 
K

Keith S.

Karl said:
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.

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

- Keith
 
S

SaltPeter

Keith S. said:
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

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.
 
V

Victor Bazarov

Keith said:
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.

Is that 'sizeof(fred) * 2'?
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?

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

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

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,173
Messages
2,570,938
Members
47,474
Latest member
VivianStuk

Latest Threads

Top