for structures, whenever we define a variable, why do we use malloc function to asign it space?? why doesnt the space gets created on its own as soon as we define the variable??...for other data types like int,char..we dont have to asign space to them..it happens on its own!! Why does not it apply to structures??
If you declare an object of a structure space does get reserved for it.
But if you declare a pointer to the structure then you need to allocate space.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
float one, two;
struct someStruct anObject; //Don't need malloc or new here
struct someStruct *aPointer; //Space allocated for the pointer itself but not for the struct
aPointer = (struct someStruct *) malloc( sizeof( someStruct ) ); //space allocated for the object of the struct, pointed to by aPointer