Malloc/Calloc Dynamic Memory Allocation.

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2007
Posts: 15
Reputation: warpstar is an unknown quantity at this point 
Solved Threads: 0
warpstar warpstar is offline Offline
Newbie Poster

Malloc/Calloc Dynamic Memory Allocation.

 
0
  #1
Oct 26th, 2007
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.

  1. p = (float *) malloc ( n * sizeof( float ));

And whats the difference in malloc and calloc?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,343
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1458
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Malloc/Calloc Dynamic Memory Allocation.

 
0
  #2
Oct 26th, 2007
>>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
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 15
Reputation: warpstar is an unknown quantity at this point 
Solved Threads: 0
warpstar warpstar is offline Offline
Newbie Poster

Re: Malloc/Calloc Dynamic Memory Allocation.

 
0
  #3
Oct 26th, 2007
whats the difference in malloc and calloc/?

Originally Posted by warpstar View 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.

  1. p = (float *) malloc ( n * sizeof( float ));

And whats the difference in malloc and calloc?
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Malloc/Calloc Dynamic Memory Allocation.

 
0
  #4
Oct 26th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,851
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Malloc/Calloc Dynamic Memory Allocation.

 
0
  #5
Oct 26th, 2007
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
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