473,395 Members | 1,652 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,395 software developers and data experts.

How does malloc align pointer

The standard says that,
The pointer returned by malloc(), if the allocation
succeeds is suitably aligned so that it may be assigned to a
pointer to any type of object.

Since, the alignment cannot be done portably, is that mean
that malloc is implementation specific ? The malloc()
written for one implementation may not be work on other
implementations ?

Also, how does malloc() determine, which pointer type has the most
strict alignment storage restrictions ? Because it has to return
a pointer that would always be aligned to the pointer type that
has most strict alignment storage restriction.

Nov 14 '05 #1
9 7448
ju**********@yahoo.co.in writes:
The standard says that,
The pointer returned by malloc(), if the allocation
succeeds is suitably aligned so that it may be assigned to a
pointer to any type of object.
Right.
Since, the alignment cannot be done portably, is that mean
that malloc is implementation specific ? The malloc()
written for one implementation may not be work on other
implementations ?
Exactly.
Also, how does malloc() determine, which pointer type has the most
strict alignment storage restrictions ? Because it has to return
a pointer that would always be aligned to the pointer type that
has most strict alignment storage restriction.


The person who writes the malloc() function has to know enough about
the compiler to know what alignment is required.

--
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 #2
ju**********@yahoo.co.in wrote:
The standard says that,
The pointer returned by malloc(), if the allocation
succeeds is suitably aligned so that it may be assigned to a
pointer to any type of object.

Since, the alignment cannot be done portably, is that mean
that malloc is implementation specific ?
Yes. (Which is one reason it's in the C library.)
The malloc()
written for one implementation may not be work on other
implementations ?
That's right.
Also, how does malloc() determine, which pointer type has the most
strict alignment storage restrictions ? Because it has to return
a pointer that would always be aligned to the pointer type that
has most strict alignment storage restriction.


The chap who wrote the malloc knows the implementation they're
writing it for, and hence knows what the pointer alignments
are, and hence which one is the strictest.

--
Chris "electric hedgehog" Dollin
It's called *extreme* programming, not *stupid* programming.
Nov 14 '05 #3
In article <11*********************@f14g2000cwb.googlegroups. com>,
<ju**********@yahoo.co.in> wrote:

Also, how does malloc() determine, which pointer type has the most
strict alignment storage restrictions ? Because it has to return
a pointer that would always be aligned to the pointer type that
has most strict alignment storage restriction.


Alignment is dictated by properties of the host CPU and operating
system architecture, which seldom change. Some malloc's just
have a macro or constant set to 4, 8, or 16 (for example) and
it just stays that way for decades.

A malloc meant for more portable use (like the one in the GNU C
library) probably has compile time tests or flags in the
makefile that help determine what CPU and architecture are
in play, combined with some checks on sizeof for some
basic types (long, double).

The final determination of alignment can take into
account the requested size of the item that is passed to
malloc. A request for two bytes of storage might only be
aligned on a two-byte boundary, on the valid assumption
that even though the CPU might require 4 or 8 byte alignment
in the general case, the only things that fit into a 2 byte
space (char, short) happen to require only 2 byte alignment.
--
7842++
Nov 14 '05 #4
On Tue, 21 Jun 2005 17:59:43 +0000, Anonymous 7843 wrote:

....
The final determination of alignment can take into
account the requested size of the item that is passed to
malloc. A request for two bytes of storage might only be
aligned on a two-byte boundary, on the valid assumption
that even though the CPU might require 4 or 8 byte alignment
in the general case, the only things that fit into a 2 byte
space (char, short) happen to require only 2 byte alignment.


The standard requires a non-null return from malloc() to be appropriately
aligned for any type, irrespective of the size of the allocation. It isn't
clear that a strictly conforming program could tell the difference in
every case but an implementation that doesn't stick to this rule is
treading on uncertain ground.

Lawrence

Nov 14 '05 #5
In article <pa****************************@netactive.co.uk> ,
Lawrence Kirby <lk****@netactive.co.uk> wrote:


On Tue, 21 Jun 2005 17:59:43 +0000, Anonymous 7843 wrote:

...
The final determination of alignment can take into
account the requested size of the item that is passed to
malloc. A request for two bytes of storage might only be
aligned on a two-byte boundary, on the valid assumption
that even though the CPU might require 4 or 8 byte alignment
in the general case, the only things that fit into a 2 byte
space (char, short) happen to require only 2 byte alignment.


The standard requires a non-null return from malloc() to be appropriately
aligned for any type, irrespective of the size of the allocation. It isn't
clear that a strictly conforming program could tell the difference in
every case but an implementation that doesn't stick to this rule is
treading on uncertain ground.

Lawrence


A pointer-type-mismatch-signalling implementation might want to
steer clear of this trick. Can't think of anything else.
--
7842++
Nov 14 '05 #6
On Tue, 21 Jun 2005 23:24:21 +0000, Anonymous 7843 wrote:
In article <pa****************************@netactive.co.uk> ,
Lawrence Kirby <lk****@netactive.co.uk> wrote:


On Tue, 21 Jun 2005 17:59:43 +0000, Anonymous 7843 wrote:

...
The final determination of alignment can take into
account the requested size of the item that is passed to
malloc. A request for two bytes of storage might only be
aligned on a two-byte boundary, on the valid assumption
that even though the CPU might require 4 or 8 byte alignment
in the general case, the only things that fit into a 2 byte
space (char, short) happen to require only 2 byte alignment.


The standard requires a non-null return from malloc() to be appropriately
aligned for any type, irrespective of the size of the allocation. It isn't
clear that a strictly conforming program could tell the difference in
every case but an implementation that doesn't stick to this rule is
treading on uncertain ground.

Lawrence


A pointer-type-mismatch-signalling implementation might want to
steer clear of this trick. Can't think of anything else.


An issue would be something like this

char *pc = malloc(1);

if (pc != NULL)
pc = (char *)(long *)pc;

This could fail if the return value of malloc() is not appropriately
aligned for long. The question is that since it *is* required to be so
aligned whether the cast to long * and back is valid.

Lawrence


Nov 14 '05 #7
In article <pa****************************@netactive.co.uk> ,
Lawrence Kirby <lk****@netactive.co.uk> wrote:
On Tue, 21 Jun 2005 23:24:21 +0000, Anonymous 7843 wrote:
In article <pa****************************@netactive.co.uk> ,
Lawrence Kirby <lk****@netactive.co.uk> wrote:

The standard requires a non-null return from malloc() to be appropriately
aligned for any type, irrespective of the size of the allocation. It isn't
clear that a strictly conforming program could tell the difference in
every case but an implementation that doesn't stick to this rule is
treading on uncertain ground.

Lawrence


A pointer-type-mismatch-signalling implementation might want to
steer clear of this trick. Can't think of anything else.


An issue would be something like this

char *pc = malloc(1);

if (pc != NULL)
pc = (char *)(long *)pc;

This could fail if the return value of malloc() is not appropriately
aligned for long. The question is that since it *is* required to be so
aligned whether the cast to long * and back is valid.


I don't see any question about it. The cast is valid C.

The implementor must be aware of whether the pointers are compatible or
not _on that implementation_ and make the choice to use the sub-aligned-
malloc trick accordingly. He may have other reasons for not using it
(i.e. it's an untested wild suggestion from some anonymous borderline
crackpot on comp.lang.c).

One subtlety is that on a lot of ostensibly pointer-alignment-sensitive
architectures (SPARC, for one) the pointers are not really checked for
alignment until dereferencing, in other words the ptr alignment stuff
is a misnomer: it would be better to call it dereference alignment or
object alignment.
--
7842++
Nov 15 '05 #8
Keith Thompson wrote:
ju**********@yahoo.co.in writes: [...]
Also, how does malloc() determine, which pointer type has the most
strict alignment storage restrictions ? Because it has to return
a pointer that would always be aligned to the pointer type that
has most strict alignment storage restriction.

The person who writes the malloc() function has to know enough about
the compiler to know what alignment is required.


Is the usual union alignment hack^H^H^H^Htrick not good enough?

-Peter

--
Pull out a splinter to reply.
Nov 15 '05 #9
Peter Ammon <ge******@splintermac.com> writes:
Keith Thompson wrote:
ju**********@yahoo.co.in writes:

[...]
Also, how does malloc() determine, which pointer type has the most
strict alignment storage restrictions ? Because it has to return
a pointer that would always be aligned to the pointer type that
has most strict alignment storage restriction.

The person who writes the malloc() function has to know enough about
the compiler to know what alignment is required.


Is the usual union alignment hack^H^H^H^Htrick not good enough?


It's not easy, and it may not even be possible, to pick a set of types
that's guaranteed to include one with the implementation's strictest
alignment.

--
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 15 '05 #10

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

Similar topics

36
by: Bhalchandra Thatte | last post by:
I am allocating a block of memory using malloc. I want to use it to store a "header" structure followed by structs in my application. How to calculate the alignment without making any assumption...
34
by: Richard Hunt | last post by:
I'm sorry for asking such a silly question, but I can't quite get my head around malloc. Using gcc I have always programmed in a lax C/C++ hybrid (which I suppose is actually c++). But I have...
24
by: David Mathog | last post by:
If this: int i,sum; int *array; for(sum=0, i=0; i<len; i++){ sum += array; } is converted to this (never mind why for the moment):
74
by: Suyog_Linux | last post by:
I wish to know how the free()function knows how much memory to be freed as we only give pointer to allocated memory as an argument to free(). Does system use an internal variable to store allocated...
18
by: cs | last post by:
This is the function malloc_m() that should be like malloc() but it should find memory leak, and over bound writing in arrays. How many errors do you see? Thank you...
6
by: lovecreatesbeauty | last post by:
Hello experts, 1. Does C guarantee the data layout of the memory allocated by malloc function on the heap. I mean, for example, if I allocate a array of 100 elements of structure, can I always...
5
by: mkaushik | last post by:
Hi everyone, Im just starting out with C++, and am curious to know how "delete <pointer>", knows about the number of memory locations to free. I read somewhere that delete frees up space...
52
by: ¬a\\/b | last post by:
is it possible that a malloc function that use a nobody student is better than all yours :) How many errors do you see? Bug1: memory<13MB Bug2: it seems do you have to write you own sprintf...
8
by: digz | last post by:
I saw the following code and comments in a source file, I am not able to appreciate what the author is trying to do in ALIGNED_ALLOC , can some one plz help me understand why CACHE_LINE_SIZE * 2...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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...
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...

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.