Forum > General

Padding rules in memory allocation

(1/3) > >>

jollytall:
What is the padding logic when memory is used?
I have some objects, all 24 bytes long on an X64 machine. I would have expected that the objects are placed next to each other both in stack and heap, as 24 is dividable by 8 (bytes = 64 bits), but what I see in memory is that they are placed 32 bytes from each other adding an extra 8 byte to pad.
Similarly, when I have pointers (8 bytes) right after each other, I would have thought that they are right after each other again, but what I see is that they are 16 bytes apart.
Why is that that on a 64 bit computer padding is done to 128 bits?

Thaddy:
If you want them sequentally and without the spacing, put your objects in a packed array.

jollytall:
Thanks, I know that. Packed is always a good option if I do not want padding (or is it called spacing?). My question was why the padding is done to 128 bits, when the processor is 64? I have another issue, where the actual data is 56 bytes (see a separate question), so the padding is not aligned to 128 bits. So I am confused, what is the rule without "packed".

BobDog:

The spacing looks OK here, or am I missing something?
Win 10 64 bits
fpc 3.2.2 (64 bit compiler)

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---typeudt=objectb:array[1 .. 24] of byte;end; var a:array[0 .. 10] of udt; i:int32;beginwriteln(sizeof(udt));for i:=0 to high(a) do writeln(int64(@a[i]));readln;end. 24
4295016448
4295016472
4295016496
4295016520
4295016544
4295016568
4295016592
4295016616
4295016640
4295016664
4295016688

jollytall:
Thanks. I checked your code. If it is in an array as you wrote, I get the same result as you, i.e. 24 bytes aligned.
But I checked the following code:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type  udt=object    b:array[1 .. 24] of byte;    end;var  a, a2: udt;beginwriteln(sizeof(udt));writeln(int64(@a),' ',int64(@a2));readln;end.   
Result:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---244398224 4398256The alignment is 32 bytes. It is clearly not something hidden field (size is 24) and also if I reduce the array size to 16, then there is no padding. If it is 17 bytes then the alignment jumps up to 32 bytes not to 24 bytes as I would expect.

I use Linux on x64 FPC 3.2.0

Navigation

[0] Message Index

[#] Next page

Go to full version