I have this class:
tree a; //Ok
tree a; //Segmentation fault
Is there a max limit on the no. of objects we can create?
Well, yes. It's called the stack limit. It depends on how big your stack is. Your stack size here is:
stack_size = sizeof(int) * 2 * 90
If on your platform
sizeof(int) is 4 (which it typically is), you have
4 * 2 * 90 = 720 bytes of stack, which seems very small.
Either way, the correct way of allocating large amounts of objects is on the heap, which typically has more space then the stack:
//std::vector allocates its elements on the heap std::vector<tree> a(1024);
If you can't use
std::vector for some reason, you can still use the old school style dynamic array (not recommended though):
tree* a = new tree;