Advertisement

Linked Lists in Linux

Started by January 23, 2001 09:32 AM
7 comments, last by p4n1c 23 years, 11 months ago
Hi, I''m having a problem with my linked lists in Linux. The program runs fine under DOS(compiled with DJGPP), but when I run it under Linux it seg faults on printing them out.

void i_print(npc_t *character) {
   inven_t *temp = NULL;

   for(temp = character->ihead; temp != NULL; temp = temp->next)
      printf("%d: %s", temp->inum, temp->iname);
}
 
thnx p4n1c -=[ p4n1c ]=- -=[ Come out swingin'' ]=-
-----For I have seen the face of evil and it will not soon be forgotten.
Looks like pretty valid code to me (though I''m tired and may be missing something). I guess something else outside this function went wrong, so you should add some ASSERT()s / whatever or learn to use one of the Linux debuggers (just don''t go for gdb, it''s horrible; there are several graphical front-ends though) to find out where the actual segfault happens - it''s most definitely a NULL pointer. Then you can follow it back until you find where it comes from.

cu,
Prefect
Widelands - laid back, free software strategy
Advertisement
Actually I do use GDB. And it''s happening in the second(for adding 3 items) itteration of the loop for printing. So I don''t think it is reading the linked list members right or nothing is getting stored in the first place, even though it does run through the add function fine for all the items added.

I''m wondering if the Linux version is picking up a memory violation that the DOS version isn''t? But I can''t see that when I''m using the same compiler for both DOS(DJGPP) and Linux.

---

-=[ p4n1c ]=-
-=[ Come out swingin'' ]=-
-----For I have seen the face of evil and it will not soon be forgotten.
I have found with experience that initializing a pointer to NULL can create problems sometimes. instead of the line "inven_t *temp = NULL", either drop off the "= NULL" part and deal with the warning gcc spits at you for not initializing, or allocate memory to the pointer. I don''t know the equivalent in C, but in C++ it would be "inven_t *temp = new inven_t;". I suppose you would be in malloc land using C. Better yet, change it to "inven_t *temp = character->ihead;" and consider using a diffent type of loop (while() maybe?).

I know this works in C++ (not sure about C), but you can leave out the *temp declaration completely, and do your for loop liek this:

for(inven_t *temp = character->ihead; temp!=NULL; temp = temp->next)

*temp will only exist within the for loop, and will be deallocated when the for loop breaks.

Now, what im saying may be completely wrong, but try stuff out and see if it works. When this happened to some of my linked list code, I ended up using my temp pointer uninitialized.

Saai
Equivalent in C for new will be malloc(value)
Eg:-
char *t;
t = malloc(100);
Hello from my world
I''m gonna go through the code again and see what I can find, one piece at a time, I don''t think ihead is getting set properly. All the data is there, I checked that. It''s a good learning experience.

----

-=[ p4n1c ]=-
-=[ Come out swingin'' ]=-
-----For I have seen the face of evil and it will not soon be forgotten.
Advertisement
Just as a side note to Saai, for what he''s doing (traversing a linked list) he absolutely does not want to allocate memory for the temp pointer. Also, initializing a pointer to NULL will never cause a problem in and of itself. Problems arise when it is initialize to NULL and, for instance, later dereferenced (without ever assigning it a valid value).

As for the code in question, it is completely valid. A few things to check would be make sure ihead points where it should (like you suggested). Another suggestion would be to examine to the inven_t objects in the list. It is possible that you are killing the next pointer by writing outside the bounds of an array, or something of that nature.
I had that feeling too, that I''m walking outside of the pointer. But it''s funny that it happens in Linux and not DOS.

----

-=[ p4n1c ]=-
-=[ Come out swingin'' ]=-
-----For I have seen the face of evil and it will not soon be forgotten.
If it is really an out-of-bounds thing it's probably because the Linux C-lib's malloc() uses different internal structures compared to the DOS libs.

Hmm.. I just realized you're compiling with DJGPP for DOS, which is based on GCC, so that seems unlikely. But then, maybe you're using different versions of libc....

cu,
Prefect

Edited by - Prefect on January 25, 2001 1:32:52 PM
Widelands - laid back, free software strategy

This topic is closed to new replies.

Advertisement