Memory problem?

hi,

what will happen if u allocate memory using new or malloc but u forget to delete?
You can only free memory allocated with malloc by using free.

Anyway, you get a memory leak, which means there is memory that no programs can use (in the worst case, until system reboot). However, all OSes that I know of will automatically free all memory for you at the end of your program to prevent that kind of problem (although it is good practice to delete/free it anyway).
Good Practice?
O.O
I always thought that it is mandatory for us to delete/free the memory we allocated. And only stack memory is freed by compilers. We are responsible for heap.
What am I missing here?
For example:
1
2
3
4
int main(){
    int a=new int;
    return 0;
}

In this case, it doesn't matter if we delete a or not. The OS will free the memory anyway.
I should mention that sometimes it does matter whether an object is deleted or not. For example, an object may save state to the file system when its destructor is called. Objects allocated in the heap that aren't explicitly deleted by the programmer are freed by the OS, and their destructors aren't called.
Ah. Got it. Thanks helios.
Topic archived. No new replies allowed.