A place where programmers can discuss various programming topics and experiences.



Know Your C++: The new operator and operator new

Make sure you know how the new operator works when you use it in your C++ programs. Here is an example of how it is normally used:

wchar_t * myString = new wchar_t[256];

Most C++ programmers are familiar with this call and will tell you that the new operator invokes the target object,s constructor. That constructor performs operations like initializing member variables and whatnot. Yes, this is true, but how is this memory allocated and who allocates it?

This task is performed by a call to operator new (could the names be more confusing?). You can think of this as like a call to malloc(). operator new is usually declared like this:

void * operator new(size_t size);

The return type is void* because it returns a pointer to raw, uninitialized memory (on the heap or free store). You will also notice that the size_t parameter actually tells the routine how much memory to allocate. In the case where operator new cannot allocate the desired size, it will either return NULL or throw an exception. Now that we know who actually allocates the memory for the new operator, let us look at how that memory is allocated.

When a heap allocation request is invoked, a scan is performed that looks for a sufficiently large chunk of contiguous memory. This entails traversing the heap through pointers that point to the next areas on the heap that contain free memory blocks. When a sufficiently sized block is found, the memory is "tagged" with a size value (the boundary of where this allocated chunk ends) and pointers are rearranged so that this memory block is not seen as "free" (until we delete it later). Finally, a pointer to the starting address of this newly allocated section on the heap is then returned to the caller of operator new (in our case that is the new operator).

Now that our starting memory address is returned we can continue with the last task that the new operator must perform. This final step is the initialization of the allocated memory via invocation of the target object's constructor.

When a program is finished with its dynamically allocated memory, it must be manually released by invoking the delete operator. As with the new operator, a similar process occurs when operator delete is called by the delete operator (yes, this naming is just as confusing as with new). When this occurs, the deallocated memory is added back to the top of the heap so it can be reused.

To summarize, we can see that a call to the new operator, always performs the following two operations:

Knowing this type of information is a must for C++ programmers. Even though you may never need to overload operator new (you can,t override the new operator) it is useful information for understanding how C++ allocates and instantiates user-defined objects.

-Gilemonster

** Update **
There are a couple more items to mention that have come from comments and other readers.

** End Update **

Labels:

posted by Gilemonster @ 8:37 PM,

2 Comments:

At 12:54 PM, Anonymous Chris Selvaggi said...

Good article. I hadn't reflected on the similarity and differences between operator new and new operator. Mostly I'm used to seeing operator new, probably because there are many, many operators in C++ that you can override (operator= being one of the more common).

I do have one comment about the summary part of the article, which describes how the new operator works. If you're instantiating an array of objects, the new operator doesn't invoke the operator new function. Instead, it invokes the operator new[] function. Just to be confusing, this is more commonly known as the array new function. More trivia for you...

 
At 10:51 AM, Blogger Gilemonster said...

Excellent suggestion Chris! I'll update the article now to include your comments. Thanks again.

 

Post a Comment

Links to this post:

Create a Link

<< Home