8

How to determine the size of the memory allocated by C++ new operator. ?

In C, malloc has the syntax:

void *malloc(size_t size);

here we know what size we are allocating.

but in C++ how can we determine what size is allocated when we do memory allocation as below. I am interested to know how new determines the size that needs to be allocated.

foo *pFoo = new foo();
Nrupatunga
  • 469
  • 1
  • 8
  • 16
  • This question led me to some interesting references. Clearly it's deliberately not made easy for you to find out how big an object is. (After all, if you use that information the wrong way you break encapsulation pretty badly.) But of course I can think of reasons you would want to know, and it's not trivial... – RobP Apr 14 '14 at 05:33
  • 1
    Use the `sizeof` operator. – 001 Apr 14 '14 at 05:33
  • @JohnnyMopp sizeof() is likely to return 1. http://stackoverflow.com/questions/4338966/c-how-can-i-know-the-size-of-base-class-subobject/4339216#4339216 – RobP Apr 14 '14 at 05:35
  • @RobP It isn't 'likely'. It's *possible.* Your link doesn't put it any higher than that. And it's still the answer to the question, apart from a possible vtable pointer. – user207421 Apr 14 '14 at 05:39
  • @RobP: `sizeof` returning 1 for empty classes is desirable here... `new` should return a distinct pointer to a single byte too. 5.3.4/14 "`new T` results in a call of `operator new(sizeof(T))`" – Tony Delroy Apr 14 '14 at 05:44
  • I think the spirit of the question isn't literally how much memory is allocated by new() itself, but by all the downstream constructor code... how much memory does it actually use to make one of these here objects? – RobP Apr 14 '14 at 06:05
  • Use ```new char[size]``` – Supergamer Nov 23 '22 at 14:54

3 Answers3

8

The C++ operator new allocates sizeof(T) bytes (either using the standard global allocator ::operator new(size_t) or a custom allocator for T if it has been defined).

After that it calls the constructors (first bases and other sub-objects and then the constructor for T itself).

It's however possible and even common that some of the constructors called allocate more memory.

6502
  • 112,025
  • 15
  • 165
  • 265
2

For test purposes, you can override global operator new to find out how much is allocated:

void* operator new(size_t size)
{
    std::cout << "allocating " << size << std::endl;
    return malloc(size);
}

[ Really not recommended for production code, but it can be done - read up very carefully.].

Keith
  • 6,756
  • 19
  • 23
-3
void *malloc(size_t size);

Here, you specify the size that needs to be allocated and what it returns is void* which you can cast it later to the required pointer type.

foo *pFoo = new foo();

Here, the size is decided by the new itself by going through the object layout. Also note that the new operator calls the constructor of foo. If you need it the same you did it for malloc, do it like below:

 void * ptr = new size_t[size];

Not even satisfied..! You can overload new operator the way you want. Google for overloading new operator.

Sivaraman
  • 438
  • 2
  • 9