Z’s

August 24, 2007

A Little C/C++ Recipe You Can Live Without Knowing :)

Filed under: C/C++,Techs — mdzahidh @ 8:11 pm

Yes, read this if you are kind of bored or probably want to be too fussy about “How things work?” Have you ever wondered how compilers know how many instances of a class had been previously allocated when delete [] is called; because delete [] might have to traverse through each of the object and call their destructor. So it must be running some kind of loop right? But where does the compiler get the number of instances?

C/C++ compilers play a nifty trick when you allocate memories with “new.” The only time the compiler needs to traverse through each object and call destructor is when you provide a destructor, right? When you do not provide any destructor there is no point in calling destructor, hence no point in traversing through each object. In such case, when destructor are not explicitly provided by the coder, delete [] simply deallocates the memory, very much like “free().”

But things are different when you have an explicit destructor in a class. When “new” is called to allocate memory for such a class, it allocates 4 extra bytes. The first 4 bytes are actually used to store an integer….. ah you guessed it right :)…. that’s exactly the number of instances of the object. So for the following code

class A{
public:

int m_uselessVariable;A(){} // constructor
~A(){} // destructor

};

A *p = new A[10]

What the “new” does in this case, is allocate 4 extra bytes = sizeof(int), i.e. total memory allocated will be sizeof(A)*10+4: use the first 4 bytes to store 10 and simply return the pointer from the 5th byte.

When

delete [] p

is called, the compiler simple checks the 4 bytes behind p, [i.e. *((init*)((char*)p – 4)) ] or more simply [ *((int*)p – 1) ] and reads an integer, which will return 10 for the above case. Now I hope i do not have to explain anymore what the compiler does with that number!

You can verify all these stuffs by overloading “new” and “delete” operators and see by yourself how many bytes are requested upon memory allocation. And also verify that when a class doesn’t have any explicit destructor it doesn’t allocate that mysterious 4 extra bytes, because it doesn’t need to traverse objects while “delete []”

Yes, you can ofcourse lead a healthy C/C++ life without even knowing all these :).

PS: I happen to discover this almost 3 years ago while developing a leak-detector library, quite esoterically named “Terror Lib,” for a game engine while at eSophers.

Advertisements

5 Comments »

  1. Nice one for the C++ freaks! OH! and the Terror lib! Brings back some freaking but cheerful memories 🙂

    Comment by Remon — August 26, 2007 @ 6:44 am | Reply

  2. Thanks for the nicely explained info. Google brought me to your door in my hour of need 🙂

    Comment by John — July 13, 2008 @ 3:03 am | Reply

  3. […] ojczysty więc odsyłam do źródła zachęcam do lektury (tytuł dodatkowo mówi sam za siebie): https://mdzahidh.wordpress.com/2007/08/24/a-little-cc-recipe-you-can-live-without-knowing/ Tagi: […]

    Pingback by Magiczne delete [] | pamiętnik programisty — June 22, 2009 @ 6:10 pm | Reply

  4. Great one thank you man, makes me understand C++ a lot deeper when i learn this neat stuff.

    i was wondering though if this is a standard of C++, do all compilers implement it this way?

    keep up the good work

    Comment by Wiaam — July 27, 2009 @ 6:26 am | Reply

  5. its nice

    Comment by naina — November 3, 2010 @ 5:20 am | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: