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

Rewrote the getName() function as:

strcpy(name, this->name);

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

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.