| | |
dynamically allocating memory-malloc
Thread Solved |
•
•
Join Date: Aug 2008
Posts: 91
Reputation:
Solved Threads: 0
Hi lately i have been studying the malloc and calloc functions for dynamically allocating memory. I thought the form was:
But then I stumbled onto this code snippet on a tutorial site:
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?
C Syntax (Toggle Plain Text)
(data_type*) malloc(data_size);
c Syntax (Toggle Plain Text)
#include <cstdlib> void *malloc( size_t size );
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.
c Syntax (Toggle Plain Text)
void *malloc ( size_t size );
c Syntax (Toggle Plain Text)
int q = 34; char *test; test = (char*)malloc(q); ... free( test );
Chris
Last edited by Freaky_Chris; Feb 4th, 2009 at 5:29 am.
Knowledge is power -- But experience is everything
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:
General form:
<req>*<variable> = (<req>*)malloc(<size>);
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:
c Syntax (Toggle Plain Text)
struct node{ int a ; char b; }; typedef struct node * NODE; int main() { //Some code NODE temp=(NODE)malloc(sizeof(struct node)); //Some code }
General form:
<req>*<variable> = (<req>*)malloc(<size>);
Last edited by csurfer; Feb 4th, 2009 at 1:02 pm.
I Surf in "C"....
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:
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.
Here's a much improved form:
c Syntax (Toggle Plain Text)
/* Allocate one object */ p = malloc ( sizeof *p ); /* Allocate N objects */ p = malloc ( N * sizeof *p );
I'm here to prove you wrong.
![]() |
Similar Threads
- Dynamically allocating 2d arrays (C)
- dynamic memory allocation in a struct (C)
- Convert char to Hex (C)
- Macro for dynamically allocate multidimensional arrays (C)
- Problem with memory allocation =( (C)
- C++ BASICS ==> Pointers, Call by Reference/Value, Inheritance, Functions & Arrays (C++)
- Pointers (C++)
- A little Help (C++)
Other Threads in the C Forum
- Previous Thread: Help
- Next Thread: Assembly code generated from C program
| Thread Tools | Search this Thread |
#include adobe api array arrays asterisks binarysearch calculate char cm copyanyfile copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic feet fflush fgets file fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o include incrementoperators input interest kernel kilometer linked linkedlist linux linuxsegmentationfault list lists locate logical_drives loopinsideloop. match matrix meter microsoft motherboard mqqueue mysql number odf open opensource owf pattern pdf performance pointer posix probleminc process program programming pyramidusingturboccodes radix read recursion recv repetition research scanf scheduling scripting segmentationfault send sequential shape socket socketprograming stack standard string systemcall turboc unix user voidmain() wab win32api windows.h






