JavaScript EditorFree JavaScript Editor     Ajax Editor 



Main Page
  Previous Section Next Section

Memory Management

C++ has a new memory management system based on the operators new and delete. They are equivalent to malloc() and free() for the most part, but are much smarter because they take into consideration the type of data being requested and/or deleted. Here's an example:

In C, to allocate 1,000 ints from the heap:

int *x = (int*)malloc(1000*sizeof(int));

What a mess! Here's the same thing in C++:

int *x = new int[1000];

Much nicer, huh? You see, new already knows to send back a pointer to int—that is, an int*—so you don't have to cast it. Now, to release the memory in C, you would do this:

free(x);

In C++, you would do this:

delete x;

They're about the same, but the cool part is the new operator. Also, use either C or C++ to allocate memory. Don't mix calls to new with calls to free(), and don't mix calls to malloc() with calls to delete.

      Previous Section Next Section
    



    JavaScript EditorAjax Editor     JavaScript Editor