7 Replies - 1152 Views - Last Post: 30 October 2009 - 09:58 AM Rate Topic: -----

#1 masar77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 28-May 09

delete pointer questions

Post icon  Posted 30 October 2009 - 04:03 AM

Hello,

I have a few questions regarding delete pointer, the following code:
void newPerson(Person **p, int &n) {
	string s, signum;
	char a[30];
	Name *name = new Name();
	Address *address = new Address();
.
.
.
	Person *person = new Person(*name, *address, signum);
	p[n] = person;
}



Is there a memory leak here?
I can use:
	delete name;
	delete address;


But if I use:
		delete person;



the program crashes when I try to retrieve that person object in the array,
what am I missing here?

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: delete pointer questions

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6110
  • View blog
  • Posts: 23,670
  • Joined: 23-August 08

Re: delete pointer questions

Posted 30 October 2009 - 05:01 AM

Memory leaks occur when you don't delete memory allocated with new when you're done working with it.

We'd need to see more code to understand what exactly is causing your problem.
Was This Post Helpful? 0
  • +
  • -

#3 masar77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 28-May 09

Re: delete pointer questions

Posted 30 October 2009 - 07:34 AM

View PostJackOfAllTrades, on 30 Oct, 2009 - 04:01 AM, said:

Memory leaks occur when you don't delete memory allocated with new when you're done working with it.

We'd need to see more code to understand what exactly is causing your problem.


It's not really a problem, I just want to know when and if to delete the pointers name, address and person. All these three pointers are put in a person array at the end of the function. Like this:
.
.
	Person *person = new Person(*name, *address, signum);
	p[n] = person;
	cout << endl << "Person tillagd!" << endl;
	n++;
	//delete name;
	//delete address;
	//delete person;
}



Correct me if I'm wrong, but if I delete as in the code above, I'm also deleting the person in p[n]?
If so, when should they be deleted since I'm exiting the function. Is it enough to delete[] the person array in the main program?

Thanks.
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5936
  • View blog
  • Posts: 12,862
  • Joined: 16-October 07

Re: delete pointer questions

Posted 30 October 2009 - 08:26 AM

View Postmasar77, on 30 Oct, 2009 - 08:34 AM, said:

Correct me if I'm wrong, but if I delete as in the code above, I'm also deleting the person in p[n]?

Yep.

View Postmasar77, on 30 Oct, 2009 - 08:34 AM, said:

If so, when should they be deleted since I'm exiting the function. Is it enough to delete[] the person array in the main program?

Nope.

However, your life would be easier if your person array was also wrapped in an object. Then the cleanup will be mostly automatic.

e.g.
struct PersonList {
	Person *data;
	int maxSize, size;
	
	PersonList(int ms) : size(0), maxSize(ms) { data = new Person[maxSize]; }
	~PersonList() { 
		for(int i=0; i<size; i++) { delete data[i]; }
		delete data[];
	}
	
	void addPerson(Person *person) { this->data[size++] = person; }
	
	void addPerson(const Name &name, const Address &address, const string &signum) {
		this->addPerson(new Person(name, address, signum));
	}
	
}

void newPerson(PersonList &pl) {
	string s, signum;
	char a[30];
	Name *name = new Name();
	Address *address = new Address();
	//...
	pl.addPerson(*name, *address, signum);
}




You can even incorporated newPerson into your class. Maybe addPerson with no parameters?

Hope the helps.
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6110
  • View blog
  • Posts: 23,670
  • Joined: 23-August 08

Re: delete pointer questions

Posted 30 October 2009 - 08:41 AM

You can't delete the person when exiting the function, otherwise the object pointed to by p[n] will be removed.
Was This Post Helpful? 0
  • +
  • -

#6 sparkart  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 113
  • View blog
  • Posts: 691
  • Joined: 16-February 09

Re: delete pointer questions

Posted 30 October 2009 - 09:09 AM

Quote

If so, when should they be deleted since I'm exiting the function.

They should be deleted when you no longer need them but before the end of your program. You don't even need to delete them, just let the operating system do it for you, but then again, that's bad practice.
Was This Post Helpful? 0
  • +
  • -

#7 masar77  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 28-May 09

Re: delete pointer questions

Posted 30 October 2009 - 09:33 AM

Thank you all for your replies!

I will go with baavgai's solution.

But, of interest, how would one delete those pointers? Or is it just bad practice creating them like that in a function, and putting them in an array?

Thanks. :^:
Was This Post Helpful? 0
  • +
  • -

#8 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: delete pointer questions

Posted 30 October 2009 - 09:58 AM

Generally speaking, I consider raw news and deletes scattered around bad news. This is no trivial topic, and there's plenty of reading to do in the various C++ idioms and provisions for dealing with pointers.

Baavgai hints at using RAII. That's an idiom you should definitely be familiar with. There are also smart pointers. I suggest starting out by reading about both topics.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1