943,540 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 2623
  • C RSS
Feb 4th, 2009
0

dynamically allocating memory-malloc

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
CPPRULZ is offline Offline
91 posts
since Aug 2008
Feb 4th, 2009
0

Re: dynamically allocating memory-malloc

malloc returns a void* to the memory allocated and you need to ensure that you type cast it to the correct type.
Featured Poster
Reputation Points: 431
Solved Threads: 116
Practically a Master Poster
Agni is offline Offline
654 posts
since Dec 2007
Feb 4th, 2009
1

Re: dynamically allocating memory-malloc

  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.
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Feb 4th, 2009
1

Re: dynamically allocating memory-malloc

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.
Reputation Points: 485
Solved Threads: 88
Posting Pro
csurfer is offline Offline
564 posts
since Jan 2009
Feb 4th, 2009
2

Re: dynamically allocating memory-malloc

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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Help
Next Thread in C Forum Timeline: Assembly code generated from C program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC