473,769 Members | 4,202 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Allocating memory aligned on a given boundary

I would like to get the result of malloc() such that it is
aligned on a given boundary - 8, in this case. Assuming that sizeof(u64)
is 8, will

u64 *p = malloc(N) ;

do the trick in general?

Here N is a positive integer, and I am assuming that malloc()
succeeds in allocating the chunk of memory specified.

This seems to work, but I do not know whether it is just my
compiler, or a general rule in C.
Apr 24 '07 #1
29 2834
K. Jennings said:
I would like to get the result of malloc() such that it is
aligned on a given boundary - 8, in this case. Assuming that
sizeof(u64) is 8, will

u64 *p = malloc(N) ;

do the trick in general?
The C Standard guarantees that malloc's result (if not NULL) is always
correctly aligned for any object type.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Apr 24 '07 #2
In article <7K************ *************** ***@bt.com>,
Richard Heathfield <rj*@see.sig.in validwrote:
>K. Jennings said:
>I would like to get the result of malloc() such that it is
aligned on a given boundary - 8, in this case. Assuming that
sizeof(u64) is 8, will
>u64 *p = malloc(N) ;
>do the trick in general?
>The C Standard guarantees that malloc's result (if not NULL) is always
correctly aligned for any object type.
Expanding slightly:

This means that if the particular implementation is able and
willing to handle u64's (and everything else) on (say) 4 byte boundaries,
then malloc(sizeof u64) is not guaranteed to be aligned on a multiple
of sizeof u64. The guarantee is only that it will be allocated on
an accessible boundary. The difference between accessible boundaries
and specific alignment boundaries (e.g., 8) can sometimes be of
concern, such as if you are trying to take into account cache effects.
--
Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us. -- Ecclesiastes
Apr 24 '07 #3
Richard Heathfield wrote, On 24/04/07 17:41:
K. Jennings said:
>I would like to get the result of malloc() such that it is
aligned on a given boundary - 8, in this case. Assuming that
sizeof(u64) is 8, will

u64 *p = malloc(N) ;

do the trick in general?

The C Standard guarantees that malloc's result (if not NULL) is always
correctly aligned for any object type.
However it makes no guarantees about what the alignment might be nor any
way to find out. So if you really want a specific alignment, for example
so that you can use some highly system specific tricks, you will need to
use some highly system specific method. Any such methods and tricks
would be off topic here so you would have to ask in a group dealing with
the specific platform(s) of interest.
--
Flash Gordon
Apr 24 '07 #4
"K. Jennings" <kj*******@resu rgence.netha scritto nel messaggio
news:pa******** *************@r esurgence.net.. .
I would like to get the result of malloc() such that it is
aligned on a given boundary - 8, in this case. Assuming that sizeof(u64)
is 8, will

u64 *p = malloc(N) ;

do the trick in general?

Here N is a positive integer, and I am assuming that malloc()
succeeds in allocating the chunk of memory specified.
Is the real question "What happens if N isn't a multiple of 8?"?
Apr 24 '07 #5
On Tue, 24 Apr 2007 21:26:05 +0200, Army1987 wrote:
"K. Jennings" <kj*******@resu rgence.netha scritto nel messaggio
news:pa******** *************@r esurgence.net.. .
>I would like to get the result of malloc() such that it is aligned on a
given boundary - 8, in this case. Assuming that sizeof(u64) is 8, will

u64 *p = malloc(N) ;

do the trick in general?

Here N is a positive integer, and I am assuming that malloc() succeeds
in allocating the chunk of memory specified.

Is the real question "What happens if N isn't a multiple of 8?"?
No. The actual value of N is irrelevant, as far as that is
concerned.

Apr 24 '07 #6
K. Jennings wrote On 04/24/07 15:43,:
On Tue, 24 Apr 2007 21:26:05 +0200, Army1987 wrote:

>>"K. Jennings" <kj*******@resu rgence.netha scritto nel messaggio
news:pa****** *************** @resurgence.net ...
>>>I would like to get the result of malloc() such that it is aligned on a
given boundary - 8, in this case. Assuming that sizeof(u64) is 8, will

u64 *p = malloc(N) ;

do the trick in general?

Here N is a positive integer, and I am assuming that malloc() succeeds
in allocating the chunk of memory specified.

Is the real question "What happens if N isn't a multiple of 8?"?


No. The actual value of N is irrelevant, as far as that is
concerned.
Are you still seeking answers, and if so, to what?
To summarize what's appeared thus far: A successful malloc()
allocates memory that is suitably aligned for all C data
types (but not necessarily for non-C "types" like "pages"),
and there is no portable way to discover what the "suitable
for all types" alignment requirement is, nor even to discover
the alignment of any particular allocation. Does this answer
your question, or is there something else you hope to learn?

--
Er*********@sun .com
Apr 24 '07 #7
On Tue, 24 Apr 2007 16:49:14 -0400, Eric Sosman wrote:
K. Jennings wrote On 04/24/07 15:43,:
>On Tue, 24 Apr 2007 21:26:05 +0200, Army1987 wrote:

>>>"K. Jennings" <kj*******@resu rgence.netha scritto nel messaggio
news:pa***** *************** *@resurgence.ne t...

I would like to get the result of malloc() such that it is aligned on
a given boundary - 8, in this case. Assuming that sizeof(u64) is 8,
will

u64 *p = malloc(N) ;

do the trick in general?

Here N is a positive integer, and I am assuming that malloc() succeeds
in allocating the chunk of memory specified.

Is the real question "What happens if N isn't a multiple of 8?"?


No. The actual value of N is irrelevant, as far as that is
concerned.

Are you still seeking answers, and if so, to what?
To summarize what's appeared thus far: A successful malloc() allocates
memory that is suitably aligned for all C data types (but not
necessarily for non-C "types" like "pages"), and there is no portable
way to discover what the "suitable for all types" alignment requirement
is, nor even to discover the alignment of any particular allocation.
Does this answer your question, or is there something else you hope to
learn?
I think I have the info that I wanted. If I understand it
correctly, it makes no difference whether you do

u64 *p = malloc(N) ;

or

char *p = malloc(N) ;

the address returned, when non-NULL, will be aligned on a boundary equal
to the largest basic data size supported. Thus, for 32-bit architectures
it might be either 4 or 8 bytes, whereas for 64-bit ones it would be 8.
Or am I missing something?
Apr 25 '07 #8
K. Jennings wrote:
On Tue, 24 Apr 2007 16:49:14 -0400, Eric Sosman wrote:

>>K. Jennings wrote On 04/24/07 15:43,:
>>>On Tue, 24 Apr 2007 21:26:05 +0200, Army1987 wrote:

"K. Jennings" <kj*******@resu rgence.netha scritto nel messaggio
news:pa**** *************** **@resurgence.n et...
>I would like to get the result of malloc() such that it is aligned on
>a given boundary - 8, in this case. Assuming that sizeof(u64) is 8,
>will
>
>u64 *p = malloc(N) ;
>
>do the trick in general?
>
>Here N is a positive integer, and I am assuming that malloc() succeeds
>in allocating the chunk of memory specified.

Is the real question "What happens if N isn't a multiple of 8?"?
No. The actual value of N is irrelevant, as far as that is
concerned.

Are you still seeking answers, and if so, to what?
To summarize what's appeared thus far: A successful malloc() allocates
memory that is suitably aligned for all C data types (but not
necessarily for non-C "types" like "pages"), and there is no portable
way to discover what the "suitable for all types" alignment requirement
is, nor even to discover the alignment of any particular allocation.
Does this answer your question, or is there something else you hope to
learn?


I think I have the info that I wanted. If I understand it
correctly, it makes no difference whether you do

u64 *p = malloc(N) ;

or

char *p = malloc(N) ;

the address returned, when non-NULL, will be aligned on a boundary equal
to the largest basic data size supported. Thus, for 32-bit architectures
it might be either 4 or 8 bytes, whereas for 64-bit ones it would be 8.
Or am I missing something?
Or it might be something else if there is a bigger type (long double for
example). On my boxes, the alignment is 16 on 32 and 64 bit
(sizeof(long double) == 12 ).

--
Ian Collins.
Apr 25 '07 #9
"K. Jennings" <kj*******@resu rgence.netwrote :
I think I have the info that I wanted. If I understand it
correctly, it makes no difference whether you do

u64 *p = malloc(N) ;

or

char *p = malloc(N) ;

the address returned, when non-NULL, will be aligned on a boundary equal
to the largest basic data size supported.
No, it will be aligned at least on the strictest required alignment
boundary. Often, that will be the same as the largest basic type size,
but that's not necessarily true. For example...
Thus, for 32-bit architectures it might be either 4 or 8 bytes, whereas
for 64-bit ones it would be 8.
....that 8-byte type might only need an alignment of 4 bytes, i.e., it
could be legally aligned on boundaries that are only half its own size
apart.
On some systems, there might not be any alignment requirements at all.
All objects, regardless of size, could be located anywhere in memory. On
that system, malloc() could return pointers at any position.
OTOH, on some systems, malloc() could return pointers that are more
widely aligned than the most restrictive basic type needs. Apart from a
requirement of the underlying hardware, I cannot think of any good
reason why it should, but you can't assume that it doesn't.

And then there are the _really_ wild systems, where long ints are 4
bytes but doubles are 5 bytes, and void *s 7... in which case, malloc()
would need to return pointers aligned to 4*5*7==140 byte boundaries. I
don't know of any example of this in practice, but it could exist, there
might be good reasons for it to exist, and the Standard doesn't forbid
it. So really, my first sentence in this post should be "No, it will be
aligned to the least common multiple of all basic object type sizes".

Richard
Apr 25 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
2368
by: pinkfloydhomer | last post by:
Please read the example below. I'm sorry I couldn't make it smaller, but it is pretty simple. When client code is calling newThingy(), it is similar to malloc: the client gets a pointer to a chunk of memory that it can use for it's own purposes. But on the "library" side, I want to be able to do some book-keeping for each memory block that I hand out. I want some "hidden" meta-data (that I keep in struct Container) to be associated...
2
5218
by: O.B. | last post by:
Given the two following structures: public struct Articulation { public byte typeDesignator; // 1 byte public byte changeIndicator; // 1 byte public Unsigned16 attachmentID; // 2 bytes public Unsigned32 type; // 4 bytes public Bits64 value; // 8 bytes }
17
5711
by: Frank Rizzo | last post by:
Hello, I've compiled my app for AnyCPU setting in vs2005. Then I tried the app on both 32-bit Windows 2003 R2 and 64-bit Windows 2003 R2. The memory usage of the application when working on the same data set were unbelievable (the data is from Task Manager) : 32-bit Windows 2003 R2 Mem Usage: 481,400k Peak Mem: 583,020k
13
3633
by: Chris Thomasson | last post by:
Here is some info on a C++ allocator prototype I am working on: http://groups.google.com/group/comp.lang.c++/browse_frm/thread/beeee1f61fdbb52c Any tried-and-true techniques for calculating the correct alignment of any C++ type the user can throw at it? For the initial code, I was assuming that the alignment(T) == sizeof(T)... Now that I am so close to being able to release this thing, I wanted to be able to stitch up this loose end. ...
43
2376
by: bharath539 | last post by:
how much memory is allocated for following structure struct bharath { int b; char c; float d; } and how?
2
3800
by: somenath | last post by:
Hi All, I have one question regarding the alignment of pointer returned by malloc. In K&R2 page number 186 one union is used to enforce the alignment as mentioned bellow. typedef long Align; union header { struct {
6
5185
by: Francois Grieu | last post by:
Hello, I'm asking myself all kind of questions on allocating an array of struct with proper alignment. Is the following code oorrect ? I'm most interested by the statement t = malloc(n*sizeof(r)) and (to a degree) by the surrounding error checking.
31
1690
by: Chris Thomasson | last post by:
How many C compilers provide extensions which allow for a standard implementation of the following hack? ____________________________________________________________________ #include <stdio.h> typedef union aligner_types_u aligner_types; typedef struct aligner_offset_s aligner_offset;
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10219
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9865
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7413
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3967
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.