468,257 Members | 1,420 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,257 developers. It's quick & easy.

Address on x- byte boundary

Hi all,

I was wondering how does address alignment to x byte boundary is done.
For example, if I say "adjust the requested size to be on a 4-byte
boundary" or for that matter 8 byte boundary. How is this
adjustment/alignment done?

I goolged around alot and wans't able to find how is it done, all it
said was what is byte alignment and byte padding.

Also verifying that it is aligned to 4 byte boundary is easy just check
for last 2 bits to be 0. But how is a size of say 3 bytes is aligned on
a 4 byte boundary still is a mystery.

Any pointers or info would be appreciated.

Thanx in advance.

Regards,
Taran

Nov 14 '05 #1
11 3461


Taran wrote:
Hi all,

I was wondering how does address alignment to x byte boundary is done.
For example, if I say "adjust the requested size to be on a 4-byte
boundary" or for that matter 8 byte boundary. How is this
adjustment/alignment done?

I goolged around alot and wans't able to find how is it done, all it
said was what is byte alignment and byte padding.

Also verifying that it is aligned to 4 byte boundary is easy just check
for last 2 bits to be 0. But how is a size of say 3 bytes is aligned on
a 4 byte boundary still is a mystery.

Any pointers or info would be appreciated.

Thanx in advance.

Regards,
Taran


You cannot align an address to x byte boundary portably. Different
implementations may have different pointer representations.

Nov 14 '05 #2
junky_fellow wrote:
You cannot align an address to x byte boundary portably.


Does casting a _char *_ into an _unsigned long_ have undefined behavior?
Nov 14 '05 #3
"Taran" <ta************@honeywell.com> wrote:
I was wondering how does address alignment to x byte boundary is done.
For example, if I say "adjust the requested size to be on a 4-byte
boundary" or for that matter 8 byte boundary.


You can't ask for that in C. The implementation decided how each object
and pointer must be aligned, and aligns created objects and allocated
memory accordingly. How it does this is its business, not yours, and
there's nothing in the language which allows you to change it.

Richard
Nov 14 '05 #4


Grumble wrote:
junky_fellow wrote:
You cannot align an address to x byte boundary portably.


Does casting a _char *_ into an _unsigned long_ have undefined behavior?


Yes. According to C standard (N869), Section 6.3.2.3

"A pointer to an object or incomplete type may be converted to a
pointer to a different object or incomplete type. If the resulting
pointer is not correctly aligned) for the pointed-to type, the behavior
is undefined."

Nov 14 '05 #5
Grumble <de*****@kma.eu.org> writes:
junky_fellow wrote:
You cannot align an address to x byte boundary portably.


Does casting a _char *_ into an _unsigned long_ have undefined behavior?


No, but it's implementation-defined.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #6
Me
> I was wondering how does address alignment to x byte boundary is done.
For example, if I say "adjust the requested size to be on a 4-byte
boundary" or for that matter 8 byte boundary. How is this
adjustment/alignment done?

I goolged around alot and wans't able to find how is it done, all it
said was what is byte alignment and byte padding.

Also verifying that it is aligned to 4 byte boundary is easy just check
for last 2 bits to be 0. But how is a size of say 3 bytes is aligned on
a 4 byte boundary still is a mystery.


You cannot do this portably. I'm not really sure what you're really
asking here, but here is something that might work on your
implementation:

#include <stdint.h>

unsigned align; void *mem;

uintptr_t p = (uintptr_t)mem;

// round up style:
p = ((p+(align-1)) / align) * align;

// round down style:
p = (p / align) * align;
// or
p -= p % align;
mem = (void*)p;
If you're on an older compiler that doesn't have stdint.h, odds are
either unsigned int or unsigned long should be used in place of
uintptr_t (check your compiler's manual).

Nov 14 '05 #7

Le 15/06/2005 10:20, dans 42****************@news.xs4all.nl, «*Richard Bos*»
<rl*@hoekstra-uitgeverij.nl> a écrit*:
"Taran" <ta************@honeywell.com> wrote:
I was wondering how does address alignment to x byte boundary is done.
For example, if I say "adjust the requested size to be on a 4-byte
boundary" or for that matter 8 byte boundary.


You can't ask for that in C. The implementation decided how each object
and pointer must be aligned, and aligns created objects and allocated
memory accordingly. How it does this is its business, not yours, and
there's nothing in the language which allows you to change it.


If you use gcc, you probably have language extensions for that, but I'm
not sure. And depending on your platform, "malloc-ated" data may be
always aligned (4 byte on MacOSX for example). If there is nothing
you know of, a solution consists in allocating N+3 bytes and adjusting
your pointer, but be careful with "free".

Nov 14 '05 #8
ju**********@yahoo.co.in writes:
Grumble wrote:
junky_fellow wrote:
> You cannot align an address to x byte boundary portably.


Does casting a _char *_ into an _unsigned long_ have undefined behavior?


Yes. According to C standard (N869), Section 6.3.2.3

"A pointer to an object or incomplete type may be converted to a
pointer to a different object or incomplete type. If the resulting
pointer is not correctly aligned) for the pointed-to type, the behavior
is undefined."


No. The section you quoted is about converting one pointer type to
another pointer type. The question was about converting a pointer
type to an integer type, which is implementation-defined, not
undefined.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #9


Keith Thompson wrote:
ju**********@yahoo.co.in writes:
Grumble wrote:
junky_fellow wrote:

> You cannot align an address to x byte boundary portably.

Does casting a _char *_ into an _unsigned long_ have undefined behavior?


Yes. According to C standard (N869), Section 6.3.2.3

"A pointer to an object or incomplete type may be converted to a
pointer to a different object or incomplete type. If the resulting
pointer is not correctly aligned) for the pointed-to type, the behavior
is undefined."


No. The section you quoted is about converting one pointer type to
another pointer type. The question was about converting a pointer
type to an integer type, which is implementation-defined, not
undefined.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


I am sorry. I didn't read that carefully. Thanx for pointing it out.

Nov 14 '05 #10
On Wed, 15 Jun 2005 01:23:42 -0700, junky_fellow wrote:
Grumble wrote:
junky_fellow wrote:
> You cannot align an address to x byte boundary portably.


Does casting a _char *_ into an _unsigned long_ have undefined behavior?


Yes. According to C standard (N869), Section 6.3.2.3

"A pointer to an object or incomplete type may be converted to a
pointer to a different object or incomplete type. If the resulting
pointer is not correctly aligned) for the pointed-to type, the behavior
is undefined."


However that doesn't cover pointer to integer conversions. This does:

"Any pointer type may be converted to an integer type. Except as
previously specified, the result is implementation-defined. If the result
cannot be represented in the integer type, the behavior is undefined. The
result need not be in the range of values of any integer type."

In other words pointer to integer conversion is not a portable operation.
It can result in undefined behaviour and is at best
implementation-defined. There is no requirement that such a conversion
will preserve any sort of alignment related information.

C guarantees that a declared object will be appropriately aligned for its
type, and than a non-null return from malloc/calloc/realloc is
appropriately aligned for any type. There are a few other cases such as
main()'s argv value and fopen()'s return value and these will also be
appropriately aligned. For normal portable programming this is all you
need.

Lawrence

Nov 14 '05 #11
Jean-Claude Arbaut <je****************@laposte.net> wrote:
Le 15/06/2005 10:20, dans 42****************@news.xs4all.nl, «*Richard Bos*»
<rl*@hoekstra-uitgeverij.nl> a écrit*:
"Taran" <ta************@honeywell.com> wrote:
I was wondering how does address alignment to x byte boundary is done.
For example, if I say "adjust the requested size to be on a 4-byte
boundary" or for that matter 8 byte boundary.
You can't ask for that in C. The implementation decided how each object
and pointer must be aligned, and aligns created objects and allocated
memory accordingly. How it does this is its business, not yours, and
there's nothing in the language which allows you to change it.


If you use gcc, you probably have language extensions for that, but I'm
not sure.


It would be off-topic here, anyway.
And depending on your platform, "malloc-ated" data may be
always aligned (4 byte on MacOSX for example).
Allocated memory is always correctly aligned for any type, on all
platforms. How much this is differs between platforms; but memory you
get from malloc() is required to be aligned for all types on that
implementation.
If there is nothing you know of, a solution consists in allocating N+3
bytes and adjusting your pointer,


That carries a grave risk of _de_-aligning the pointer.

Richard
Nov 14 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Marco | last post: by
89 posts views Thread by Sweety | last post: by
5 posts views Thread by Kelvin Moss | last post: by
11 posts views Thread by !truth | last post: by
reply views Thread by kermitthefrogpy | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.