Can't figure out what's wrong with my delete function. If I remove the delete for the name and address it works as expected, but am I not leaking memory that way?

Structs:

struct Person
{
    char* name;
    char* address;
    int zipcode;
};

struct Node
{
	Person* per;        // Person structure from earlier problem
	Node*  prev;        // address of the Node previous to this one
	Node*  next;        // address of the Node after this one
};
struct NodeList
{
	Node* head;
	Node* tail;
	int size;
};

Init a Node:

Node * newNode = new Node;
Init(newNode);
void InitNode(Node * node)
{
	// Declare and initialize new Person
	Person * tempPerson = new Person;
	
	// prompt user for new Person
	cout << "NEW NODE" << endl;

	_flushall(); // Cant get anything other than this to work
	ClearInputStream(); //Portable input clear (but doesn't work)

	char buffer[80+1]; // Initialize Char buffer for Person Name
	cout << "Name: ";
	cin.getline(buffer, 80);
	tempPerson->name = new char[strlen(buffer)+1]; // Assign memory for new Name
	strcpy(tempPerson->name, buffer); // Update name


	_flushall();
	ClearInputStream();

	char buffer2[80+1]; // Initialize Char buffer for Person Address
	cout << "Address: ";
	cin.getline(buffer2, 80);
	tempPerson->address = new char[strlen(buffer)+1]; // Assign memory for new Address
	strcpy(tempPerson->address, buffer2); // Update address

	int zip; // Declare Int for zip code
	cout << "Zip: ";
	cin >> zip;
	tempPerson->zipcode = zip; // Update zipcode

	node->per = tempPerson; // Update Person for Node

	return;
}

Deleting a node

DeleteNode(node);
delete node;
void DeleteNode(Node * node)
{
	//Person * tempPerson = node->per;
	//Comment out the next 2 lines and it works, but leaking memory?
	delete node->per->address; // ERROR!
	delete node->per->name; // ERROR
	delete node->per; // Works

	return;
}

Well I got to see how you call DeleteNode function. Is the node parameter to the DeleteNode function is a valid pointer to a strcut node?

And in the Init function you create a new Person node and do you assign that it some other struct anywhere?

And by the way heck do you want to mix up with all C & C++ code. Your just violating the standard!

-ssharish

i googled and found this code on the web, untested:

int *pI = new int;
int *pArr = new int[10];
delete pI;
delete [] pArr;

notice their delete is delete [] variablename.

maybe this is it.

Mike

>delete [] pArr;
Thats is to delete an array of node. Not to delete just a node. Atleast from OP context.

Please stop posting C++ code on C forum.

-ssharish

if your goal is to write in c you would be using:

malloc() instead of new

and

free() instead of delete

if you want to program in c++ then he is correct maybe just use c++, don't call it c and post in the c++ forum.

If your code is mixed, well i'm not taking a stand but issues with the c++ portion of the code probably belong in the c++ forum.

Mike

Thanks a lot for the replies.

This is for a course "Foundations of C", but is a preperatory course for a C++ course. Using new/delete is definitely how it's explained in the course material. I think the idea is to make programs in valid C++, but not using OOP.

I put how I called DeleteNode in the original post, but again:

DeleteNode(node);
delete node;

I tried delete [] node->per->address; but got an error.

Edited 7 Years Ago by kevinkace: n/a

struct Person{    char* name;    char* address;    int zipcode;}; struct Node{	Person* per;        // Person structure from earlier problem	Node*  prev;        // address of the Node previous to this one	Node*  next;        // address of the Node after this one};

i'm trying to think this through.

node->per is a reference to the person object. At this point you now have a fully qualified object not a pointer? i.e. node->per.address would be appropriate, since per doesnt point to address? i cant run c++ at work so can't test.

Mike

I was using

delete node->per->address;

which didn't work.

I think it has to do with the way I allocate memory for the person struct. I'll play around with it and post back my results.

This article has been dead for over six months. Start a new discussion instead.