Last night, I was writing a program to implement a Stack using Linked Lists which I learnt about recently at school but only had a vague idea about before that. Upon thinking (I might be overthinking this) more about my program, I feel that the compiler should complain "Out of Memory" or something of that sort. Can someone explain me why it isn't the case?
Code:
struct STUDENT
{
int ID;
STUDENT* LINK;
};
int main()
{
STUDENT S;
}
Reason for why I feel this should give an error complaining out of memory is because "S" has a data member "ID" (an integer) and "LINK" which is a pointer of type "STUDENT". Now, "LINK" of "S" also has data members "ID" (an integer) and "LINK" which is a pointer of type "STUDENT" and this keeps going on and on.
What I mean is:
Code:
S.ID=10;
S.LINK->ID=20;
S.LINK->LINK->ID=30;
S.LINK->LINK->LINK->ID=40;
//etc...
//cout << S.ID <<" "<< S.LINK->ID and so on...
/*This way there are basically infinitely many objects of type STUDENT and so as soon as we declare an object of type STUDENT and run the program, the compiler should report an error, shouldn't it?*/