Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

delete keyword

Posted on 1999-08-12
3
Medium Priority
?
190 Views
Last Modified: 2010-04-16
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.
0
Comment
Question by:RayLeong
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 3

Accepted Solution

by:
LucHoltkamp earned 120 total points
ID: 1202339
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,
Luc
0
 
LVL 2

Expert Comment

by:basant
ID: 1202340
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.
0
 

Author Comment

by:RayLeong
ID: 1202341
Hi LucHoltkamp and basant

Thanks for the detailed explaination.

Rgds,
Raymond
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question