Advertisements

Search results for malloc

Example:[1]

            What is the error on the following program, currently it returns garbage value as output?
#include <stdio.h>
#include <string.h>
char *fun();
void main()
{
    char *s;
    char *fun();
    s = fun();
    printf("%s\n", s);
}
char *fun()
{
    char buffer[30];
    strcpy(buffer, "RAM");
    return (buffer);
}
          
Option A:

            Declare buffer as a global variable.
            
Option B:

            Declare char *s outside main function.
            
Option C:

            Include header file stdlib.h.
            
Option D:

            Allocate memory using malloc inside main()
            
Correct Answer: option A
Example:[2]

            ptr is ____ pointer after executing the following program?
#include <stdio.h>  
#include <stdlib.h>
void main()  
{  
   int *ptr = (int *)malloc(sizeof(int));  
   int a = 5;  
   ptr = &a; 
   printf("%d\n", *ptr); 
   free(ptr); 
}
          
Option A:

            NULL pointer
            
Option B:

            Void pointer
            
Option C:

            Dangling pointer
            
Option D:

            None of the above
            
Correct Answer: option C
Example:[3]

            The dangling pointer errors can be avoided by
          
Option A:

            Allocating more memory using malloc
            
Option B:

            Assigning NULL value to the pointer after freeing allocated memory from the pointer
            
Option C:

            Assigning void value to the pointer
            
Option D:

            Deallocating memory from pointer using free.
            
Correct Answer: option B
Example:[4]

            Which of the following is NOT dynamic memory allocation function?
          
Option A:

            malloc
            
Option B:

            free
            
Option C:

            alloc
            
Option D:

            calloc
            
Correct Answer: option C
Example:[5]

            What will be error the following program will face during execution?
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#define BUFSIZE 2
void main() 
{
    char *buf;
    buf = (char *)malloc(sizeof(char)*BUFSIZE);
    strcpy(buf, "Hello world\n");
}
          
Option A:

            Stack overflow
            
Option B:

            Heap overflow
            
Option C:

            Frozen memory
            
Option D:

            Memory leak
            
Correct Answer: option B

 Search Links


 Popular C search MCQs


 Popular C search examples