Jump to content
  • Advertisement

Archived

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

InsaneX

Assignment Question

This topic is 5371 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 was wondering if there could ever be anything wrong with doing something like this
for(int x = 0; x < count; x++)
	{
		Current->Next = new SDataEntry;
		Current = Current->Next;
		TotalNodes++;
	}
My question is about the assignment Current to equal Current->Next. Could that cause a problem?

Share this post


Link to post
Share on other sites
Advertisement
No problems with the assignment, no.

Using std::list is generally preferable to rolling your own linked-list.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan (C programming language co-inventor)

Share this post


Link to post
Share on other sites
Remember that the order of operator precedence is very well defined in C++. Here''s a list if you don''t have one handy in a book or something.

So what happens with your statement "Current = Current->Next; " is that all your operators are considered. That line only has two: = and -> . If you check the chart, -> is way way higher than = , thus it executes first. So Current->Next is evaluated, and is effectively replaced by its result, which is the value of the pointer Next . Then, all the operators are considered again. This time there is only one left, = , so it is evaluated. That which is on the left becomes equal to that on the right (which is currently the pointer value from Next ), and then it is replaced by that value. Now there are no operators, and just a simple value, so the evaluation of the entire line is done.

Hopefully stepping through the process of what occurs might help understand why that is perfectly valid, and doesn''t lead to any wierd effects. So always remember (assuming nothing fancy likes threads are involved) that lines are executed one single operation at a time, and the order in which those operations are executed are almost always very well defined. Most of the time, an order-of-precedence table will be all you need to figure out how a line is evaluated.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
The two problems I see are the series of dangling pointers and the absolutely horrible indentation of brackets.

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!