dynamically allocating memory-malloc

Thread Solved

Join Date: Aug 2008
Posts: 91
Reputation: CPPRULZ is an unknown quantity at this point 
Solved Threads: 0
CPPRULZ CPPRULZ is offline Offline
Junior Poster in Training

dynamically allocating memory-malloc

 
0
  #1
Feb 4th, 2009
Hi lately i have been studying the malloc and calloc functions for dynamically allocating memory. I thought the form was:
  1. (data_type*) malloc(data_size);
But then I stumbled onto this code snippet on a tutorial site:
  1. #include <cstdlib>
  2. void *malloc( size_t size );
Why is the data type void?
Is my understanding of the malloc and calloc functions wrong or is this website wacky?
Oh and is the * after void for the pointer returned?
Last edited by CPPRULZ; Feb 4th, 2009 at 2:17 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 443
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 68
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: dynamically allocating memory-malloc

 
0
  #2
Feb 4th, 2009
malloc returns a void* to the memory allocated and you need to ensure that you type cast it to the correct type.
thanks
-chandra
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: dynamically allocating memory-malloc

 
1
  #3
Feb 4th, 2009
  1. void *malloc ( size_t size );
This is the function prototype, and as Agni stated it returns a void pointer. Since this is the function prototype you do not use this syntax when calling the function and infact you use the syntax you originally posted. Here is a small example
  1. int q = 34;
  2. char *test;
  3. test = (char*)malloc(q);
  4. ...
  5. free( test );
The snippet above will create a character array (c-string) of length 33, since 1 space is need for the null terminator. Be sure to read about the use of free(), it's essential!

Chris
Last edited by Freaky_Chris; Feb 4th, 2009 at 5:29 am.
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 476
Reputation: csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice csurfer is just really nice 
Solved Threads: 76
csurfer's Avatar
csurfer csurfer is offline Offline
Posting Pro in Training

Re: dynamically allocating memory-malloc

 
1
  #4
Feb 4th, 2009
malloc(<size>) just allocates a memory space for the size specified and returns a void * pointer pointing to the starting of the allocated memory block.
It returns a void * pointer (so that it can be type cast to any pointer type) and as you know void * pointer itself cannot be used to dereference any variable.So we need to typecast it according to our needs.
Example:
  1. struct node{
  2. int a ;
  3. char b;
  4. };
  5. typedef struct node * NODE;
  6.  
  7. int main()
  8. {
  9. //Some code
  10.  
  11. NODE temp=(NODE)malloc(sizeof(struct node));
  12.  
  13. //Some code
  14. }

General form:

<req>*<variable> = (<req>*)malloc(<size>);
Last edited by csurfer; Feb 4th, 2009 at 1:02 pm.
I Surf in "C"....
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,630
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 718
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: dynamically allocating memory-malloc

 
2
  #5
Feb 4th, 2009
The cast is bad in C. It hides potential errors such as forgetting to include <stdlib.h> and makes maintenance more difficult if you change the type of the pointer, so you'd best get into the habit of not casting the return value. Pointers are implicitly converted to and from void* with no complaints, so there's no good reason to cast anyway.

Here's a much improved form:
  1. /* Allocate one object */
  2. p = malloc ( sizeof *p );
  3.  
  4. /* Allocate N objects */
  5. p = malloc ( N * sizeof *p );
The cast is a throwback from the dark ages when char* was the generic pointer rather than void* and there wasn't an explicit conversion rule in place.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC