473,405 Members | 2,160 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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 3736


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Marco | last post by:
Hi I have to read a binary image. Info tell me that it is a binary image without encoding but with RLE. Also i know it is 4 bytes align. What it means ?? How can i read it ?? I red it is for...
89
by: Sweety | last post by:
hi, Is main function address is 657. its show in all compiler. try it & say why? bye,
5
by: Kelvin Moss | last post by:
Hi group, I have an array of structure. Say - typedef struct Person { int i; int j; int k; }person;
14
by: gamja | last post by:
Hi all. This is my first post on this group. Nice to meet you, cool guys~! I'm on system programming on various embedded systems and understand very well the byte alignment issues. When I write...
16
by: Ekim | last post by:
hello, I'm allocating a byte-Array in C# with byte byteArray = new byte; Now I want to pass this byte-Array to a managed C++-function by reference, so that I'm able to change the content of the...
16
by: jimjim | last post by:
#include <stdio.h> #include <stdlib.h> //WARNING: The function's signature should be: f( int ** ) void f(int *ip) { static int dummy = 5; *ip = &dummy; } int main(){
6
by: fnoppie | last post by:
Hi, I am near to desperation as I have a million things to get a solution for my problem. I have to post a multipart message to a url that consists of a xml file and an binary file (pdf)....
11
by: !truth | last post by:
Hi, i feel confused about the following program, and it's works to get a pointer-member's address. #define NETDEV_ALIGN 32 #define NETDEV_ALIGN_CONST (NETDEV_ALIGN - 1) ...
15
by: Pengjun Jia | last post by:
HP-UX 11.23, HP aC++ B3910B A.03.63 Here is a samples. bjhp1 /nfs/users/pjia>cat tt.c #include <stdlib.h> int main() { int i =10 ;
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.