# GML Buffer Alignment Explanation?

Discussion in 'Programming' started by DankMemes, Oct 7, 2018.

Joined:
Sep 25, 2018
Posts:
38
2. ### TheouAegisMember

Joined:
Jul 3, 2016
Posts:
6,889
Consider a buffer 10 bytes long

0 1 2 3 4 5 6 7 8 9

If you write a value of \$FF using 2 byte alignment, the buffer would be

\$FF \$00 2 3 4 5 6 7 8 9

If you then wrote a value of \$80, it would be

\$FF \$00 \$80 \$00 4 5 6 7 8 9

So after two writes the buffer would be at byte 4, not byte 2.

3. ### DankMemesMember

Joined:
Sep 25, 2018
Posts:
38
Didn’t really get that
Where did 1 go?
Why us there an extra 0?
Why is there an extra \$

Is the 00 the two bite alignment?
Maybe im not getting what alignment means?

4. ### YellowAfterlifeᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀForum StaffModerator

Joined:
Apr 21, 2016
Posts:
2,404
Essentially, positions of values in a buffer are multipliers of alignment.
So, if alignment is 2 but you wrote a 1-byte value (like buffer_u8 or buffer_bool), GM will add another empty byte so that the next one would be aligned accordingly;
Similarly, if alignment is 4 and you wrote a 1-byte value, it'd have to add 3 more bytes to align the next one.
As you might imagine, this is a fairly exotic thing to desire and is only used if you are interfacing with something that uses alignment (e.g. executable file formats commonly have file sections aligned to multipliers of 4).

Kasra_n likes this.
5. ### NightFrostMember

Joined:
Jun 24, 2016
Posts:
1,955
Alignment is important because buffers have an internal pointer. That is, a buffer remembers where your position is, and every read and write advances that pointer by the set alignment. If you've for example used PHP its arrays also feature an internal pointer that advances on reads and writes. The behavior is advantageous because it lets you iterate across the data structure without having to loop with a counter. Consider PHP's foreach:
Code:
```// PHP code
foreach(\$my_array as \$value){
echo \$value;
}
```
This will go over every position of array my_array , get the value into value variable and display it. It works thanks to internal pointer advancing on every read the looping command performs. Sadly, in GML no other data structure features pointers besides buffers.

Joined:
Sep 25, 2018
Posts:
38

File size:
272 KB
Views:
5
7. ### Kasra_nMember

Joined:
Jun 19, 2019
Posts:
26
yello i like the way you describing, your posts are allways usefull