?
Solved

pointers and memory question

Posted on 2003-03-06
2
Medium Priority
?
210 Views
Last Modified: 2010-04-01
Hi,
I am a novice c++ programmer and I am confused about a couple of issues with pointers and memory allocation.   I have three subquestions concerning a class that contains a pointer member variable, but I don't explicitly assign it to a memory location on the free store using new within the class anywhere.  

Part One:
Do I need to worry about deleting it in the destructor?

 For example, here is a Node class that could be used in a linked list.

class Node
{
public:
Node(): next(0) {};
~Node(); // need to delete next here?
private:
Node * next;  // Here is the pointer in question
double its_length;
};

Now imagine I make a pointer to a Node and allocate it some space on the heap with new:

int main()
{
Node * pNode = new Node;  // Line 3
delete pNode;
pNode = 0;
return 0;
}
Part two :
After the use of new in line three, I have pNode point to an object Node allocated on the free store, is the member variable next now on the free store and do I have to worry about delete within the destructor of Node?  This brings up the point that as the author of the class Node but not the main function necesssarily, I don't know whether the Node objects will be placed on the heap or not.  

Part three:
If I don't need to use delete in this simple example, that means the default copy constructor, and assignment operators are also fine?

Thanks for helping me clear up this confusion!
0
Comment
Question by:kevin_in_va
[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
  • 2
2 Comments
 

Expert Comment

by:Grass_Hopper
ID: 8084911
Part 1:
~Node(); // need to delete next here?

I think if you call delete here for "Next",
you will be in effect calling delete on every next node in the list. Unless that is what you want to do, the answer to #1 is no.

Part2:
is the member variable next now on the free store ...

When you call delete on a node, you are freeing the memory for that instance.
The "Next" pointer doesn't need to be deleted.
Pointers are usualy alllocated from the stack, and hold the memory address, in this case of the "node" which is allocated from the heap by calling "new".

Part3:
A pointer to the head of the list should be passed to the constructor to initialise the "Next" member.
If you are creating the first node, you should pass it NULL.

does this make any sense ?
0
 

Accepted Solution

by:
Grass_Hopper earned 400 total points
ID: 8085029
Example: constructor for linked list class.
(this class uses the default destructor)

__fastcall TTileNumber::TTileNumber(int number, int posX,
                                           int posY, TTileNumber* Next)
{
    FTileNumber = number;
    FX = posX;
    FY = posY;
    FNext = Next;
}

Example: searching through linked list.

TTileNumber*__fastcall TTileNumber::FindNumber(int number)
{
    if(number == FTileNumber)
        return (TTileNumber*) this;
    if(!FNext)
        return (TTileNumber*) 0;
    return FNext->FindNumber(number);
}


if you call delete on "FNext" in the destructor, there would be nothing to search for. :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

765 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