GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

  GeekInterview.com  >  Placement Papers  >  Birlasoft  >  Technical

 Print  |  
Question:  Which one of the following statements allocates enough space to hold an array of 10 integers that are initialized to 0 ?


Answer: a) int *ptr = (int *) calloc(10,sizeof(int));
b) int *ptr = (int *) alloc( 10*sizeof(int));
c) int *ptr = (int *) malloc( 10*sizeof(int));
d) int *ptr = (int *)calloc(10*sizeof(int));

Answered by Ragini rani on 2005-05-13 09:01:56: a is the correct ans Calloc allocates space and initialized them to 0


October 10, 2006 08:34:32 #8
 Santh kumar   Member Since: Visitor    Total Comments: N/A 

RE: Which one of the following statements allocates en...
 

 Hi all, i'd like clear difference b/w those  2 functions.

There are 2 differences.

First, is in the number of arguments. malloc() takes a single argument(memory required in bytes), while calloc() needs 2 arguments(number of variables to allocate memory, size in bytes of a single variable).

Secondly, malloc() doesnot initialize the memory allocated, while calloc() initializes the allocated memory to ZERO.

The difference b/w malloc and calloc are: 1. malloc() allocates byte of memory where as calloc()allocates block of memory.

malloc(s); returns a pointer for enough storage for an object of s bytes. calloc(n,s); returns a pointer for enough contiguous storage for n objects,each of s bytes. The storage is all initialised to zeros.

     

 

Back To Question