C Programming - Memory Allocation - Discussion
Discussion Forum : Memory Allocation - Point Out Errors (Q.No. 1)
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;
}
Answer: Option
Explanation:
We should store the address in a[i]
Discussion:
17 comments Page 1 of 2.
Rohit said:
6 years ago
We can't change the base address of an array because it is the constant with the help of which the compiler refers to all the elements held by the array. Note that an array with N elements holds a contiguous space in memory for N elements. This array does not hold any space for storing the address of the first element (base address) somewhere (unlike a pointer which reserves memory for storing an address).
The compiler can not allow the programmer to change this to any other address without losing the memory allotted to the array. Consider the base address to be equivalent to a special kind of constant pointer which points to the first element of the array.
The compiler can not allow the programmer to change this to any other address without losing the memory allotted to the array. Consider the base address to be equivalent to a special kind of constant pointer which points to the first element of the array.
Krushikesh said:
8 years ago
Basically all size of 'a' or size of 'pointer' wont matter here.
A Simple explanation why given program won't work is 'a' is alias of an array which is to be treated as a constant pointer.
Thus we can change the value of a.
Similar example.
char s[] ="hello";// intialize
s = "wont work"; //----> wont work bcoz s is alias of array and treated as const pointer.
A Simple explanation why given program won't work is 'a' is alias of an array which is to be treated as a constant pointer.
Thus we can change the value of a.
Similar example.
char s[] ="hello";// intialize
s = "wont work"; //----> wont work bcoz s is alias of array and treated as const pointer.
Robert said:
9 years ago
int *a[3] = an array of integer pointers.
If you try to a = (int *)malloc(sizeof(int) * 3);
Remember a = a[0] = base address of the array of pointers, already assigned.
You can't assign there anything else.
If you want to point something to a memory address, you can always use one of the pointers from the array, so this one becomes true:
a[0] = (int *)malloc(sizeof(int) * 3)
If you try to a = (int *)malloc(sizeof(int) * 3);
Remember a = a[0] = base address of the array of pointers, already assigned.
You can't assign there anything else.
If you want to point something to a memory address, you can always use one of the pointers from the array, so this one becomes true:
a[0] = (int *)malloc(sizeof(int) * 3)
Amrendra said:
8 years ago
Correct me, if I am wrong.
Here int *p[3], the precedence of [] array subscript is higher than * so it is array which return integer pointer which is wrong so correct form is---> int (*p)[3] () this operator has higher precedence and now we can assign size in p.
Here int *p[3], the precedence of [] array subscript is higher than * so it is array which return integer pointer which is wrong so correct form is---> int (*p)[3] () this operator has higher precedence and now we can assign size in p.
Yash said:
8 years ago
For @Amrendra.
int *p[3] is equivalent to (int *) p[3], which means that array p can store three integer pointers.
What you are talking is:
int (*p)[3] : this means that p is a pointer to an array of 3 integer elements.
These are both opposites of each other.
int *p[3] is equivalent to (int *) p[3], which means that array p can store three integer pointers.
What you are talking is:
int (*p)[3] : this means that p is a pointer to an array of 3 integer elements.
These are both opposites of each other.
(1)
RAJU said:
1 decade ago
*a[3] is an array of pointers. An array a contains address of independent variable or another arrays. we cannot store address of allocated memory in a.we have to store address of allocated memory in a[i] i.e subscript we have to mention.
Sheetal said:
1 decade ago
Here a gives the address of first address element in array, i.e., a is pointer to a pointer and we are assigning it to a pointer (here int*) , so we get an error. We can assign any element of a which is an int pointer.
Balaji said:
1 decade ago
No matter whether it is integer pointer or pointer to a pointer or, pointer to pointer to a pointer and so. All the pointers are having same type value(i.e. integer value) right? then why can't we assign the address?
Giovanni said:
1 decade ago
The correct code should be this:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *a[3];
*a = (int*) malloc(sizeof(int)*3);
free(*a);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *a[3];
*a = (int*) malloc(sizeof(int)*3);
free(*a);
return 0;
}
Jayram parmar said:
9 years ago
Option b is right.
Because in C language, we define any variable like this
int g,l;
or not define int*a{3}
One more thing in c all variable start with any character or _.
Because in C language, we define any variable like this
int g,l;
or not define int*a{3}
One more thing in c all variable start with any character or _.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers