Memory Allocate new

Started by
22 comments, last by monkeyboi 11 years, 9 months ago

Why were you downvoted??? you were spot on in everything you mentioned.

oh and yeah i completely forgot about 2[var] syntax. I never use it since its confusing.

What do you mean overloading new? I'm not sure i've come accross that use of new



Also monkeyboi other things about memory management:

  • When using new, as stated before, it goes to the heap. If you allocate too many items, you will run out of memory unless you deallocate them. When you continually allocate memory but never free it, this is called a Memory Leak
  • To deallocate, you use the delete key word.
    say I put SomeClass* x = new SomeClass(); to deallocate it just use delete x; (keep in mind this calls the destructor for SomeClass and frees the memory that x was pointing to, allowing later allocations to use that memory)
    but if you allocate an array you have to do something else; in the case of char* ca = new char[3]; you will have to use delete [] ca;



Overloading new... I don't recommend what I'm doing here (since it's stupid) but it's a quick example:
[source lang="cpp"]static char sBuffer[512];

void * operator new (size_t sz)
{
return (void *)sBuffer;
}[/source]
That won't return the pointer from the heap, but rather will return sBuffer.

In regards to placement new:
[source lang="cpp"]char buffer[512];
new (buffer) Object;[/source]

That also will not use the heap, but will construct the object within buffer.

For placement new, do not ever call delete. Call the destructor manually. It will crash/cause a black hole as it is not a valid pointer to data on the heap.

[source lang="cpp"]char buffer[512];
new (buffer) Object;
((Object *)buffer)->~Object();[/source]

Although technically beyond the scope of mid-level C++, he should also have familiarity with the C allocators (malloc, calloc, realloc, ...) and deallocator (free).

They function relatively the same as new (with a few caveats) except that they do not call constructors/destructors. The pointers may also not be interchanged between malloc and delete, for instance.

alloca, if it's present, will allocate the memory on the stack (by decrementing the stack pointer, generally). I don't usually recommend it as it's not safe - a failure for alloca to allocate results in a stack overflow.
Advertisement
Also don't forget that using malloc/calloc etc you must actually put how many bytes you want to allocate.

int* c = (int *) malloc( sizeof(int) * 30); // this will give you 30 elements of integers
Always improve, never quit.
Firstly, use 'sizeof( )' instead of 'strlen( )':

int size = sizeof( char ) * 4;

Also, if you declare a static sized array like:
char myChar[4];
sizeof( myChar ) = 4,
int myInt[4];
sizeof( myInt ) = 16
because the compiler is able to determine the size of this at compilation time.

It cannot do this for run-time allocated array using new or malloc because you'll always be comparing the size of the type of the variable you pass in which is a pointer.
int* myInt = new int[4];
sizeof( myInt ) = 4,

*Or in this case the size is '8' because we're on a 64bit compiler, and all pointers are 8 bytes in size (According to the standard).
sizeof( char* ) = 8
sizeof( short* ) = 8
sizeof( int* ) = 8
....etc etc

Secondly, in C/C++ you don't have array access checks to warn you if you're going out-of-bounds, which leads to a typical problem of memory
overwrites which every C/C++ developer has had fun with. With the array defined as:
char myArray[4] = "123";

it is possible to do this without causing any compiler errors:
char value1 = myArray[5];
char value2 = mrArray[-1];

The value in value1 & value2 is random, could be anything, valid, in-valid, garbage, another object's data... What they are is the value stored at the address of 'myArray[0] plus 5 bytes '(value1), and 'myArray[0] minus 1 byte' (value2), if that makes sense :S

Finally, to give some further detail and explaination to the point of the memory spacing you got between ta & tb (40 hex, 64dec) can also be compiler and OS specific. For instance Windows, and in debug libs being used it can allocate more memory then you actually asked for, so if you asked for 4 bytes of data you might actually get 8 bytes. The additional 4 bytes can be given 2 bytes eitherside of your memory for padding. Padding which can be used to detect memory-overwrites or corruption, or prehaps prevent even minor cases over overwrites (ie. if it only corrupts the padding leaving your game-data untouched).

However still don't expect several calls of 'new' to give memory allocs sequentially ordered from eachother, use a single 'new' call for the total 'char' you need and reallocate from that eg.

char* myCharArray = new char[8];
char* ta = &myCharArray[0];
char* tb = &mayCharArray[4];

Memory Address:
ta = 0x000a5fc0
tb = 0x000a5fc4

(tb)0x000a5fc4 - (ta)0x000a5fc0 = 4

Hope this helps explain a few things.
Thanks you all guys

I indeed learn a lot from this thread.

Jerry

This topic is closed to new replies.

Advertisement