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.
Priti Nannavare said:
6 years ago
Array name itself is base address so here they are assigning dynamic allocated memory's address in array name that is in the base address.
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.
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)
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.
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.
Graeme said:
9 years ago
It should be a = (int *)malloc(sizeof(int*) * 3);
The allocated size should be based on the size of the pointer, not int since the array is of pointers.
The allocated size should be based on the size of the pointer, not int since the array is of pointers.
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)
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 _.
Gmdn said:
1 decade ago
Array base address could't never be changed during runtime.
Here "a" is address of array. It can never be changed during runtime.
Instead "*a = malloc" works.
Here "a" is address of array. It can never be changed during runtime.
Instead "*a = malloc" works.
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;
}
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers