General Questions 1 Memory Allocation 1. Which header file should be included to use functions like malloc() and calloc()? A. memory.h B. stdlib.h C. string.h D. dos.h 2. What function should be used to free the memory allocated by calloc()? A. dealloc(); B. malloc(variable_name, 0) C. free(); D. memalloc(variable_name, 0) 3. How will you free the memory allocated by the following program? #define MAXROW 3 #define MAXCOL 4 int **p, i, j; p = (int **) malloc(maxrow * sizeof(int*)); A. memfree(int p); B. dealloc(p); C. malloc(p, 0); D. free(p); Answer: Option D 4. Specify the 2 library functions to dynamically allocate memory? A. malloc() and memalloc() B. alloc() and memalloc() C. malloc() and calloc() D. memalloc() and faralloc()
2 Find Output of Program 1. What will be the output of the program? int *p; p = (int *)malloc(20); /* Assume p has address of 1314 */ free(p); printf("%u", p); A. 1314 B. Garbage value C. 1316 D. Random address 2. What will be the output of the program (16-bit platform)? int *p; p = (int *)malloc(20); printf("%d\n", sizeof(p)); free(p); A. 4 B. 2 C. 8 D. Garbage value 3. What will be the output of the program? #include<string.h> char *s; char *fun(); s = fun(); printf("%s\n", s); char *fun() char buffer[30];
strcpy(buffer, "RAM"); return (buffer); A. 0xffff B. Garbage value C. 0xffee D. Error 3 The output is unpredictable since buffer is an auto array and will die when the control go back to main. Thus s will be pointing to an array, which not exists. 4. What will be the output of the program? 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); A. 10 B. Garbage value C. 10.100000 D. Error 5. Assume integer is 2 bytes wide. How many bytes will be allocated for the following code? #define MAXROW 3 #define MAXCOL 4 int (*p)[maxcol]; p = (int (*) [MAXCOL])malloc(MAXROW *sizeof(*p)); A. 56 bytes B. 128 bytes C. 24 bytes D. 12 bytes
4 6. Assume integer is 2 bytes wide. What will be the output of the following code? #define MAXROW 3 #define MAXCOL 4 int (*p)[maxcol]; p = (int (*) [MAXCOL])malloc(MAXROW *sizeof(*p)); printf("%d, %d\n", sizeof(p), sizeof(*p)); A. 2, 8 B. 4, 16 C. 8, 24 D. 16, 32 7. How many bytes of memory will the following code reserve? int *p; p = (int *)malloc(256 * 256); if(p == NULL) printf("allocation failed"); A. 65536 B. Allocation failed C. Error D. No output Hence 256*256 = 65536 is passed to malloc() function which can allocate upto 65535. So the memory allocation will be failed in 16 bit platform (Turbo C in DOS). If you compile the same program in 32 bit platform like Linux (GCC Compiler) it may allocate the required memory. Point Out Errors
5 1. Point out the error in the following program. int *a[3]; a = (int*) malloc(sizeof(int)*3); free(a); A. Error: unable to allocate memory B. Error: We cannot store address of allocated memory in a C. Error: unable to free memory D. No error We should store the address in a[i] 2. Point out the error in the following program. char *ptr; *ptr = (char)malloc(30); strcpy(ptr, "RAM"); printf("%s", ptr); free(ptr); A. Error: in strcpy() statement. B. Error: in *ptr = (char)malloc(30); C. Error: in free(ptr); D. No error Answer: ptr = (char*)malloc(30); Point Out Correct Statements 1. Point out the correct statement will let you access the elements of the array using 'p' in the following program?
6 int i, j; int(*p)[3]; p = (int(*)[3])malloc(3*sizeof(*p)); A. for(i=0; i<3; i++) for(j=0; j<3; j++) printf("%d", p[i+j]); for(i=0; i<3; i++) B. printf("%d", p[i]); C. for(i=0; i<3; i++) for(j=0; j<3; j++) printf("%d", p[i][j]); for(j=0; j<3; j++) D. printf("%d", p[i][j]); 2. Which of the following statement is correct prototype of the malloc() function in c? A. int* malloc(int); B. char* malloc(char); C. unsigned int* malloc(unsigned int); D. void* malloc(size_t); Answer: Option D 3. Point out the correct statement which correctly free the memory pointed to by 's' and 'p' in the following program? struct ex int i; float j; char *s ; struct ex *p; p = (struct ex *)malloc(sizeof(struct ex));
p->s = (char*)malloc(20); A. free(p);, free(p->s); B. free(p->s);, free(p); C. free(p->s); D. free(p); 7 4. Point out the correct statement which correctly allocates memory dynamically for 2D array following program? 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]); A. p = (int*) malloc(3, 4); B. p = (int*) malloc(3*sizeof(int)); C. p = malloc(3*4*sizeof(int)); D. p = (int*) malloc(3*4*sizeof(int)); Answer: Option D True / False Questions 1. 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. A. True B.False 2. malloc() allocates memory from the heap and not from the stack. A. True B.False
8 3. malloc() returns a NULL if it fails to allocate the requested memory. A. True B.False 4. If malloc() successfully allocates memory it returns the number of bytes it has allocated. A. True B.False Syntax: void *malloc(size_t size); The malloc() function shall allocate unused space for an object whose size in bytes is specified by size and whose value is unspecified. The order and contiguity of storage allocated by successive calls to malloc() is unspecified. The pointer returned if the allocation succeeds shall be suitably aligned so that it may be assigned to a pointer to any type of object and then used to access such an object in the space allocated (until the space is explicitly freed or reallocated). Each such allocation shall yield a pointer to an object disjoint from any other object. The pointer returned points to the start (lowest byte address) of the allocated space. If the space cannot be allocated, a null pointer shall be returned. If the size of the space requested is 0, the behavior is implementation-defined: the value returned shall be either a null pointer or a unique pointer. Yes / No Questions 1. Can I increase the size of dynamically allocated array? A. Yes B.No Use realloc(variable_name, value); 2. Can I increase the size of statically allocated array? A. Yes B.No
3. When we dynamically allocate memory is there any way to free memory during run time? A. Yes B.No Using free() 9