Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > malloc(allocator) aligned by 8 or 16 byte

Reply
Thread Tools

malloc(allocator) aligned by 8 or 16 byte

 
 
shaanxxx
Guest
Posts: n/a
 
      09-12-2007
why malloc (allocator) guarantees that address return by them will be
aligned by 8 byte ( on 32bit machin ) or 16 byte (64 bit machin) ?

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      09-12-2007
shaanxxx wrote:
> why malloc (allocator) guarantees that address return by them will be
> aligned by 8 byte ( on 32bit machin ) or 16 byte (64 bit machin) ?
>

It doesn't. It guarantees the address is suitably aligned for any type.
This has to be the case because malloc returns a void* which can be
assigned to any pointer type.

--
Ian Collins.
 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      09-12-2007
shaanxxx said:

> why malloc (allocator) guarantees that address return by them will be
> aligned by 8 byte ( on 32bit machin ) or 16 byte (64 bit machin) ?


The C language standard makes no such guarantee.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      09-12-2007
In article <(E-Mail Removed). com>,
shaanxxx <(E-Mail Removed)> wrote:
>why malloc (allocator) guarantees that address return by them will be
>aligned by 8 byte ( on 32bit machin ) or 16 byte (64 bit machin) ?


Presumably on those systems there are objects that have to be aligned
on 8- or 16- byte boundaries. For example, it's quite likely that a
double is 8 bytes long and has to be 8-byte aligned. Even if it
doesn't *have* to be aligned, it may be more efficient if it is.
This varies from system to system - you can't rely on it.

Since malloc() doesn't know what kind of object you're going to store
in the memory, it has to make sure it's aligned for any kind (or at
least, any kind that will fit).

I can't guess what type it is on the 64-bit machine that requires
16-byte alignment.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
 
Reply With Quote
 
Michal Nazarewicz
Guest
Posts: n/a
 
      09-12-2007
shaanxxx <(E-Mail Removed)> writes:

> why malloc (allocator) guarantees that address return by them will be
> aligned by 8 byte ( on 32bit machin ) or 16 byte (64 bit machin) ?


IIRC malloc() (on success) is guaranteed to return address that is
aligned so that any object can be stored correctly. Why it is exactly 8
or 16 bytes you should check in your compiler's documentation.

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>---<jid:mina86*chrome.pl>--ooO--(_)--Ooo--
 
Reply With Quote
 
shaanxxx
Guest
Posts: n/a
 
      09-12-2007
On Sep 12, 4:03 pm, Ian Collins <(E-Mail Removed)> wrote:
> shaanxxx wrote:
> > why malloc (allocator) guarantees that address return by them will be
> > aligned by 8 byte ( on 32bit machin ) or 16 byte (64 bit machin) ?

>
> It doesn't. It guarantees the address is suitably aligned for any type.
> This has to be the case because malloc returns a void* which can be
> assigned to any pointer type.
>
> --
> Ian Collins.


http://g.oswego.edu/dl/html/malloc.html

if you read this document, you will find that author is little keen
about keeping alignment at eight byte.


 
Reply With Quote
 
Charlie Gordon
Guest
Posts: n/a
 
      09-12-2007
"Richard Tobin" <(E-Mail Removed)> a écrit dans le message de news:
fc8hql$24ri$(E-Mail Removed)...
> In article <(E-Mail Removed). com>,
> shaanxxx <(E-Mail Removed)> wrote:
>>why malloc (allocator) guarantees that address return by them will be
>>aligned by 8 byte ( on 32bit machin ) or 16 byte (64 bit machin) ?

>
> Presumably on those systems there are objects that have to be aligned
> on 8- or 16- byte boundaries. For example, it's quite likely that a
> double is 8 bytes long and has to be 8-byte aligned. Even if it
> doesn't *have* to be aligned, it may be more efficient if it is.
> This varies from system to system - you can't rely on it.
>
> Since malloc() doesn't know what kind of object you're going to store
> in the memory, it has to make sure it's aligned for any kind (or at
> least, any kind that will fit).
>
> I can't guess what type it is on the 64-bit machine that requires
> 16-byte alignment.


long double could have such a requirement.
long long could be 128 bits on a 64 bit machine, and have the same
requirement.
almost anything at all can have such a requirement on a DS9K

--
Chqrlie.


 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      09-12-2007
shaanxxx <(E-Mail Removed)> wrote:

> On Sep 12, 4:03 pm, Ian Collins <(E-Mail Removed)> wrote:
> > shaanxxx wrote:
> > > why malloc (allocator) guarantees that address return by them will be
> > > aligned by 8 byte ( on 32bit machin ) or 16 byte (64 bit machin) ?

> >
> > It doesn't. It guarantees the address is suitably aligned for any type.
> > This has to be the case because malloc returns a void* which can be
> > assigned to any pointer type.

>
> http://g.oswego.edu/dl/html/malloc.html
>
> if you read this document, you will find that author is little keen
> about keeping alignment at eight byte.


Perhaps, but he's writing one for Unix. It may be that on his variant of
Unix, 8 bytes are a reasonable granularity for memory. On some systems
it may be larger, on some smaller. On some, it may be 1 byte. On some, a
whole sector of 256 bytes. It all depends on the system; ISO C says
nothing about this, except that the memory returned by malloc() must be
aligned suitably for any object type - but again, how strict object must
be aligned depends on the system.
As for why these different systems require their respective alignments
to be as strict as they are - that, too, varies with the system. On some
it's just faster because of how their microprocessors work. On some
others it might only be more convenient because of the OS's memory
handler. If you want to know, read your implementation's, your OS's, and
your hardware's manuals. ISO C, and therefore comp.lang.c, cannot tell.

Richard
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      09-13-2007
Richard Bos wrote:
>
> shaanxxx <(E-Mail Removed)> wrote:
>
> > On Sep 12, 4:03 pm, Ian Collins <(E-Mail Removed)> wrote:
> > > shaanxxx wrote:
> > > > why malloc (allocator) guarantees that address return by them will be
> > > > aligned by 8 byte ( on 32bit machin ) or 16 byte (64 bit machin) ?
> > >
> > > It doesn't. It guarantees the address is suitably aligned for any type.
> > > This has to be the case because malloc returns a void* which can be
> > > assigned to any pointer type.

> >
> > http://g.oswego.edu/dl/html/malloc.html
> >
> > if you read this document, you will find that author is little keen
> > about keeping alignment at eight byte.

>
> Perhaps, but he's writing one for Unix.


And also,
"This article is now out of date,
and doesn't reflect details of current version of malloc."

--
pete
 
Reply With Quote
 
shaanxxx
Guest
Posts: n/a
 
      09-13-2007
On Sep 13, 5:31 am, pete <(E-Mail Removed)> wrote:
> Richard Bos wrote:
>
> > shaanxxx <(E-Mail Removed)> wrote:

>
> > > On Sep 12, 4:03 pm, Ian Collins <(E-Mail Removed)> wrote:
> > > > shaanxxx wrote:
> > > > > why malloc (allocator) guarantees that address return by them will be
> > > > > aligned by 8 byte ( on 32bit machin ) or 16 byte (64 bit machin) ?

>
> > > > It doesn't. It guarantees the address is suitably aligned for any type.
> > > > This has to be the case because malloc returns a void* which can be
> > > > assigned to any pointer type.

>
> > >http://g.oswego.edu/dl/html/malloc.html

>
> > > if you read this document, you will find that author is little keen
> > > about keeping alignment at eight byte.

>
> > Perhaps, but he's writing one for Unix.

>
> And also,
> "This article is now out of date,
> and doesn't reflect details of current version of malloc."
>
> --
> pete- Hide quoted text -
>
> - Show quoted text -


where i can find the current version of malloc ?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
byte[] > string > byte[] problem Peter Java 3 08-05-2004 10:55 AM
connection 1: transfer chunk 1 (byte 0 to byte 1024) Jean-Daniel Gamache Java 0 07-14-2004 03:57 AM
bitcounting on 30000+ bits, but not byte aligned Eric C++ 10 06-27-2004 06:58 AM
Single byte addressable, multiple byte readout. Andreas VHDL 1 05-04-2004 01:49 PM
Appending byte[] to another byte[] array Bharat Bhushan Java 15 08-05-2003 07:52 PM



Advertisments