C Programming

C Programming

Memory Allocation has 24 Questions

Which header file should be included to use functions like malloc() and calloc()?

memory.h
stdlib.h
string.h
dos.h
What will be the output of the program?
#include<stdio.h>
#include<stdlib.h>

int main()
{
    int *p;
    p = (int *)malloc(20); /* Assume p has address of 1314 */
    free(p);
    printf("%u", p);
    return 0;
}

1314
Garbage value
1316
Random address
Point out the error in the following program.
#include<stdio.h>
#include<stdlib.h>

int main()
{
    int *a[3];
    a = (int*) malloc(sizeof(int)*3);
    free(a);
    return 0;
}

Error: unable to allocate memory
Error: We cannot store address of allocated memory in a
Error: unable to free memory
No error
Point out the correct statement will let you access the elements of the array using 'p' in the following program?
#include<stdio.h>
#include<stdlib.h>

int main()
{
    int i, j;
    int(*p)[3];
    p = (int(*)[3])malloc(3*sizeof(*p));
    return 0;
}

for(i=0; i<3; i++)
{
    for(j=0; j<3; j++)
        printf("%d", p[i+j]);
}
for(i=0; i<3; i++)
    printf("%d", p[i]);
for(i=0; i<3; i++)
{
    for(j=0; j<3; j++)
        printf("%d", p[i][j]);
}
for(j=0; j<3; j++)
    printf("%d", p[i][j]);
malloc() returns a float pointer if memory is allocated for storing float's and a double pointer if memory is allocated for storing double's.

True
False
Can I increase the size of dynamically allocated array?

Yes
No
What function should be used to free the memory allocated by calloc() ?

dealloc();
malloc(variable_name, 0)
free();
memalloc(variable_name, 0)
What will be the output of the program (16-bit platform)?
#include<stdio.h>
#include<stdlib.h>

int main()
{
    int *p;
    p = (int *)malloc(20);
    printf("%d", sizeof(p));
    free(p);
    return 0;
}

4
2
8
Garbage value
Point out the error in the following program.
#include<stdio.h>
#include<stdlib.h>

int main()
{
    char *ptr;
    *ptr = (char)malloc(30);
    strcpy(ptr, "RAM");
    printf("%s", ptr);
    free(ptr);
    return 0;
}

Error: in strcpy() statement.
Error: in *ptr = (char)malloc(30);
Error: in free(ptr);
No error
Which of the following statement is correct prototype of the malloc() function in c ?

int* malloc(int);
char* malloc(char);
unsigned int* malloc(unsigned int);
void* malloc(size_t);
malloc() allocates memory from the heap and not from the stack.

True
False
Can I increase the size of statically allocated array?

Yes
No
How will you free the memory allocated by the following program?
#include<stdio.h>
#include<stdlib.h>
#define MAXROW 3
#define MAXCOL 4

int main()
{
    int **p, i, j;
    p = (int **) malloc(MAXROW * sizeof(int*));
    return 0;
}

memfree(int p);
dealloc(p);
malloc(p, 0);
free(p);
What will be the output of the program?
#include<stdio.h>

int main()
{
    char *s;
    char *fun();
    s = fun();
    printf("%s", s);
    return 0;
}
char *fun()
{
    char buffer[30];
    strcpy(buffer, "RAM");
    return (buffer);
}

0xffff
Garbage value
0xffee
Error
Point out the correct statement which correctly free the memory pointed to by 's' and 'p' in the following program?
#include<stdio.h>
#include<stdlib.h>

int main()
{
    struct ex
    {
        int i;
        float j;
        char *s
    };
    struct ex *p;
    p = (struct ex *)malloc(sizeof(struct ex));
    p->s = (char*)malloc(20);
    return 0;
}

free(p); , free(p->s);
free(p->s); , free(p);
free(p->s);
free(p);
malloc() returns a NULL if it fails to allocate the requested memory.

True
False
When we dynamically allocate memory is there any way to free memory during run time?

Yes
No
Specify the 2 library functions to dynamically allocate memory?

malloc() and memalloc()
alloc() and memalloc()
malloc() and calloc()
memalloc() and faralloc()
What will be the output of the program?
#include<stdio.h>
#include<stdlib.h>

int main()
{
    union test
    {
        int i;
        float f;
        char c;
    };
    union test *t;
    t = (union test *)malloc(sizeof(union test));
    t->f = 10.10f;
    printf("%f", t->f);
    return 0;
}

10
Garbage value
10.100000
Error
Point out the correct statement which correctly allocates memory dynamically for 2D array following program?
#include<stdio.h>
#include<stdlib.h>

int main()
{
    int *p, i, j;
    /* Add statement here */
    for(i=0; i<3; i++)
    {
        for(j=0; j<4; j++)
        {
            p[i*4+j] = i;
            printf("%d", p[i*4+j]);
        }
    }
    return 0;
}

p = (int*) malloc(3, 4);
p = (int*) malloc(3*sizeof(int));
p = malloc(3*4*sizeof(int));
p = (int*) malloc(3*4*sizeof(int));
If malloc() successfully allocates memory it returns the number of bytes it has allocated.

True
False
Assume integer is 2 bytes wide. How many bytes will be allocated for the following code?
#include<stdio.h>
#include<stdlib.h>
#define MAXROW 3
#define MAXCOL 4

int main()
{
    int (*p)[MAXCOL];
    p = (int (*) [MAXCOL])malloc(MAXROW *sizeof(*p));
    return 0;
}

56 bytes
128 bytes
24 bytes
12 bytes
Assume integer is 2 bytes wide. What will be the output of the following code?
#include<stdio.h>
#include<stdlib.h>
#define MAXROW 3
#define MAXCOL 4

int main()
{
    int (*p)[MAXCOL];
    p = (int (*) [MAXCOL])malloc(MAXROW *sizeof(*p));
    printf("%d, %d", sizeof(p), sizeof(*p));
    return 0;
}

2, 8
4, 16
8, 24
16, 32
How many bytes of memory will the following code reserve?
#include<stdio.h>
#include<stdlib.h>

int main()
{
    int *p;
    p = (int *)malloc(256 * 256);
    if(p == NULL)
        printf("Allocation failed");
    return 0;
}

65536
Allocation failed
Error
No output