Jump to content
  • Advertisement
Sign in to follow this  
jon723

Cant figure out why my variable is getting clobbered

This topic is 4518 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by jon723
title = idxgetTitle(temp);


is this supposed to be assignation?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!