Re: array allocaton size

From: Karl Heinz Buchegger (kbuchegg_at_gascad.at)
Date: 06/25/04


Date: Fri, 25 Jun 2004 12:35:18 +0200


"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
kbuchegg@gascad.at


Relevant Pages

  • Re: read keyboard input and storing in an array?
    ... > I'm trying to store user input in an array, ... You have the beginnings of that logic already since your prompt tells the ... 201st value since the array only has room for 200 values. ... This will force you to store the int values as Integer ('Integer' ...
    (comp.lang.java.help)
  • Re: attempting an actual game...
    ... >>> and inflexible by the absurd decision to use a bit array for square ... as then one has 8 bits in which to store a color and a few flags ... Using a 2D int (or, ... > Change direction and you may eventually complete a game. ...
    (comp.games.development.programming.misc)
  • Re: Adding large numbers in C
    ... one of the numbers - or perhaps the result - is too big to store in an int. ... you have bitstrings longer than 8 bits, simply use an array of unsigned ... Incidentally, the subtraction routine does similar juggling, so if M and N ...
    (comp.lang.c)
  • Re: Reading a Text file
    ... int main ... In any case, you said earlier you want to store the data in an array, ... don't store its result in a char. ...
    (comp.lang.c)
  • (patch for Bash) regex case statement
    ... Following up on my previous patch for regex conditional tests, ... /* Return an array of strings; ... int dollarflag, zeropad, compareflag; ... SHELL_VAR *var; ...
    (comp.unix.shell)