how does dealloction with delete work?
Hi,
I'll try to explain my problem. I have made three very simple progams and I have compared the memory size each one takes with cmd "top". My problem is to understand how the function delete works. the first one : int main(int argc, char *argv[]) { int i; cin>>i; return 0; } $top Virtual: 1036 Res: 1032 The second : int main(int argc, char *argv[]) { int* tab = new int[100000]; delete[] tab; int i; cin>>i; return 0; } $top Virtual: 1036 Res: 1032 All memory is deallocated The last: int main(int argc, char *argv[]) { //I have changed only the size of my array of int int* tab = new int[2500]; delete[] tab; int i; cin>>i; return 0; } $top Virtual: 1040 Res: 1036 I have lost 4ko. Can someone explain me why? In some cases all memory is deallocated, and in other not .... :scratch: |
Please, please... post code between [ code ] and [ /code ] tags (without the white spaces).
|
Excuse me :), Irepost my code
Code:
int main(int argc, char *argv[]){int i;cin>>i;return 0;} Virtual:1036 res:1032 Code:
int main(int argc, char *argv[]){int* tab = new int[100000];delete[](tab);int i;cin>>i;return 0;} Virtual:1036 res:1032 All memory is deallocated Code:
int main(int argc, char *argv[]){int* tab = new int[2500];delete[](tab);int i;cin>>i;return 0;} $top Virtual:1040 res:1036 |
I retry with indent
the first one : Code:
int main(int argc, char *argv[]) Virtual: 1036 Res: 1032 The second : Code:
int main(int argc, char *argv[]) Virtual: 1036 Res: 1032 All memory is deallocated The last: Code:
int main(int argc, char *argv[]) Virtual: 1040 Res: 1036 I have lost 4ko. Can someone explain me why? |
Basically, you shouldn't rely on top to tell you whether the memory's been deallocated or not. Your code's fine :)
Alex |
You asked.
new and delete call malloc & free. malloc knows how much memory is allocated to the data segment of your code. If malloc decides more memory is needed it calls brk to extend the size of the data segment by a large amount because calling brk is expensive. Then malloc doles out small pieces of the newly allocated memory as needed. When the image (program running in memory) exits, brk is called again to release the memory. This is why you do not necessarily see any change. malloc holds onto the memory in case it's needed again. Plus, the C optimizer probably never allocated memory in some instances anyway. Why? Because it can see your code did nothing with the variable. Basically, unless you want to be a system programmer, do not worry about it. The C library and the OS take care of everything for you. Plus what you see does not reflect what your program is doing -- it reflects what the OS is doing with your program. If you do want to be a system programmer, learn how to write good code first, anyway. |
Thanks for your reply and for your explanation about new/delete, it explains me why my program allocates memory as needed but the problem of deallocation is not totally clear.
What I really want to do, is to maintain the same quantity of memory taken by my program, with static allocation there is no problem, all memory deallocated is restitued to the system. With dynamic allocation in some cases the memory is freed only for the process and in other case delete deallocates memory for the system. How can I know the real memory that my programs takes? I would be able to track future memory leaks, and know the memory limits of my program :) |
All times are GMT -5. The time now is 02:34 PM. |