By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,187 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

malloc() again

P: n/a
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


Nov 13 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Mon, 24 Nov 2003 05:42:24 +0100, "Robert Stankowic"
<pc******@netway.at> wrote in comp.lang.c:
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
Nov 13 '05 #2

P: n/a
On Mon, 24 Nov 2003 05:42:24 +0100, "Robert Stankowic"
<pc******@netway.at> wrote:
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>>
Nov 13 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.