malloc() again

R

Robert Stankowic

Reading through the recent discussions about casting/not casting the return
value of *alloc() I stumbled over a probably obvious thought:

There is a difference between a generic void * and the void * returned from
*alloc, because a generic void * has no alignment requirements, while the
void * returned from *alloc() is properly aligned for any type of object.
This eliminates the danger of getting a misaligned <TYPE> *, which IMHO
makes the assignment
<TYPE> *p = malloc(<some_size> * sizeof *P);
perfectly safe in all cases I can imagine.
[OT] I even wonder, why this is not allowed in C++. [/OT]

Any comments?
regards
Robert
 
J

Jack Klein

Reading through the recent discussions about casting/not casting the return
value of *alloc() I stumbled over a probably obvious thought:

There is a difference between a generic void * and the void * returned from
*alloc, because a generic void * has no alignment requirements, while the
void * returned from *alloc() is properly aligned for any type of object.
This eliminates the danger of getting a misaligned <TYPE> *, which IMHO
makes the assignment
<TYPE> *p = malloc(<some_size> * sizeof *P);
perfectly safe in all cases I can imagine.
[OT] I even wonder, why this is not allowed in C++. [/OT]

There is nothing preventing a generic (sic) pointer to void from
happening to meet all of an implementation's requirements for
alignment of all types of objects.

As to why C++ doesn't allow this, comp.lang.c++ is down the hall to
the left. You could also read Stroustrup's "Design & Evolution". Or
you could find it on one of his web pages:

http://www.research.att.com/~bs/bs_faq.html

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
B

Barry Schwarz

Reading through the recent discussions about casting/not casting the return
value of *alloc() I stumbled over a probably obvious thought:

There is a difference between a generic void * and the void * returned from
*alloc, because a generic void * has no alignment requirements, while the
void * returned from *alloc() is properly aligned for any type of object.

The alignment requirement is a requirement on the value that malloc
returns, not on the type of that value. There is only one type of
pointer to void and it is the same whether a value of that type is
"properly" aligned or not.


<<Remove the del for email>>
 

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

No members online now.

Forum statistics

Threads
474,102
Messages
2,570,645
Members
47,247
Latest member
GabrieleL2

Latest Threads

Top