Q:

Why Heap memory exist? 

I was reading about Stack and heap memory that how, the stack memory stores automatic variables and the heap is used to dynamically allocate memory with the help of pointers, But.  I have a minor confusion regarding why, even heap memory exist, why not, whole the memory is comprised of code segment and stack only, I know that's a weird question but, if that would have been the case, there would also be very less errors and also no concept of memory leakage will be there.  Can, someone give a valid reason for this question. 

A:

The heap allows objects to have lifetimes of uncertain duration, as well as allows for allocations of uncertain sizes.  That is a lifetime or size that can not be determined at compile time. 
For example, say you are working on a word processor.  At the time you are compiling the program you have no idea how large or small any particular file is going to become, nor how long the user is going to want that file open.  Placing objects related to open files on the heap allows you to easily solve both of these issues. 
Without a heap you would be reduced to simply specifying a maximum size at compile time and hope it is enough and hope that the machine that is eventually going to run the program has enough memory to support the maximum file size.