GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C++
Go To First  |  Previous Question  |  Next Question 
 C++  |  Question 170 of 203    Print  
Memory Allocation
How much memory is allocated with the statements below?
(i) short* ps = (short*) malloc (100);
(ii) short* ps = (short*) new short (100);



  
Total Answers and Comments: 4 Last Update: September 08, 2009     Asked by: prettyfox 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: rocky2583
 
The first statement
short* ps = (short*) malloc (100);
will allocate 100 bytes of memory.

while the second statement
short* ps = (short*) new short (100);

will allocate (assuming short is 2 bytes) 200 bytes of memory.

There is no need to typecast the pointer returned by new operator, coz new automatically returns pointer to desired type, unlike malloc which returns void pointer.


Above answer was rated as good by the following members:
yzesong, j_l_larson, thumbeti
July 19, 2008 13:52:40   #1  
rocky2583 Member Since: July 2008   Contribution: 3    

RE: Memory Allocation
The first statement
short* ps (short*) malloc (100);
will allocate 100 bytes of memory.

while the second statement
short* ps (short*) new short (100);

will allocate (assuming short is 2 bytes) 200 bytes of memory.

There is no need to typecast the pointer returned by new operator coz new automatically returns pointer to desired type unlike malloc which returns void pointer.

 
Is this answer useful? Yes | NoAnswer is useful 2   Answer is not useful 2Overall Rating: -N/A-    
September 29, 2008 02:37:09   #2  
hrisheekesh.kale27 Member Since: September 2008   Contribution: 4    

RE: Memory Allocation
The first statement will allocate 100 bytes.

The second one will allocate 2 bytes (one short) and call the constructor with arg as 100.
This will initialize the memory to 100.

 
Is this answer useful? Yes | NoAnswer is useful 3   Answer is not useful 0Overall Rating: +3    
December 07, 2008 09:22:07   #3  
burraganesh Member Since: December 2008   Contribution: 20    

RE: Memory Allocation
The first statement will allocate 100 bytes on heap the memory locations will have garbage values.

The second statement will allocate 2 bytes on heap and initialise the allocated region with 100.


 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 1Overall Rating: -N/A-    
September 08, 2009 07:32:38   #4  
k.senthilvelan Member Since: September 2009   Contribution: 1    

RE: Memory Allocation
short* ps (short*) malloc (100);
malloc will allocate 100 bytes and then convert it to short type...


short* ps1 (short*) new short (100);
this creates a memory of size short and assign the value 100 to *ps1.

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape