0

My program keeps crashing when the retrieve function calls the getName function. I'd like to make sure my getName function is correct before tinkering with my retrieve function. I'd appreciate any ideas. Is there a problem with deleting the name if it's a pointer?

header file:

void getName(char * name)const;

.cpp file:

void data::getName (char * name)const
{
    if(name)
    delete[]name;
    name = new char[strlen(name)+1];
	strcpy(name, this->name);
}
1
Contributor
2
Replies
3
Views
9 Years
Discussion Span
Last Post by sbenware
0

Rewrote the getName() function as:

strcpy(name, this->name);

and I was able to move on. :) Yes!!

0

Well, making the getName function just have the strcpy line got all of my outputs and traversals to work, but now my program crashes when I call the getName function from my retrieve function.

header file:

bool recurRetrieve(const char *key,data& aData,int index)const;
bool retrieve(const char *key, data& aData) const;

.cpp file:

bool BST::recurRetrieve(const char *key,data& aData,int index)const
{
	if(items[index].empty)
	{
		return false;
			
	}
	items[index].data.getName(temp);
	if (strcmp(temp,key)==0)
	{
		aData = items[index].data;
		return true;
    }
	else
	{
		if(aData < items[index].data)
			recurRetrieve(key,aData,2*index+1);
		else
			recurRetrieve(key,aData,2*index+2);
	}
}

bool BST::retrieve(const char *key, data& aData) const
{
	return(recurRetrieve(key,aData,0));
	
}

Ahhh! So if the program is actually in getName when it crashes, this means that getName is the true evil, yes?

This question has already been answered. 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.