Skip to main content
gamedev.net
🔒 Locked

4-byte aligned memory

Started by Kurioes 6 replies 400+ views
Original Post
Kurioes
Kurioes
Just a quick question about aligned memory blocks... Is it possible to just do
#define align4(x) ((x & 0x03) ? (x + (4 - (x & 0x03))) : (x))
   
on a pointer (if fed an int, the int will be divisible by four, incremented by 1, 2 or three to achieve this, I hope) so it is aligned? Of course, assuming you allocate an additional three bytes of memory. I use Windows (in case Win has some strange way of protecting memory). Edit: very bad typo [edited by - Kurioes on December 14, 2002 5:16:59 PM]
Stary
Stary
quote:
Original post by Kurioes
#define align4(x) ((x & 0x03) ? (ptr + (4 - (x & 0x03))) : (x))


1. What''s ptr? For the rest of this, i assume ptr is supposed to be x.
2. What are you planning to use it on?

short *x;
// x = something.

x = align4(x);


expands to x = ((x & 0x03) ? (x + (4 - (x & 0x03))) : (x);

Now let''s say sizeof(short) == 2 and that x is 2-aligned but not 4-aligned. x & 0x03 gives 2. so x = x + 4 - 2 (which in turn is x + 2). What happens when you add 2 to a pointer to something with size 2?

Right, you end up with a short pointer that''s still not 4-aligned, but now it''s 4 bytes ahead in memory. And who knows what''s there?

If you''re planning to use it on things where you allocate up to 4 bytes too much to align the pointer you get, a modified version of this would be a hack that works, otherwise you''d just end up wherever in memory.

Summarized, be careful with this. You''d probably be better off finding out what compiler-specific macros your compiler has to align data.
Kurioes
Kurioes
quote:

1. What's ptr? For the rest of this, i assume ptr is supposed to be x.


Yes... typo

quote:

2. What are you planning to use it on?

short *x;
// x = something.

x = align4(x);


expands to x = ((x & 0x03) ? (x + (4 - (x & 0x03))) : (x);

Now let's say sizeof(short) == 2 and that x is 2-aligned but not 4-aligned. x & 0x03 gives 2. so x = x + 4 - 2 (which in turn is x + 2). What happens when you add 2 to a pointer to something with size 2?


You make a copy of the pointer (or else you couldn't free the memory). You align the pointer after you allocate it (you allocate memory of course but you know what I mean). Thus, nothin will happen. The two most insignificant bytes will (should) be zero.

quote:

Right, you end up with a short pointer that's still not 4-aligned, but now it's 4 bytes ahead in memory. And who knows what's there?


It's actually 0, 1, 2 or 3 bytes ahead in memory, which you should've allocated extra. No harm there.

quote:

If you're planning to use it on things where you allocate up to 4 bytes too much to align the pointer you get, a modified version of this would be a hack that works, otherwise you'd just end up wherever in memory.

Summarized, be careful with this. You'd probably be better off finding out what compiler-specific macros your compiler has to align data.


Might be a good idea. Yann_L told me you don't have to worry about it because all memory you allocate in modern os'es is aligned (4-bytes). I posted dtat question in the OpenGL forum but this one is more relevant.. my apologies for that.


Edit: I screwed up the quote tags

[edited by - Kurioes on December 14, 2002 5:16:38 PM]

[edited by - Kurioes on December 14, 2002 5:18:15 PM]
Brother Bob
Brother Bob
Consider this code.


short *foo = (short *)6;
foo = foo + 2;


What address is foo now pointing to? If you answer 8, then you're wrong. The correct answer is 10 (assuming sizeof(short) is 2). Your macro adds 1, 2 or 3 to the pointer, depending on the two least significant bits. But adding 1, 2 or 3 to a pointer is not the same as moving it 1, 2 or 3 bytes ahead.

[edited by - Brother Bob on December 14, 2002 6:34:36 PM]
Kurioes
Kurioes
Hmmm... so it''s allright when the pointer type is void* or char*? (or sizeof(ptr)==1)

(messy)

#define align4(x) ((x & 0x03) ? ((char*)x + (4 - x & 0x03))) : (x))
Zipster
Zipster
No, not void*. To sum things up, whenever you add a number - let''s say n - to a pointer, it doesn''t add n bytes, but n * sizeof(type) bytes. void doesn''t have a size, so you''d probably get a compiler error if you tried to add to a void pointer.
Brother Bob
Brother Bob
This is what MSVC 6 says when I try to increament a void *.

error C2036: ''void *'' : unknown size

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.