for loop bombing - very strange

This is a discussion on for loop bombing - very strange within the C Programming forums, part of the General Programming Boards category; I am coding using a network simulator (OPNET). For some strange reason my for loop is causing the simulator to ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    29

    for loop bombing - very strange

    I am coding using a network simulator (OPNET).

    For some strange reason my for loop is causing the simulator to bomb (no error message which is why I can't solve the problem).

    I have tracked it to the for loop though (through tedious hours of commenting out different lines of code).

    Basically if I have the following, it runs perfectly.

    Code:
    for (y=0; y<nodeCount; y++)
    {
         break;
    }
    However if I let the loop run to completion (even with no code in it) it just iterates until the end of the loop (14 times) and then bombs.
    Also if I set nodeCount to 2 so that it loops twice it also bombs. So basically if it acts as a loop should and loops more than once the simulator crashes with no error message. I have initialised y and nodeCount to 0 when declaring the variables.

    Any idea?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,893
    The problem probably lies elsewhere, likely undefined behaviour somewhere earlier in the code, but was manifested at this point.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,433
    The problem is not in the loop.

    The probable cause is some other apparently unrelated code, executed before the loop, that molests a pointer. Examples might be dereferencing a NULL pointer, dereferencing an uninitialised pointer, or falling off the end of an array (eg accessing the 10th element of an array with 5 elements).

    Having said that, your response will probably be "Huh? How can code unrelated to a loop affect a loop?" The thing is, invalid operations are allowed to do anything, including overwriting random areas of memory, and the effects are not always immediately visible to the program, let alone to a user of the program. If that random area of overwritten memory affects the value of (say, in your code) nodeCount, then the behaviour of the loop is compromised. Or behaviour of your network simulator is compromised.

    Note that pointer molestations are not the only way code can exhibit undefined behaviour. For C programmers, however, they are the most common ways.
    Right 98% of the time, and don't care about the other 3%.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    29
    Hi laserlight, grumpy

    Thanks for the replies - I was beginning to suspect as much. The problem now is finding the needle in the haystack ....guess I can write off a couple of hours

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Have you already tried a tool like valgrind which helps to find memory management bugs?

  6. #6
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Or you could just do what 99% of other people do and run the damn thing in a debugger. That will show you not only the exact line it bombs on, but the values of all variables at that point too, and let you poke the code while its still running.

    Seriously, don't go stabbing randomly looking for needles in your haystacks, roll back time and watch the needle being inserted. Despite my "printf" comment in my signature, which works for casual debugging of most simple problems, if you're stumped you should always single-step the program and see where it ACTUALLY crashes and why.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    When you have a serious error in your program, it doesn't mean that it will stop running immediately, with C. That's what you have here - a serious error that has already compromised the program, but not caused it to crash yet.

    There's nothing wrong with this loop code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 05-01-2010, 06:08 AM
  2. Need help with a strange for-loop problem
    By jasperleeabc in forum C++ Programming
    Replies: 4
    Last Post: 05-29-2009, 08:47 AM
  3. Program bombing out
    By Necrofear in forum C++ Programming
    Replies: 2
    Last Post: 11-04-2006, 07:08 PM
  4. Strange loop
    By D@rk_force in forum C++ Programming
    Replies: 22
    Last Post: 12-18-2004, 01:40 PM
  5. strange strange functions
    By threahdead in forum C Programming
    Replies: 4
    Last Post: 10-13-2002, 05:31 PM

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