Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Matsen

References and release mode weirdness

This topic is 5850 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

Hi I'm making a template stack which uses stores its data by reference. The weird thing is that it works fine in debug mode, but in release mode, my data seems to get corrupted (or something) Here's the push code:
      
template <class T>
void Stack<T>::Push(const T &data)
{
	Node<T> *pNode = new Node<T>(data);
	pNode->next = top;
	top = pNode;
	size++;
}
   

...and the code for pop:

      
template <class T>
const T& mehs::Stack<T>::Pop()
{
	const T &data = top->data;
	Node<T> *pNode = top;
	top = top->next;

	delete pNode;

	size--;

	return data;
}
   

...and the code for the Node class

      
template <class T>
class Node
{
public:
	Node(const T &d) : data(d), next(0) {}
	virtual ~Node() {}
	const T &data;
	Node *next;
}; 
      
When I push 1, 2, 3 on the stack, like this...
  
Stack<int> s;
s.push(1);
s.push(2);
s.push(3);
  
in release mode and then pop it, the output is wrong, usually 3,3,3419312. But, when I push variables, like this...
  
int a=1, b=2, c=3;
Stack<int> s;
s.push(a);
s.push(b);
s.push(c);
  
it works fine (even in release mode). Please help! I can't find what I'm doing wrong. (I have to make a stack for my C++ course in school, so no use-the-std-stack-flame please) Regards Mats [edited by - Matsen on September 16, 2002 2:19:23 AM] [edited by - Matsen on September 16, 2002 2:20:15 AM] [edited by - Matsen on September 16, 2002 2:23:35 AM]

Share this post


Link to post
Share on other sites
Advertisement
Essentially, you're doing this...

const int &data = 1;

... Trying to store a reference to the value '1'. '1' is not a variable.



Either

A) '1' is a literal with no location in memory and the reference returned is completely invalid... or

B) '1' is given a location in memory but it's lifetime is undefined (just as bad a A)... or maybe

C) I'm completely wrong (I don't have any reference material with me now). But I'm pretty sure it's A or B.


Unless your assignment specifically asks you to store references I'd store values (prevents dangling pointers). Why this worked with a debug build I couldn't say.

[edited by - Solo on September 16, 2002 12:02:43 PM]

Share this post


Link to post
Share on other sites
Your reply makes sense. But still, it works fine in debug mode and in GCC (compiled with -Wall -pedantic). I also noticed that it worked fine in release mode when I turned optimizations to ''Default'' (I''m using VS6).

The std::stack uses references, void push(const value_type& _X), but it doesn''t suffer from my problem.(?)

Further help is appreciated!

Regards Mats

Share this post


Link to post
Share on other sites
quote:
Original post by Matsen
The std::stack uses references, void push(const value_type& _X), but it doesn''t suffer from my problem.(?)



That''s because it still makes a copy internally.

MSN

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Typically debug to release mode crashes are caused by the fact that in debug mode all your pointers are initialized to null by default, however in release mode they are not. So check your pointers.

Share this post


Link to post
Share on other sites
quote:
Original post by msn12b
That''s because it still makes a copy internally.



Thanks.. it all makes sense now, and works fine. I changed the Node class to store a copy instead of a reference.

Regards Mats

Share this post


Link to post
Share on other sites
For anyone following the thread that didn't understand:

const T &data = top->data; // 'data' is a reference to top->data
Node *pNode = top; // 'pNode' is a pointer to top
top = top->next; // move top down the stack

delete pNode; // now we delete the pNode, which is the old
// 'top' - but 'data' holds a reference to the
// old top's data! So the 'data' reference is no
// longer valid.

return data; // return it anyway, causing havoc


[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files | My stuff ]

[edited by - Kylotan on September 16, 2002 8:16:45 PM]

Share this post


Link to post
Share on other sites

  • 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!