Cant figure out why my variable is getting clobbered

Started by
10 comments, last by M2tM 18 years ago
I have a char pointer that get filled with a string and I also have a stl string that gets filled with some data. When I assign the stl string its data the char* suddenly becomes garbage and I can figure out why. Here is a small snippet where the problem occurs

	char *title = NULL;
	char *tm = NULL;
	title = idxgetTitle(temp); // copy the html file title in to the title variable...THIS WORKS WITHOUT A PROBLEM

	if(title != NULL)
	{
		//title = idxcheckTitle(title);

		std::string pTitle = "";   <----- title becomes garbage here
		pTitle = "	<LI> <OBJECT type=\"text/sitemap\">\n";



Can anyone point out my problem?? Jon
www.lefthandinteractive.net
Advertisement
Quote:Original post by jon723
title = idxgetTitle(temp);


is this supposed to be assignation?
Thats just a utility function I have that checks the string for some unwanted characters (it returns itself anyway). I commented that out while I was debugging since it isnt the cause of the problem.
www.lefthandinteractive.net
Quote:Original post by jon723
title = idxgetTitle(temp); // copy the html file title in to the title variable...THIS WORKS WITHOUT A PROBLEM



That doesn't copy anything; it makes the title pointer point to something returned by the function idxgetTitle(). The cause of the problem is extremely probably within that function. If, for example, you return a pointer to a local array (which is wrong, and EVIL), the memory it uses will be freed after the function returns, and will subsequently be overwritten by the new pTitle variable.
ok thanks
www.lefthandinteractive.net
char* idxGetTitle (var temp) {   char* tempArr = (char*)malloc(temp);   return tempArr; //that way it doesn't turn garbage}

Beginner in Game Development?  Read here. And read here.

 

But now you'll have to remember to free it... not good. Besides, even if you do that, you should use new[]/delete[]. Why not just return a std::string and stop worrying about unimportant low-level stuff?
Quote:Original post by Sharlin
But now you'll have to remember to free it... not good. Besides, even if you do that, you should use new[]/delete[]. Why not just return a std::string and stop worrying about unimportant low-level stuff?

fine, be all C++ guru like. see if i care [razz]

Beginner in Game Development?  Read here. And read here.

 

actually, I modified the function to take a second argument which is a pointer to that char which gets allocated and filled inside the function. The array that gets passed to the functions gets a copy of what it needs and everything in the world is happy.
www.lefthandinteractive.net
But now you have an ownership problem - whose responsibility is freeing what's been allocated within the function?

I second using std::string

If you absolutely must use char* I would pass a preallocated buffer into the function rather than allocating the memory inside the function

This topic is closed to new replies.

Advertisement