Hi,

I have somewhat of a mysterious crash on my program. As far as myself and my primitive knowledge are concerned, this litterally does not make any sense at all, but maybe you guys can make some sense out of it.

Basically, inside a loop, I modify a float variable (which is actually an array), and then right after I modify it, I print its value. For some specific value, it'll crash when trying to print it. My print line looks like this:

printf("i: %d   j: %d    value: %lf\n", i, j, value[i][j]);

There are 2 parts to my printf: the location in the array, and the value itself. When I split these two parts into 2 printf statements, it does NOT crash. In other words, when I replace the above with:

printf("i: %d   j: %d    ", i, j);
printf("value: %lf\n", value[i][j]);

there is no crash, even though the output would exactly be the same (without crashes).

To me, this doesn't even make sense. Any of you know what the problem could be? 'Cause I have no idea what the problem is, which means that I wouldn't even know how to fix it.

Any help would be greatly appreciated.

Thanks a lot.

probably memory has been corrupted before executing that line. The most common cause is buffer overruns -- writing beyond the end of a buffer or an array.

probably memory has been corrupted before executing that line. The most common cause is buffer overruns -- writing beyond the end of a buffer or an array.

Would a debugger necessarily find these overruns, or it's likely not gonna find it with a debugger? I ask because I don't have access to gcc at the moment, only Dev-C++, and Dev-C++'s debugger doesn't seem to be quite effective.

debuggers aren't very good at finding buffer overruns. One method I use to find such errors, or narrow down where they are located, is to start commenting out large blocks of code then running the program to see if the problem goes away. If the problem persists, then the problem is not in the code that was commented out, so comment out another block of code. Repeat the process until you know what breaks the program.

It could be that your code is broken. %lf is not a valid format, so who knows what's going on as it tries to make sense of it.

$ gcc -W -Wall -ansi -pedantic foo.c
foo.c: In function `main':
foo.c:5: warning: ISO C90 does not support the `%lf' printf format
$ cat foo.c
#include <stdio.h>

int main() {
    float   a;
    printf("%lf\n", a );
    return 0;
}

If, as you say, this only happens for specific values, then you should be able to paste that value into the above program, and still get a crash. If you do, then you have something really useful to report.

Because at the moment, your "one line of code" really doesn't give us much to go on. If you post small and complete programs which mis-behave, there is a good chance we can tell you where you went wrong.

Also, since Dev-C++ is GCC, then you can use the same compiler flags to check your code as well.

Would you mean that I should use %f instead of %lf? I've always used %lf, which is obviously a bad habit, since I'm guessing it's for long floats. Either way, I've always used %lf, wronfully perhaps, but never had any problem.

As for posting one line of code, this a 1000+ lines algorithm, so I didn't think I could put more lines that explained my code more thoroughly without adding a couple dozen lines.

> As for posting one line of code, this a 1000+ lines algorithm
All the more reason to then work on trying to cut away as much as you can whilst still preserving the nature of the problem.
If you can get it down to a couple of hundred lines, then I suggest you post the complete code. The collected wisdom of the site can then tell you all the things which are simply wrong with the code, or which are dubious enough to change for something better.

Quite how you expect us to extrapolate your 1000+ lines from only the one line you posted is beyond me. Do you explain your "limp" to your doctor over the phone, and expect her to diagnose your broken ankle? We need more information!

> Either way, I've always used %lf, wronfully perhaps, but never had any problem.
A lot of programmers start out with "beginners luck". All sorts of rubbish gets written which just happens to produce the expected answer, despite all the problems. But sooner or later (or in your case, now), all the bad habits conspire to actually break something for real. Once you get into the 1000's of lines of code, being lucky no longer counts, and you have to really work on being right.

I'm going to suggest that your single (so far) incorrect use of printf is far from being the only problem with your code.

> Would you mean that I should use %f instead of %lf?
Have you compiled with all the additional flags I mentioned, and fixed all the additional warnings you would no doubt get as a result?

Well, I figured that it wouldn't be fair to ask anyone to help me by posting a lot of lines of code, since it'd take time to read and understand, but I maybe I was wrong.

As for my problem itself, it was exactly as Ancient Dragon said; I was accessing beyond the bounds of a dynamic array.

Also, I always fix compiler warnings.

This article has been dead for over six months. Start a new discussion instead.