I recently upgraded my linux to release 11 with the newer compilers.
Code that has been running for years on many platforms now traps with a memory fault and I've tracked it down to the latest runtime library.
Imagine you have a structure:
struct test{
char id[5];
char name[4];
};
Now you do a
sprintf(test.id,"%5.5d",number);
test.name = ...;
Of course the sprintf added an terminating null beyond the 5th character, but since the next statement overwrote that null, this code is not violating memory, even though it may not be the most elegant.
With the latest g++ runtime library, I'm now getting a memory fault on the sprintf instruction.
Problems like this can be easily fixed, but when I compile the code in debug mode without optimization, the original code runs fine. I would like to be able to compile and put code (that I know is perfectly fine) into production, but these erroneous runtime failures are scary.
I have a lot of existing code and don't want to resort to going to older compilers. So does anyone know if there is a compile switch to turn off these runtime checks?
You can enforce structure packing regimes with pragmas, and thus ensure contiguous memory use in structures. For example,
#pragma pack(1)
Be aware that sometimes a header might have its own struct packing shenanigans going on, so you can ensure that your code keeps your packing regime with this sort of thing:
1 2 3
#pragma pack(push)
#include "someHeaderFile.h" // Somewhere in here is another #pragma pack(x)
#pragma pack(pop)
This of course ties your code to compilers that are happy with this non-standard pragma, and usually when a pragma is not understood, you get no warning.