malloc(allocator) aligned by 8 or 16 byte

R

Richard Tobin

Keith Thompson said:
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
 
S

shaanxxx

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 address 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 ?
 
R

Richard Heathfield

shaanxxx said:

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top