C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » C programming language

C Interview Questions & Answers (Page -4)

Pointer & Dynamic Memory Allocation(DMA)


41) Explain the concept of Dynamic Memory Allocation (DMA)?

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:

malloc(): To allocate dynamic memory for one dimensional array i.e. contiguous memory allocation.

calloc(): To allocate dynamic memory for two dimensional array i.e. memory allocates in row and column manner.

realloc(): To re allocate dynamic memory.

free(): To free dynamically allocated memory.

42) 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. malloc(), calloc() and realloc() functions are used to declare memory dynamically. Dynamic Memory Allocation is done in heap segment.






43) What is the difference between malloc() and calloc(), explain with the syntax/examples?

malloc() and calloc() both allocate 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.

malloc() allocates the memory in single block according to given size, while calloc() allocates in multiple blocks according to given size.

	    int num=10;
	    int *ptr;
	    ptr=(int*)malloc(num*sizeof(int));

Here, malloc() allocates the memory for 10 integers and returns the address, which is getting store in ptr.

    int row=10,col=5;
	    int *ptr;
	    ptr=(int*)calloc(row,col*sizeof(int));

Here, calloc() allocates the 10 (row) memory blocks of 20 bytes (5*sizeof(int) and returns the address, which is getting store in ptr.

44) What are the return type of malloc() and calloc(), how can we use?

malloc() and calloc() both functions return void* (a 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 have to convert it into int*.

    int *ptr;
    ptr=(int*)malloc(N*sizeof(int));
Here, malloc() will return void* and ptr variable is int* type, so we are converting it into (int*).

45) 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:

Example 1)

    char* myFunction()
    {
        char text[20];
        strcpy(text,"Hello, world!");
        return(test); 
    }
    //returned pointer points to text which has gone out of scope; 
    //it might cause a Segmentation fault.

Example 2)

    char *ptr = (char*)malloc(5*sizeof(char));
    free(ptr);
    ptr[1] = 'a'; 
    //invalid access through dangling pointer! 
    //It might cause a Segmentation fault.

46) What preprocessors do?

Preprocessors change the code based on applied pre processor directives like #include, #define.. etc, It modifies the code before compilation.

47) What is a Macro?

Macro is a 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.

48) 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.

#ifndef  _MY_HDR_H
#define _MY_HDR_H
	// code of the file
	// code of the file
#end if
    

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.

49) What is file and streams?

A 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.

Streams

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.

50) What is FILE (FILE POINTER)?

FILE (capital letters) is a structure, which is declared in stdio.h, a FILE* is a pointer of type FILE which is used to store memory address of a file.

Example

    FILE *fp;	// declaration
    fp=fopen("ok.txt", "r");

    if(fp==NULL)
    {
        printf("\nFile opening error!!!");
        exit(1);
    }
    printf("\nFile opened successfully,");

Here, fopen is using to open "ok.txt" file in read mode. If file opened successfully, fopen returns the memory address of "ok.txt" file to fp, where fp is a FILE* (FILE Pointer). If file does not open successfully, fopen returns NULL to fp.