Hi,
I'm porting code from Windows to HP-UX 11.23, using gcc (I'm begining to suspect this might not be the best choice, but I thought it would be most compatible since we've already compiled on Linux with gcc).
I have in my code something similar to the folowing:
The last line gives a segmentation fault.
In fact, I have found that its enough to write the following in order to crash:
And the same is true for any index in the array which does not divide by 4.
I assume this is an alignment problem. If I were writing new code I could work around it, but since I am porting code this is a problem. Apparently the code uses this type of code, treating char arrays as memory buffers for different types, in many places and it would be very difficult to find them all and fix them.
Is there any compilation flag that could change the alignment for me?
Any other ideas for a global fix for this?
Thanks!
RO
I'm porting code from Windows to HP-UX 11.23, using gcc (I'm begining to suspect this might not be the best choice, but I thought it would be most compatible since we've already compiled on Linux with gcc).
I have in my code something similar to the folowing:
Code:
unsigned char *ch_arr = new (unsigned char)[20];
unsigned char ptr = ch_arr + 10;
unsigned int number = 100;
*((unsigned int *)ptr - 1) = number;
The last line gives a segmentation fault.
In fact, I have found that its enough to write the following in order to crash:
Code:
unsigned char *ch_arr = new (unsigned char)[20];
unsigned int number = 100;
ch_arr[2] = number;
And the same is true for any index in the array which does not divide by 4.
I assume this is an alignment problem. If I were writing new code I could work around it, but since I am porting code this is a problem. Apparently the code uses this type of code, treating char arrays as memory buffers for different types, in many places and it would be very difficult to find them all and fix them.
Is there any compilation flag that could change the alignment for me?
Any other ideas for a global fix for this?
Thanks!
RO