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?


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;
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


	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


Deleting a 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

7 Years
Discussion Span
Last Post by kevinkace

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!



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.



>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.



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

malloc() instead of new


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.



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:

delete node;

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

Edited 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.



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.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.