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

 
 
Richard Tobin
Guest
Posts: n/a
 
      09-13-2007
In article <(E-Mail Removed)>,
Keith Thompson <(E-Mail Removed)> wrote:

>There is at least one real consequence of the requirement. Converting
>from one pointer type to another invokes UB if the resulting pointer
>is not correctly aligned. So this:
>
> long double *ptr = malloc(1);
>
>does *not* invoke UB even if you can't store a long double in the
>allocated memory.


True; I hadn't thought of that case.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
 
Reply With Quote
 
 
 
 
shaanxxx
Guest
Posts: n/a
 
      09-14-2007
On Sep 14, 1:56 am, Keith Thompson <(E-Mail Removed)> wrote:
> (E-Mail Removed) (Richard Tobin) writes:
> > In article <(E-Mail Removed)>,
> > Keith Thompson <(E-Mail Removed)> wrote:
> >>> 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).

>
> >>Actually the standard requires the memory to be aligned for any type
> >>whether it's big enough or not (though if an implementation violated
> >>this rule, it's not clear that a program could detect it).

>
> > And if you can't detect it, the standard doesn't require it.

>
> There is at least one real consequence of the requirement. Converting
> from one pointer type to another invokes UB if the resulting pointer
> is not correctly aligned. So this:
>
> long double *ptr = malloc(1);
>
> does *not* invoke UB even if you can't store a long double in the
> allocated memory.
>
> But if the implementation doesn't impose alignment requirements on
> converted pointers, but does impose alignment requirements on objects,
> then an implementation can get away with having malloc(1) return a
> misaligned pointer without any portable program being able to detect
> it.
>
> What I mean by the above is, for example, if a long double object must
> always be aligned on an 8-byte boundary (attempting to read or write a
> misaligned long double invokes UB), but all pointers are just byte
> pointers, and constructing a 'long double*' that points to an odd
> address doesn't do anything nasty.
>
> [...]
>
> --
> Keith Thompson (The_Other_Keith) (E-Mail Removed) <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."
> -- Antony Jay and Jonathan Lynn, "Yes Minister"


sorry I am not good at short forms , what is UB ?

 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      09-14-2007
shaanxxx said:

<snip>

> sorry I am not good at short forms , what is UB ?


He means "undefined behaviour", i.e. behaviour on which the Standard
imposes no constraints.

--
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
 
 
 
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