Im just getting garbage values. And it is wierd the debugger shows the correct values. But its printing weird stuff insted...
this frist part is fine. Essentially, It just takes me to my problem. I have what I need to print inside that h.hashtable[hashIndex] array.
ostream& operator<<(ostream& out, const hashmap& h)
{
const char *getSymbol = NULL;
for ( int hashIndex = 0; hashIndex < maxSize; hashIndex++ )
{
getSymbol = h.hashTable[hashIndex].getSymbol();
if ( getSymbol ) // Find the one I added.
{
h.hashTable->display(out);
return out << h.hashTable[hashIndex];
}
}
return out;
}
From stackoverflow
-
Is the values at line:
getSymbol = h.hashTable[hashIndex].getSymbol();
fine, but crap afterwards?
You could be having a case where you have a const char* to something inside an anonymous variable, which gets deleted when the line is done.
: find, and fine afterwards: It is wierd. The debugger shows the correct values at the point u are describing. But its not printing what the debugger is showing.Steve314 : I think Calyth is suggesting that the pointer returned by getSymbol might point to a variable inside the getSymbol function (or something it calls) which is no longer valid when getSymbol exits. The debugger is seeing the data left behind in that memory, but a later function call (such as the display method) is reusing that memory and overwriting the previous content, so the pointer points to garbage by the time the output is done. IOW, getSymbol is a "dangling pointer". This kind of issue is very likely with dangling pointers, because of the way the call stack works.Calyth : Sorry for the inability to communicate, but Steve314's right. -
Make sure the stream is set to print in decimal
out << dec << s.m_sharePrice;
(
m_sharePrice
is a non-pointer type, right?): This is actually the answer. Its amazing how now having dec afftected my output...The major clue the debugging showing it as the correct result; meaning that it was an IO issue
0 comments:
Post a Comment