Martin Franke wrote: > Hi everybody, > > First post, so thanks a lot for reading, and, hopefully, replying! > > When turning on optimisation (gcc 3.4.2, O2/Os -fno-strict-aliasing) > when compiling our multi-platform software (C/C++ mix, target now is an > embedded MIPS based platform) it segfaults, while it runs without a > problem when turning optimisations off. I still get quite a few compiler > warnings, so it is probably best to completely resolve them first. > > Still, there is something that strikes me as odd, and I would like to > kindly ask if someone could shed some light on this. The crash occurs in > the initialisation routine of a certain module. Now, when inserting a > printf("bla") right at start-up in main() the crash goes away, removing > it makes it re-appear, so it is perfectly reproducible. The printf is in > a different object file than where the crash happens and quite a lot of > code is executed between the printf and the crash. > > So my questions are: > - Is this a side-effect to be expected? > - How can the additional printf in one object file can have a > side-effect on the code in another object file? (Each source file is > compiled and optimised separately, so I guess it must happen during > linkage, but that's where my understanding hits the wall.) > > Thanks again! > > Martin
printf segfault printf does have side-effects and C functions share the same stack. Here is a short code I wrote where removing a call to printf triggers a segfault. Also try commenting out the line. This code is implementation specific and uses undefined behaviors. Runs on gcc 4.0.2 no optimization #include <stdio.h> /* Side-effects of printf */ int main (void){ /* A dirty hack to display the stack. Implementation specific. Does work on most compilers tough */ printf("Stack looks like before printf:\n%p\n%p\n"); /* Here 2 args passed to printf because the call to sprintf below overwrites one of them on the stack. Try the code with and without this line */ printf ("%d", 0, 0); printf("Stack looks like after printf:\n%p\n%p\n"); char buffer[20]; sprintf (buffer,"%d"); int* p = -1; /* The following assignment to produce a segfault but anycode would do really */ (!atoi (buffer))?*p=0:printf ("\nRan fine\n");; return 0; } JD
_______________________________________________ help-gplusplus mailing list help-gplusplus@gnu.org http://lists.gnu.org/mailman/listinfo/help-gplusplus