C Programming - Memory Allocation
|
|
|
|
Exercise"When ambition ends, happiness begins."
- (Proverb)
|
| 1. |
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;
}
|
| 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 |
Answer: Option E
Explanation:
We should store the address in a[i]
|
| 2. |
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;
}
|
| A. |
Error: in strcpy() statement. | | B. |
Error: in *ptr = (char)malloc(30); | | C. |
Error: in free(ptr); | | D. |
No error |
Answer: Option B
Explanation:
No answer description available for this question. Let us discuss.
|
|
|