C Programming Language – Interview Questions & Answers (Page -4)
Pointer & Dynamic Memory Allocation(DMA)
Explain the concept of Dynamic Memory Allocation (DMA)?
stands for Dynamic Memory Allocation
, DMA allows us to allocate memory at run time. Generally variable allocates the memory at compile time, for example int a; here a will allocate memory at compile time. But in case of Dynamic Memory Allocation, memory allocates at run time. Using the concept of DMA you can allocate exact memory for the variables.
Here are the functions that are used to allocate or free the memory at run time:
: To allocate dynamic memory for one dimensional array i.e. contiguous memory allocation.
: To allocate dynamic memory for two dimensional array i.e. memory allocates in row and column manner.
: To re allocate dynamic memory.
: To free dynamically allocated memory.
What is the difference between static & Dynamic Memory Allocations?
Compiler allocates the memory for the declared variable at compiler time, it is known as compile time memory allocation or static memory allocation. For example, if you declare a variable int num; the memory for num will be declared at compile time. The variables which occupy memory statically are stored in stack and data segment.
While in Dynamic Memory Allocation, memory allocates at run time, if you use malloc(), calloc() functions to declare memory, memory allocation is known as DMA. Dynamic Memory Allocation is done in heap segment.
What is the difference between malloc() and calloc(), explain with the syntax/examples?
malloc() and calloc()
both allocates the memory at the run time (dynamic), these functions are very useful to allocated dynamic memory, when you are dealing with a large amount of data or want to create memory according your need at run time, for example if you want to create memory for N number of students then these functions are needed.
allocates the memory in single block according to given size, while calloc()
allocates in multiple blocks according to given size.
Here, malloc() allocates the memory for 10 integers and returns the address, which is getting store in ptr.
Here, calloc() allocates the 10 (row) memory blocks of 20 bytes (5*sizeof(int) and returns the address, which is getting store in ptr.
What are the return type of malloc() and calloc(), how can we use?
calloc() and malloc() bothe functions returns void*
(void pointer), to use/ capture the returned value in pointer variable we convert it’s type. Suppose we create memory for 10 integers then we convert it into int*.
Here, malloc() will return void* and ptr variable is int* type, so we are converting it into (int*).
What is memory leak in C?
Memory leak is really a panic, the insufficient memory/ memory resources leaks are know as memory leaks. Memory leak occurs when program does not manage the memory allocation correctly.
Consider the following examples:
//returned pointer points to text which has gone out of scope;
//it might cause a Segmentation fault.
char *ptr = (char*)malloc(5*sizeof(char));
ptr = 'a';
//invalid access through dangling pointer! It might cause a Segmentation fault.
What preprocessors do?
Preprocessors change the code based on applied pre processor directives like #include, #define.. etc, It modifies the code before compilation.
What is a Macro?
Macro is preprocessor directive, which is used to replace tokens/text in the code at compile time. Compiler will replace a macro's name with its value.
#define DEFAULT_CITY “Mumbai”
DEFAULT_CITY macro will be changed (replaced) with “Mumbai” at the time of compilation.
How can you avoid including a header file multiple times?
It is a common error when you call a header file more than once time, function/variable multiple defined error occur, here is an example by using this you can avoid multiple times code inclusion.
// code of the file
// code of the file
if _MY_HDR_H is not defined, then it will be defined and code will execute otherwise compiler will avoid the source code written in #ifndef and #endif.
What is file?
is a collection of bytes
that are stored in secondary memory like Hard Disk, CD, Pen Drive (Disk type devices). Here collection of bytes represents that data which are storing in memory (in the form of bytes).
There are two types of files Text files
& Binary Files
What are streams?
are sequence of bytes of data
; it can be in both directions. If data is passing to program, it is called input stream
and if data is returning (printing) to output device, it is called output stream
What is FILE (FILE POINTER)?
(capital case) is a structure, which is declared in stdio.h
, a FILE*
is a pointer of type FILE which is used to store address of a file.
FILE *fp; // declaration
printf("\nFile opening error!!!");
printf("\nFile opened successfully,");
is using to open “ok.txt” file in read mode. If file opened successfully, fopen return memory address of “ok.txt” file to fp, and fp is a FILE*. If file does not open successfully, fopen returns NULL to fp.