When to DELETE using Structures

When to delete when using structures

Page 1 of 1

9 Replies - 2032 Views - Last Post: 04 March 2010 - 10:38 PM Rate Topic: -----

#1 khizaraq   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 103
  • Joined: 06-November 09

When to DELETE using Structures

Posted 04 March 2010 - 09:47 PM

Hey guys here are my structures

struct additionalInfo
{
	char homeAddress[50];
	char workAddress[50];
	char middleName[30];
	char eMail[50];
	char dateOfBirth[15];
	char height[10];
	char education[5];
};

struct requiredInfo
{
	int contactID;
	char firstName[30];
	char lastName[30];
	char landLine[20];
	char cellNumber[20];
};

struct contactInfo
{
	requiredInfo *req;
	additionalInfo *add;
};




Here is my variable
contactInfo *info = NULL;


Now here are my new statements

	info = new contactInfo[totalContacts];

	for (int i = 0; i < totalContacts; i ++)
		info[i].add = new additionalInfo;

	for (int i = 0 ; i < totalContacts; i ++)
		info[i].req = new requiredInfo;

	return info;




And here is my delete statement number 1

delete [] info



And here is delete statement number 2
for (int i = 0; i < totalContacts; i++)
{
	delete info[i].add;
	delete info[i].req;
}

delete [] info



Now the question is according to how I used my new statements which delete statement I should use. Should using the first one that is delete [] info would it also delete all the new data I have allocated for add and req.

Thanks for the help.

This post has been edited by khizaraq: 04 March 2010 - 09:49 PM


Is This A Good Question/Topic? 0
  • +

Replies To: When to DELETE using Structures

#2 Martyn.Rae   User is offline

  • The programming dinosaur
  • member icon

Reputation: 548
  • View blog
  • Posts: 1,420
  • Joined: 22-August 09

Re: When to DELETE using Structures

Posted 04 March 2010 - 09:50 PM

Simple. If you have said new[] then delete []. If you have said new object then delete object.
Was This Post Helpful? 0
  • +
  • -

#3 khizaraq   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 103
  • Joined: 06-November 09

Re: When to DELETE using Structures

Posted 04 March 2010 - 09:55 PM

I didn't get you. Which statement should I use? I mean that I know that which syntax to use for deleting arrays and which for variables. But I am saying look at my structures and I have delcared pointers within structures and allocated them new space. Now look at my statements one and two and please tell me which one is correct according to my allocation of new space

This post has been edited by khizaraq: 04 March 2010 - 09:57 PM

Was This Post Helpful? 0
  • +
  • -

#4 Martyn.Rae   User is offline

  • The programming dinosaur
  • member icon

Reputation: 548
  • View blog
  • Posts: 1,420
  • Joined: 22-August 09

Re: When to DELETE using Structures

Posted 04 March 2010 - 10:01 PM

View Postkhizaraq, on 05 March 2010 - 03:55 AM, said:

I didn't get you. Which statement should I use? I mean that I know that which syntax to use for deleting arrays and which for variables. But I am saying look at my structures and I have delcared pointers within structures and allocated them new space. Now look at my statements one and two and please tell me which one is correct according to my allocation of new space


Ah gotcha. Delete all pointers held within an object before you delete the object. So on creation, create object, create sub-object 1 held by object. On deletion delete sub-object 1 and then delete object. The answer is delete statement number 2.
Was This Post Helpful? 1
  • +
  • -

#5 khizaraq   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 103
  • Joined: 06-November 09

Re: When to DELETE using Structures

Posted 04 March 2010 - 10:04 PM

Oh awesome. And one more question if you can answer.

Say I have info as it is declared above and there is some data in it now I use a function like

contactInfo *sort(contactInfo *info, int totalContacts, char firstOrLast);

And use the statemetn

info = sort(info, totalContacts, firstOrLast);

So would it replace everything at the same memory location, or do I need to delete whatever is in the OLD INFO manually before overwriting it with new data?

This post has been edited by khizaraq: 04 March 2010 - 10:06 PM

Was This Post Helpful? 0
  • +
  • -

#6 khizaraq   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 103
  • Joined: 06-November 09

Re: When to DELETE using Structures

Posted 04 March 2010 - 10:09 PM

Oh oh another question. If I use say delcare a new pointers

contactInfo *temp = NULL

And say

temp = new contactInfo[totalContacts];

And then

for (int i = 0; i < totalContacts; i ++)
temp[i] = info[i];

Then when I Am deleting temp what do I do?

delete [] temp; Will that work

Because I did not manually allocate any space to subobjects
Was This Post Helpful? 0
  • +
  • -

#7 Martyn.Rae   User is offline

  • The programming dinosaur
  • member icon

Reputation: 548
  • View blog
  • Posts: 1,420
  • Joined: 22-August 09

Re: When to DELETE using Structures

Posted 04 March 2010 - 10:22 PM

Addressing your first question. Sorts normally involve swapping data about so if you are swapping data element 1 with data element 8, then you need to use a temporary item. In this way, you are doing what is known as an in-situ sort. That is fine.

For the second question, you could delete temp[i], as temp[i] and info [i] are pointers to the same object. However, this is bad programming practice, as people who maintain your code (and it might be you in a few years time), will expect info[i] to be deleted and not a temporary pointer.
Was This Post Helpful? 0
  • +
  • -

#8 khizaraq   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 103
  • Joined: 06-November 09

Re: When to DELETE using Structures

Posted 04 March 2010 - 10:25 PM

View PostMartyn.Rae, on 04 March 2010 - 09:22 PM, said:

Addressing your first question. Sorts normally involve swapping data about so if you are swapping data element 1 with data element 8, then you need to use a temporary item. In this way, you are doing what is known as an in-situ sort. That is fine.

For the second question, you could delete temp[i], as temp[i] and info [i] are pointers to the same object. However, this is bad programming practice, as people who maintain your code (and it might be you in a few years time), will expect info[i] to be deleted and not a temporary pointer.


Yes in first question I am just swapping and then returning the pointer. Ok good to know thats fine.

And I did not understand the second one as to what you exactly mean. I would be deleting like delete [] temp; Not delete temp[i]. But I am saying the subobjects like the pointers temp[i].add or temp[i].req I did not manually allocate them any memory so is it necessary for me to delete them too?

And before the return the pointer I delete the original instance of info using the statement 2 in my first post and then create a new info with the new number of contacts and then return that
Was This Post Helpful? 0
  • +
  • -

#9 Martyn.Rae   User is offline

  • The programming dinosaur
  • member icon

Reputation: 548
  • View blog
  • Posts: 1,420
  • Joined: 22-August 09

Re: When to DELETE using Structures

Posted 04 March 2010 - 10:38 PM

What I was saying in the second answer is yes, delete temp.
Was This Post Helpful? 1
  • +
  • -

#10 khizaraq   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 103
  • Joined: 06-November 09

Re: When to DELETE using Structures

Posted 04 March 2010 - 10:38 PM

oh ok :). Thanks a lot man really really grateful.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1