TLDR: the compiler likes to add padding to structures so they sit on 4 byte bounds. If the compiler does this, copied data may shift by a few bytes destroying pointers.
L3nn0x wrote:This sounds a lot like a memory management issue, not an align issue.
It crashes when you try to allocate new memory so that means that you have written outside of our allocated memory.
The memory allocated with new/malloc is defined like this :
-------BEGIN BLOCK---------
Size
Owner
--------BEGIN DATA---------- <- the pointer you get is pointing here
.. your data
---------END DATA----------
--------BEGIN BLOCK-------
...
So that means that if you write outside of you allocated block then the heap will get messed up cause the OS relies on the the size written in here to know where to allocate the next bloc of memory.
So you're writting outside an alocated block somewhere in your code.
To help you try using valgrind or
https://en.wikipedia.org/wiki/Buffer_overflow_protection maybe ?
He should not use valgrind if hes on windows. Using gflags is the way to go because its built directly into the OS. I said alignment issues because I've seen where if you copy a data structure that is misaligned, the compiler will try to copy more bytes then you actually have aloc'd.
For example:
-
- struct badAlign {
- char data1;
- int data2;
- short data3;
- int data4;
- long long data5;
- }
The structure above isn't aligned to a 4 byte boundary. so it may or may not put padding into the memory to align it. So the following may happen:
-
- struct badAlign {
- char data1;
- char pad[3];
- int data2;
- short data3;
- char pad[2];
- int data4;
- long long data5;
- }
When it goes to copy this data, the size of the struct is now 5 bytes bigger then you expect it to be. I've seen cases where since it does this behavior, when copying data the data will get shifted by a few bytes every time a copy is invoked.
To align this structure better you would need to do this:
-
- struct goodAlign {
- int data2; // 4 bytes
- int data4; // 4 bytes
- long long data5; // 8 bytes
- short data3; // 2 bytes
- char data1; // 1 byte
- }
The compiler will still fix this and go:
-
- struct goodAlign {
- int data2; // 4 bytes
- int data4; // 4 bytes
- long long data5; // 8 bytes
- short data3; // 2 bytes
- char data1; // 1 byte
- char pad1;
- }
in this case the compiler only added one byte to the size of the structure.