| | |
Malloc/Calloc Dynamic Memory Allocation.
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2007
Posts: 15
Reputation:
Solved Threads: 0
Can someone please explain to me how dynamic memory allocation actually works? For example whast going on in the piece of code written below? If you can respond asap, that would be great. Thanks in advance.
And whats the difference in malloc and calloc?
C++ Syntax (Toggle Plain Text)
p = (float *) malloc ( n * sizeof( float ));
And whats the difference in malloc and calloc?
•
•
Join Date: Oct 2007
Posts: 15
Reputation:
Solved Threads: 0
whats the difference in malloc and calloc/?
•
•
•
•
Can someone please explain to me how dynamic memory allocation actually works? For example whast going on in the piece of code written below? If you can respond asap, that would be great. Thanks in advance.
C++ Syntax (Toggle Plain Text)
p = (float *) malloc ( n * sizeof( float ));
And whats the difference in malloc and calloc?
•
•
•
•
Originally Posted by Ancient Dragon
>>And whats the difference in malloc and calloc?
calloc() calls malloc() then initializes the data to all 0s.
A computer program is loaded into several parts of memory. There is the code (functions and the like), data (global variables and the like), the stack (local variables and the like), and the heap.
The heap is just a big block of unused memory that your program is allowed to use. When you call malloc() it takes a chunk of the heap and marks it as 'allocated', and returns a pointer to the part of the heap memory you can use. When you call free() that piece of heap memory is unmarked and available for malloc() to use again.
So for your example, you ask for n *sizeof( float ) bytes of the heap. The pointer p points to the first part of the heap you are allowed to use for your array of floats.
Last edited by Duoas; Oct 26th, 2007 at 1:01 am.
http://c-faq.com/malloc/calloc.html
Note in particular that using calloc to initialise your floats to 0.0 is NOT portable.
Also, since this is C++, you should really be using
If this were a C program, I would be telling you NOT to cast the result of malloc.
Note in particular that using calloc to initialise your floats to 0.0 is NOT portable.
Also, since this is C++, you should really be using
p = new float[n];If this were a C program, I would be telling you NOT to cast the result of malloc.
![]() |
Similar Threads
- dynamic memory allocation (C++)
- Problem with memory allocation =( (C)
- Static and Dynamic Memory Allocations...Advan's & Disadvan's??? (Computer Science)
- regarding dynamic memory allocation (C)
- memory allocation ptr to array? how? (C)
- Dynamic memory allocation homework (C++)
Other Threads in the C++ Forum
- Previous Thread: Help: GetDiskFreeSpace in standard library
- Next Thread: c++ graphics
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings struct temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






