473,320 Members | 1,612 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

malloc() again

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
2 2158
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Rano | last post by:
/* Hello, I've got some troubles with a stupid program... In fact, I just start with the C language and sometime I don't understand how I really have to use malloc. I've readden the FAQ...
7
by: Fatted | last post by:
I'm trying to learn how to create arrays dynamically. But its just not happening. Have a look at code below and point and laugh where appropriate... First part of program, I'm using an array of...
1
by: Dawn Minnis | last post by:
Hey guys - this code when called with parameters: driver.o n n 12 12 12 12 12 12 2.6 3.2 is kicking back a segmentation fault. I've read the rest of the postings but am still confused. Can...
24
by: Hrv'uljak | last post by:
Anybody has a better solution? How to avoid memory allocation in main function? Thanks! -------- #include <stdio.h> #include <conio.h> #include <string.h> #include <malloc.h>
68
by: James Dow Allen | last post by:
The gcc compiler treats malloc() specially! I have no particular question, but it might be fun to hear from anyone who knows about gcc's special behavior. Some may find this post interesting;...
71
by: desktop | last post by:
I have read in Bjarne Stroustrup that using malloc and free should be avoided in C++ because they deal with uninitialized memory and one should instead use new and delete. But why is that a...
22
by: ravi | last post by:
Hi all, I m relatively new to C. I have few queries related to malloc(): 1. When we perform malloc(), the memory allocated dynamically comes from the heap area of the process in concern. Well,...
173
by: Marty James | last post by:
Howdy, I was reflecting recently on malloc. Obviously, for tiny allocations like 20 bytes to strcpy a filename or something, there's no point putting in a check on the return value of malloc....
6
by: Peter Michaux | last post by:
Suppose I have implemented a language with garbage collection in C. I have wrapped malloc in my own C function. If malloc returns NULL then I can run the garbage collector and then try malloc...
25
by: jbholman | last post by:
I am pretty new to C and doing my first project in C. I actually read almost the entire FAQ, but can't seem to figure out this problem. I have a structure. I have a list of these structures. ...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.