Archived

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

list is not being created

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


  
	while(current_highlighted_square) {
		current_highlighted_square = current_highlighted_square->next;
	}

	// if a rectangle has been created, current and deltas will be different

	if((cCurrentX != deltaCurrentX) || (cCurrentY != deltaCurrentY) || (cCurrentZ != deltaCurrentZ)) {
		tempx1 = cCurrentX;
		tempx2 = deltaCurrentX;
		tempy1 = cCurrentY;
		tempy2 = deltaCurrentY;
		tempz1 = cCurrentZ;
		tempz2 = deltaCurrentZ;
		if(tempx1 < tempx2) {
			temporary = tempx2;
			tempx2 = tempx1;
			tempx1 = temporary;
		}
		if(tempy1 < tempy2) {
			temporary = tempy2;
			tempy2 = tempy1;
			tempy1 = temporary;
		}
		if(tempz1 < tempz2) {
			temporary = tempz2;
			tempz2 = tempz1;
			tempz1 = temporary;
		}
		tempx3 = tempx2;
		tempy3 = tempy2;
		for( ; tempz2 <= tempz1; ++tempz2) {
			for( ; tempy2 <= tempy1; ++tempy2) {
				for( ; tempx2 <= tempx1; ++tempx2) {
					current_highlighted_square = (struct tag_highlighted*)malloc(sizeof(struct tag_highlighted));
					if(current_highlighted_square) {
						current_highlighted_square->x = tempx2;
						current_highlighted_square->y = tempy2;
						current_highlighted_square->z = tempz2;
						if(persistent == 0) current_highlighted_square->stay = 0;
						if(persistent == 1) current_highlighted_square->stay = 1;
						current_highlighted_square->next = NULL;
						current_highlighted_square = current_highlighted_square->next;
					} else cout << "malloc of new squares didn''t work\n";
				}
				tempx2 = tempx3;
			}
			tempy2 = tempy3;
		}
	}

  
The loop works correctly, but at the end of it, there''s only one entry in the list and the error message about malloc failing has never gotten called.

Share this post


Link to post
Share on other sites
I think these 2 lines of yours are backwards:


  
current_highlighted_square->next = NULL;
current_highlighted_square = current_highlighted_square->next;


You''re setting current_highlighted_square to NULL in that 2nd line. You''ve also just lost the memory you just allocated.

/*=========================================*/
/* Chem0sh */
/* Lead Software Engineer & Tech Support */
/* http://www.eFaces.biz */
/*=========================================*/

Share this post


Link to post
Share on other sites
Chem0sh:
No, it''s right, the memory is allocated, the structure variables are filled in(including next), then the pointer is advanced, and the loop iterates again, and more memory is allocated for a new node. By the end of this code, current will be NULL but before I check the list I set it back to the head of the list, which never changes.

Share this post


Link to post
Share on other sites
I know we''re not supposed to shout, but I feel justified,
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

I hate it when I''m an idiot. Please feel free to call me such as well, especially considering that even within this very same project I have linked list code that works perfectly.

Chem0sh: sorry, when you said reversed, I looked at it like the other kind of reversed, as in reversing the order the lines were being executed, not the assignments, and what is truly amazing is how I don''t have a pointer to the structure that is called something like new_node, which I have in all my other linked list code. That should have been really really really really really really obvious.

Good thing I don''t do this for a living.

---------------------------
I have a new sig
---------------------------
Conservative/Republican: Display the guns proudly, hide the erotica for fear of scandal.

Liberal/Democrat: Display the erotica, hide the gun for fear of scandal.

Vote Libertarian.

Share this post


Link to post
Share on other sites
That''s ok. I once searched 3 days for why one of my linked lists wasn''t initializing properly. After massive searching, the culprit was a stray ; at the end of an if statement. Sometimes it just takes a fresh pair of eyes to catch this kind of thing.

Share this post


Link to post
Share on other sites