Satbir
Satbir

Reputation: 6496

delete & new in c++

This may be very simple question,But please help me.

i wanted to know what exactly happens when i call new & delete , For example in below code

char * ptr=new char [10];

delete [] ptr;

call to new returns me memory address. Does it allocate exact 10 bytes on heap, Where information about size is stored.When i call delete on same pointer,i see in debugger that there are a lot of byte get changed before and after the 10 Bytes.

Is there any header for each new which contain information about number of byte allocated by new.

Thanks a lot

Upvotes: 6

Views: 798

Answers (8)

camh
camh

Reputation: 42438

While the specifics are implementation dependent, one piece of information the implementation will need to store is the number of elements in the array. Or if it does not store it directly, it will need to accurately derive it from the block size allocated.

The reason for this because if an array of objects is allocated with new[], when they are deleted with delete[], the destructor of each object in the array will need to be called. delete[] will need to know how many objects to destruct. This is why it is necessary to match new with delete and new[] with delete[].

Upvotes: 0

sep
sep

Reputation: 3485

It is implementation dependent, but mostly the metadata for a block of memory is usually stored in the area before the memory address returned. The change that you observed before the 10 bytes was likely metadata being updated for this block (likely the size of the block being written into the meta data), and after the 10 bytes were metadata being updated for the next block (still unallocated, likely the pointer to the next chunk on the free list).

It is not a good idea to mess with the heap as it is not portable. However, if you want to do such heap magic, I suggest you implement your own memory pools (just get a large chunk of memory from the heap and manage it yourself). A possible place to start would be to look at libmm.

Upvotes: 0

jweyrich
jweyrich

Reputation: 32240

Precisely, according to the standard, new char[10] will alloc at least 10 bytes in the heap.

The internals of new and delete are implementation dependent. So it will vary from compiler to compiler, and platform to platform. Additionally, you can find a variety of allocator algorithms (e.g: TCMalloc).

I'll give you an overview of how it could work internally, but don't take it as absolute truth. It's written for the solely purpose of this explanation.

In short, the new operator internally invokes malloc. The malloc uses a really long linked list of available memory blocks, aka free chain. When malloc is invoked, it lookups this list for the first block that's big enough to hold the requested size. After that, it splits the block in two parts, one with the size you requested, and the other with the rest, which is then added back to the free chain. Finally, it returns the block with the request size.

The inverse occurs in a free call, which is invoked by delete/delete[]. In short, it puts the provided block back to the free chain.

There could be fancy tricks during the processes I described above, like sorting the free chain, rounding the requested size to the next power of two to reduce memory fragmentation, and so on.

Upvotes: 2

sth
sth

Reputation: 229583

That's all up to the compiler and your runtime library. It's only exactly defined what effects new and delete have on your program, but how exactly these are acieved is not specified.

In your case it seems like a little more memory than requested is allocated and it will probably store management information like the size of the current chunk of memory, information about adjacent areas of free space or information to help the debugger try to detect buffer overflows and similar problems.

Upvotes: 5

sepp2k
sepp2k

Reputation: 370112

Is there any header for each new which contain information about number of byte allocated by new.

That's platform dependent but yes, on many platforms there are.

Upvotes: 3

sharptooth
sharptooth

Reputation: 170489

It is completely implementation-dependent. In general case you have to store the number of elements elsewhere. The implementation must allocate enough space for at least the number of elements specified, but it can allocate more.

Upvotes: 3

Johann Gerell
Johann Gerell

Reputation: 25581

Do it allocate exact 10 bytes

That's implementation dependant. The guarantee is "at least 10 chars".

Where information about size is stored?

That's implementation dependant.

Is there any header for each new which contain information about number of byte allocated by new?

That's implementation dependant.

By "that's implementation dependant" I mean it's not defined in the standard.

Upvotes: 13

Vijay
Vijay

Reputation: 67211

char * ptr=new char [10];

You are creating an array of 10 character's in heap and storing the address of 0th element in a pointer.this is similar to doing an malloc in C

delete [] ptr;

You are deleting(freeing the memory) the heap memory which was allocated by the earlier statement.this is similar to doing a free in c.

Upvotes: 0

Related Questions