944,029 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4538
  • C++ RSS
Oct 26th, 2007
0

Malloc/Calloc Dynamic Memory Allocation.

Expand Post »
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)
  1. p = (float *) malloc ( n * sizeof( float ));

And whats the difference in malloc and calloc?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
warpstar is offline Offline
15 posts
since Oct 2007
Oct 26th, 2007
0

Re: Malloc/Calloc Dynamic Memory Allocation.

>>And whats the difference in malloc and calloc?
calloc() calls malloc() then initializes the data to all 0s.

The code you posted is allocating an array of n floats
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,953 posts
since Aug 2005
Oct 26th, 2007
0

Re: Malloc/Calloc Dynamic Memory Allocation.

whats the difference in malloc and calloc/?

Click to Expand / Collapse  Quote originally posted by warpstar ...
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)
  1. p = (float *) malloc ( n * sizeof( float ));

And whats the difference in malloc and calloc?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
warpstar is offline Offline
15 posts
since Oct 2007
Oct 26th, 2007
0

Re: Malloc/Calloc Dynamic Memory Allocation.

Quote originally posted by Ancient Dragon ...
>>And whats the difference in malloc and calloc?
calloc() calls malloc() then initializes the data to all 0s.
Please read answers given to you. And don't quote yourself...

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.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Oct 26th, 2007
0

Re: Malloc/Calloc Dynamic Memory Allocation.

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
p = new float[n];

If this were a C program, I would be telling you NOT to cast the result of malloc.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

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: GetDiskFreeSpace in standard library
Next Thread in C++ Forum Timeline: c++ graphics





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


Follow us on Twitter


© 2011 DaniWeb® LLC