• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 195
  • Last Modified:

delete keyword

I have a link list and i wish to delete the entire list
free the memory back to the OS. Does the following codes
does what i want?

    List* list = new List;
    delete[] list;  // list points to the head
    list = NULL;    // of the link list

Or should i do a loop.

    List* temp = list;
    while(list) {
        list = list->next;
        delete temp;
        temp = list;
    list = temp = NULL;
Does the first method works? What is the best way to
free the memory held by a link list? I want to be
sure that all memory is free, does the second method
actually free the entire list or i must go into the
list and free every variable one by one.
1 Solution
If you allocate a array in C++, like:
List *list(new List[number])
You can simply delete the array with:
delete []list
The [] is neccesary, if you omit it, the memory is released anywhay, but the destructor is only called for the first array entry (list[0]). The [] ensures that the destructor is called for every array entry.
The for-next is only neccesary if you do not allocate the container as a single memory block (an array, with a single new statement).
For instance if you wanted to make a 2 dimensional array:

Array **array(new Array*[size1]);
for (int i(0); i < size1; ++i) array[i] = new Array[size2];

If you want to delete this, you have to use a for-next again, because you didn't allocate it as a single block:
for (int i(0); i < size1; ++i)
  delete []array[i];
delete []array;

Notice that you can omit the [] if the array of you allocate doesn't calls any destructor, but it's good practice to do it anywhay. For instance:

char *str(new char[10]);
delete str; // ok because char has no destructor, its a intrinsic type

delete []str // better practice

Under the hood, there is no difference for delete and []delete (except for the destructor thing). In both cases it results in a call to free

Always remeber:
The number of new and delete statements should be exactly balanced.
If you use new[...], also use []delete.

Hope this helps,
No First method will not work because delete[]
should be used with new[]. What I mean is :
int* a = new int[10];

delete[] a;

When you use new[] then the C++ library also stores
the number of bytes you asked. Which it uses during
deletion by delete[].

Your second way is the write way to delete. But
if you want to use the first syntax. You can
overload delete[] operator and write the same
code( second) as you mentioned.
RayLeongAuthor Commented:
Hi LucHoltkamp and basant

Thanks for the detailed explaination.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now