Tuesday, March 12, 2013

Xalloc Vs Yalloc - if X='m' & Y='c'

There can be an interviewer who can throw you a question like:
Xalloc Vs Yalloc - if X='m' & Y='c'

i.e Differentiate between both


Answer:
Now this is a question to test your logical and programming knowledge at once.
PS: If X=m and Y=c, the question is simply to tell difference between malloc and calloc

And hence, the answer is--

1. calloc() zero-initializes the buffer, while malloc() leaves the memory uninitialized.

2. The first letter. The number of arugments each takes. Also, calloc zero-values all elements it allocates. 


Description:


void *malloc(size_t size); 

The function allocates an object of size_t size (size_t = int, char, float...etc), and returns the address of the object if successful; otherwise, it returns a null pointer. The values stored in the object are indeterminate. You can safely convert the return value to an object pointer of any type whose size is not greater than size.

void *calloc(size_t nelem, size_t size); 

The function allocates an array object containing nelem (number of elements) each of size_t size, stores zeros in all bytes of the array, and returns the address of the first element of the array if successful; otherwise, it returns a null pointer. You can safely convert the return value to an object pointer of any type whose size in bytes is not greater than size.


Read more on Internet if you like

No comments:

Post a Comment