Linked list logic error

This is a discussion on Linked list logic error within the C++ Programming forums, part of the General Programming Boards category; Hi, their is part of a homework problem which I seem to be stuck on. It asks what are the ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    16

    Linked list logic error

    Hi, their is part of a homework problem which I seem to be stuck on. It asks what are the logic errors in a program if the function defined is supposed to return the legnth of a linked list.

    The function is:
    Code:
    int length(NodePtr *Head){
    int size = 0;
    NodePtr *cur;
    while(cur != NULL){
    size++;
    cur = cur->next;
    }
    }
    I am having trouble seeing what is wrong with it...It seems as it is set to increment the int variable size by one while the pointed variable cur is not null. cur is then set to be the next value. This would continue until cur had nothing left thus making it null and stopping the while loop.

    Help please!

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    >> NodePtr *cur;

    hint: what does this point to?
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    16
    to the node counter?

    ..nvm, let me think some more before answering.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    regardless of the data type, a variable should be initialized to a valid state before working with it. consider:

    Code:
    int
    main(void)
    {
    	int i;
    	cout << i << endl;
    }
    what do you think the program will print?
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    16
    whatever random value the os has stored in that memory space for i at that time. Other compiler would just give an error.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    right.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    16
    And this is the exact question if I asked it wrong:
    Code:
    The following function is supposed to return the length of a linked list. What are the logical errors in
    this function?
    int length(NodePtr *Head){
    int size = 0;
    NodePtr *cur;
    while(cur != NULL){
    size++;
    cur = cur->next;
    }
    }

  8. #8
    Registered User
    Join Date
    Feb 2006
    Posts
    16
    Oh, so you are saying since *cur is pointing to nothing, it cant work?

  9. #9
    Registered User
    Join Date
    Feb 2006
    Posts
    16
    I believe it assumes that elsewhere NodePtr and Head, etc are defined.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    It's an uninitialized pointer. It doesn't matter if those things are defined elsewhere, cur doesn't point to anything when it is first used.

    >> Other compiler would just give an error.
    No, it is not a compile error, it is a logical error. All compilers should probably give a warning, though.

  11. #11
    Registered User
    Join Date
    Feb 2006
    Posts
    16
    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 03:35 PM
  2. Getting other processes class names
    By Hawkin in forum Windows Programming
    Replies: 3
    Last Post: 03-20-2008, 04:02 PM
  3. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  4. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  5. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 04:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21