473,396 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

array allocaton size

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

Jul 22 '05 #1
4 1027
"Keith S." wrote:

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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2
Karl Heinz Buchegger wrote:
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

Jul 22 '05 #3

"Keith S." <fa***@ntlworld.com> wrote in message
news:2k*************@uni-berlin.de...
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.

Jul 22 '05 #4
Keith S. wrote:
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
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Noah Spitzer-Williams | last post by:
Hello guys, I would like to do something seemingly simple: find out if an element in an array that is passed to my function exists. I used to think I could just do: if (arr) ... However, if...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
3
by: ritchie | last post by:
Hi all! Still working on this program! Just to recap, I am writing a program to sort an array with four different sort algorythms. I am having a little trouble at the moment though! Now, I...
8
by: Gerald | last post by:
I have a problem with an array of pointers. In a program I'm writing, I have to read a file, containing thousands of short lines. The content of another file will be compared against each line...
6
by: Herrcho | last post by:
in K&R Chapter 6.3 it mentions two methods to calculate NKEYS. and points out the first one which is to terminate the list of initializers with a null pointer, then loop along keytab until the...
22
by: Wynand Winterbach | last post by:
I think every C programmer can relate to the frustrations that malloc allocated arrays bring. In particular, I've always found the fact that the size of an array must be stored separately to be a...
7
by: simkn | last post by:
Hello, I'm writing a function that updates an array. That is, given an array, change each element. The trick is this: I can't change any elements until I've processed the entire array. For...
12
by: manochavishal | last post by:
Hi, I have a question. How can i know the size of array when it is passed to a function. For Example i have this code: #include <stdio.h> #include <stdlib.h>
28
Nepomuk
by: Nepomuk | last post by:
Hi! I've read, that in C++ there's no predefined method, to calculate the size of an array. However, it's supposed to work withsizeof(array)/sizeof(array)Now, this does work in some situations, but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.