Home Interview Questions and AnswersTechnical Interview Questions and AnswersC Programming C Language Pointer Interview Questions and Answers For Freshers Part-7

c-program-pointers15. Is it better to use malloc() or calloc()?

Both the malloc() and the calloc() functions are used to allocate dynamic memory. Each operates slightly different from the other. malloc() takes a size and returns a pointer to a chunk of memory at least that big:

void *malloc( size_t size );

calloc() takes a number of elements, and the size of each, and returns a pointer to a chunk of memory at least big enough to hold them all:

void *calloc( size_t numElements, size_t sizeOfElement );

There’s one major difference and one minor difference between the two functions. The major difference is thatmalloc() doesn’t initialize the allocated memory. The first time malloc() gives you a particular chunk of memory, the memory might be full of zeros. If memory has been allocated, freed, and reallocated, it probably has whatever junk was left in it. That means, unfortunately, that a program might run in simple cases (when memory is never reallocated) but break when used harder (and when memory is reused).

calloc() fills the allocated memory with all zero bits. That means that anything there you’re going to use as achar or an int of any length, signed or unsigned, is guaranteed to be zero. Anything you’re going to use as a pointer is set to all zero bits. That’s usually a null pointer, but it’s not guaranteed.

The difference between the two is that calloc() returns an array of objects; malloc() returns one object. Some people use calloc() to make clear that they want an array. Other than initialization, most C programmers don’t distinguish between

calloc( numElements, sizeOfElement)

and

malloc( numElements * sizeOfElement)

There’s a nit, though. malloc() doesn’t give you a pointer to an array. In theory (according to the ANSI C standard), pointer arithmetic works only within a single array. In practice, if any C compiler or interpreter were to enforce that theory, lots of existing C code would break. (There wouldn’t be much use for realloc(), either, which also doesn’t guarantee a pointer to an array.)

16. How do you declare an array that will hold more than 64KB of data?

The coward’s answer is, you can’t, portably. The ANSI/ISO C standard requires compilers to handle only single objects as large as (32KB – 1) bytes long.

Why is 64KB magic? It’s the biggest number that needs more than 16 bits to represent it.

For some environments, to get an array that big, you just declare it. It works, no trouble. For others, you can’t declare such an array, but you can allocate one off the heap, just by calling malloc() or calloc().

On a PC compatible, the same limitations apply, and more. You need to use at least a large data model. You might also need to call “far” variants of malloc() or calloc(). For example, with Borland C and C++ compilers, you could write

far char *buffer = farmalloc(70000L);

Or with Microsoft C and C++ compilers, you could write

far char *buffer = fmalloc(70000L);

to allocate 70,000 bytes of memory into a buffer. (The L in 70000L forces a long constant. An int constant might be only 15 bits long plus a sign bit, not big enough to store the value 70,000.)

You may also like

Leave a Comment