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";
Cant figure out why my variable is getting clobbered
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
Can anyone point out my problem??
Jon
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.
Quote:Original post by jon723title = 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.
char* idxGetTitle (var temp) { char* tempArr = (char*)malloc(temp); return tempArr; //that way it doesn't turn garbage}
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]
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.
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
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
Popular Topics
Advertisement