Thread: confusing error - please have a look at my code

  1. #1
    Registered User
    Join Date
    Jun 2003
    Location
    Austria
    Posts
    55

    confusing error - please have a look at my code

    Hi All!

    i got the problem, that my program chrashes in an endless loop in my main() func. but i have no idea why. (when the program is running press a key and it will crash - see the code where *g*)
    This are the main parts of my code (i cut a bit around - it would be too much code to post it all), so there may be some unused variables and stuff like this....

    i'm sorry for the much code - but i'm not able to reduce it more

    see the attached files

    thx IceBall
    Last edited by IceBall; 12-21-2003 at 08:34 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well the damage starts here
    lAccList->Items[0] = NULL;

    After that, its only a matter of time before it blows up

    You have
    void **Items;
    so you need to allocate Items before you start doing things with Items[0]

    Given your usage of the structure in DAList.c (more weirdness by the way, you're actually writing C++ code within a .c file), you should have
    int *Items;

    Then in main.c, you end up with
    lAccList = malloc(sizeof(DALIST));
    lAccList->Length = 0;
    lAccList->Items = NULL;


    Second, you have the classic (its in the FAQ) mis-use of feof() bug

    This is how you read each line of a text file
    while (fgets(Buffer,SIZE,f) != NULL )

    Furthermore, what is the point of allocating Buffer?
    char Buffer[SIZE];
    does just as well, and saves all the 'is it NULL' and remember to free it when you're done.

    DAList.c should include DAList.h.
    If you change the interface in either without changing the interface in the other, you'll get a compiler warning.

    Finally, one minor nit-pick
    Using spaces for indentation in place of tabs is preferred. It's the only way you'll make the code look indented properly no matter where you post it or who reads it.
    For instance, you have tabs at 8 spaces, and I have tabs at 4 spaces. Since you mixed in there some space indentation, already the code looks a mess.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Location
    Austria
    Posts
    55
    char Buffer[SIZE];
    does just as well, and saves all the 'is it NULL' and remember to free it when you're done.
    hmm yea... never thought about this


    Second, you have the classic (its in the FAQ) mis-use of feof() bug
    will check this


    you should have
    int *Items;
    huh? i want an dynamic array of pointers to a struct (or void in generall) - why should i use an int*?


    you're actually writing C++ code within a .c file
    lol - yes i know

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > huh? i want an dynamic array of pointers to a struct (or void in generall) - why should i use an int*?
    Sorry, I was confused by the sizeof

    You start with
    lAccList->Items = NULL;

    And these should be
    void **Ret = realloc(List->Items, (iItem + 1) * sizeof(*Ret) );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jun 2003
    Location
    Austria
    Posts
    55
    hmm ok, but im still wondering why the program is chrashing.
    The funny thing is: if i delete the CreateThread function call it works fine ?! - Why?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I'd try and debug the thread problem in a separate source file (or comment out the call to ReadAccFile()), because as far as I can tell, it's doing the right thing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Jun 2003
    Location
    Austria
    Posts
    55
    i actually did so, and the thread thing is working fine alone (!!) - still confused about this problems

    so:
    the thread thing is working fine alone
    the other things are working fine alone
    both together is a terrible @$%&(..

  8. #8
    Registered User
    Join Date
    Jun 2003
    Location
    Austria
    Posts
    55
    lol - another funny thing is:

    if i compile the code with lcc-win32 i get these problems i listed above
    if i compile with borland builder, no such problems occur

    another Question, i can't answer


    Ice


    PS: i'm really willing to compile it with lcc-win32

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You still have a problem then - changing compilers will just bury the problem for a short while, but it will return at some point.

    Can you repost your latest version
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Jun 2003
    Location
    Austria
    Posts
    55
    haa - it works now - the last problem was:

    i forgot to initialisize the Items-memory
    Code:
    lAccList->Items = NULL;
    now it works fine - thanks Salem, you really helped me a much! - hope i will not find more such errors *g*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM