C++/Object Instantiation vs. Dynamic Memory Allocation
Say you have class A, you can instantiate an object of class A like:
And then you can do stuff with that class (eg, a.method(), etc.)
But you can also do:
A a = new A();
And do stuff with that, and delete it with:
My question: What's the difference between the two?
Also sometimes i get a segmentation fault when i am not using new operator..and sometimes i dont ..what is the significance behind this?
The difference between these two types of allocation are where the memory comes and how longs it sticks around.
First, let's discuss the two places that memory can come from. You can allocate memory from the heap or from the stack. The stack is where all your regular variables come from. When you simply declare a variable -- be it an integer, a floating point, a boolean, a class, a struct, whatever -- it is allocated on the stack:
You can, however, dynamically allocate memory, which pulls the memory from the heap:
int* heapInt = new int;
bool* heapBool = new bool;
float* heapFloat = new float;
The stack is a small, fixed-size block of memory dedicated to your program. All of the local variables, function parameters, etc. exist in the stack memory. The heap is the larger pool of available memory that is shared between all running applications. Basically, the heap is the entirety of free memory during program execution.
Another important distinction between dynamic and non-dynamic variables is that dynamic memory must be deleted. The memory will be allocated to your running program until you call delete on the address. Local variables, on the other hand, are deallocated as soon as they fall out of scope:
int localInt = 3;
int* dynamicInt = new int;
*dynamicInt = localInt;
At the end of the above code block, the localInt variable is automatically deallocated because it is a stack-based variable. The dynamicInt variable, though, is not deallocated, meaning memory is leaked (because we are not maintaining a copy of the pointer, so we cannot delete the allocated memory).
So, that covers the basic differences between heap-based and stack-based variable allocation. The most puzzling part of your question is about the seg fault. However, you may have already started to figure out what causes the seg fault you're seeing.
When you create an instance of a class on the stack, you are requesting a block of memory from the stack that has to hold the entire class. If the class is a large class, then you end up taking a large chunk from your stack. Because the stack is generally small, and its size cannot change at runtime, you can end up running out of stack memory. When this happens, many operating systems will throw a seg fault error. The solution is to, instead, allocate any large chunks of memory on the heap. If you need a large string, it's a good idea to allocate it on the heap. If you need a large class, it's a good idea to allocate it on the heap.
OK, hopefully that clears up all of your questions. If you have any further questions, please do not hesitate to ask. I'm here to help. :)