• Advertisement

Archived

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

Linked List Prob

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

Alright, I'm really bad at asking for help (Lack of details normally), so bare with me here. Alright, I think I got this linked list thing down. I have a class called LLIST, and a class called NODE. Like so:
    
class NODE {
    public:
        NODE(int gX, int gY);
        ~NODE();
        
        int getX();
        int getY();
        
        void draw();

        NODE *next;
    private:
        SDL_Surface *nodeImg;
        SDL_Rect nodeRect;
        int x, y;
        
        char *msg;
};

class LLIST {
    public:
        LLIST() {
            head = NULL;
            current = NULL;
        }
        
        ~LLIST();
        
        void insert(int gX, int gY);
        void checkList();
    
      private:
        NODE *head, *current;
    
};
    
Now, in my main I do this to init my llist class, and add a few nodes for testing purposes, and my controls. Basically pushing escape closes the window, and pressing the spacebar spawns a node at the coords.
     
int main (int argc, char *argv[]) {
    bool done;
    LLIST *llist;

    /* Initialize SDL */
    /* Set 640x480 16-bits video mode */

    llist = new LLIST();

    llist->insert(10, 0);
    llist->insert(10, 10);
    llist->insert(10, 20);
    llist->insert(10, 30);
    llist->insert(10, 50);

    done = true;

    SDL_Event event;
    Uint8 *keys;

    while (done) {
      SDL_PollEvent(&event);
      keys = SDL_GetKeyState(NULL); 	
                 
      if(keys[SDLK_ESCAPE] == SDL_PRESSED) {
          done = false;
      }
      if(keys[SDLK_SPACE] == SDL_PRESSED) {
          llist->insert(10, 3);
      }       
      
      llist->checkList();
      SDL_Flip(screen);
    }

    return 0;
}
   
Alright, the insert function looks like this:
        
void LLIST::insert(int gX, int gY) {
    if(head == NULL) {
        head = new NODE(gX, gY);
        head->next = NULL;
        current = head;
    }
    else {
        current->next = new NODE(gX, gY);
        current = current->next;
        current->next = NULL;
    }
}
  
Now. My problem appears to be somewhere in the insert function, and in the NODE::NODE(...) function. See, when I call the first 5 inserts() before the while loop, they work just fine, and draw just fine. But when I press the spacebar and call the same function to do the SAME thing, it crashes. Here is what the NODE::NODE() function looks like:
  
NODE::NODE(int gX, int gY) {
    nodeImg = SDL_LoadBMP("images/test.bmp");
    
    nodeRect.x = gX;
    nodeRect.y = gY;
    
    nodeRect.w = nodeImg->w;
    nodeRect.h = nodeImg->h;
}
    
After logging this baby up the rear, it seems that the NODE::NODE(...) just isn't returning back to the insert() function after new is called. Why? It worked JUST fine for the first few, but when I call it with a keypress, it fails to return. There is no error or anything given by anything. It just closes. BTW, if anyone knows the tag I need to put the things in the source boxes, that would be helpful to me to help you. -AfroFire [edited by - AfroFire on June 28, 2002 7:59:01 PM]

Share this post


Link to post
Share on other sites
Advertisement
Q: Are you forced to use your own list class, or would you consider a switch to the standard C++ std::list class ?

The tags are [‍code] or <tt> for inline code, and [‍source] for code boxes.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

Share this post


Link to post
Share on other sites
Firstly, I believe you can find out how to use the formatted code boxes in the Forums FAQ.

Secondly, try running in Windowed mode rather than Fullscreen mode. Some errors will just bomb out in Fullscreen mode because DirectDraw can''t handle showing the error boxes.

Thirdly, if you can''t find where the problem is, step through it in the debugger. This should give you more details. If that is not possible, add lots of cerr/cout calls throughout the program to find out where it crashes (SDL logs those calls to stderr.txt and stdout.txt). That should narrow down the problem.

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

Share this post


Link to post
Share on other sites
quote:

Q: Are you forced to use your own list class, or would you consider a switch to the standard C++ std::list class ?


No, I guess not. This was for learning purposes. A link on the std::list would be helpful. I did try to use STL at one point, always get errors with that, even when I copied it directly out of the book. If you don''t have a link off hand, don''t bother, I can look it up, and save you the hassle.

And thanks for the tags.

quote:

Firstly, I believe you can find out how to use the formatted code boxes in the Forums FAQ.



Actually, looking back a second time, I skipped RIGHT over it. Ack, sorry about this.

quote:

Secondly, try running in Windowed mode rather than Fullscreen mode. Some errors will just bomb out in Fullscreen mode because DirectDraw can''t handle showing the error boxes.



It is in windowed mode. I do most of my test code in windowed mode.

quote:

Thirdly, if you can''t find where the problem is, step through it in the debugger. This should give you more details. If that is not possible, add lots of cerr/cout calls throughout the program to find out where it crashes (SDL logs those calls to stderr.txt and stdout.txt). That should narrow down the problem.



As I said, I logged everything, and found the problem to be when I create a new node at runtime.

-AfroFire

Share this post


Link to post
Share on other sites
Don''t think you''ve got enough code posted there. Could you post the full source that involves the linked list and your usage? Right now you''re at very least missing the checkList function.

Share this post


Link to post
Share on other sites
I posted all the code that has to do with the actual problem. The program never even gets to checkList.



-AfroFire

Share this post


Link to post
Share on other sites
quote:

Thirdly, if you can''t find where the problem is, step through it in the debugger. This should give you more details. If that is not possible, add lots of cerr/cout calls throughout the program to find out where it crashes (SDL logs those calls to stderr.txt and stdout.txt). That should narrow down the problem.

------

As I said, I logged everything, and found the problem to be when I create a new node at runtime.

-AfroFire


Like he said, run it through the debugger (F5 in MSVC), it''s much better than just logging. You''ll (probably) be able to identify specifically what the problem is, not just where it happens.

Share this post


Link to post
Share on other sites
I ran it through GDB, giving me the same information I already knew. It just points to the fact that current->next = new NODE(gX, gY) isn''t returning after it''s done constructing.



-AfroFire

Share this post


Link to post
Share on other sites
Looks fine to me, my suggestion would be to comment out the SDL_loadimage function, just keep to the bare essentals. Have you checked that memory is availible for the insert, its possible that other code is eating it all up. Also don't rule out the possibility that checklist is causing an error, comment it out as well.

,Jay

Edit: Strong not strikeout!


[edited by - Jason Zelos on June 29, 2002 11:40:50 AM]

Share this post


Link to post
Share on other sites

  • Advertisement