LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-17-2004, 10:23 AM   #1
Corneflex
LQ Newbie
 
Registered: May 2004
Location: France
Distribution: mandrake 9.2
Posts: 5

Rep: Reputation: 0
Lightbulb 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 ....
 
Old 05-17-2004, 10:53 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Please, please... post code between [ code ] and [ /code ] tags (without the white spaces).
 
Old 05-17-2004, 11:08 AM   #3
Corneflex
LQ Newbie
 
Registered: May 2004
Location: France
Distribution: mandrake 9.2
Posts: 5

Original Poster
Rep: Reputation: 0
Excuse me , Irepost my code

Code:
int main(int argc, char *argv[]){int i;cin>>i;return 0;}
$top
Virtual:1036 res:1032

Code:
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

Code:
int main(int argc, char *argv[]){int* tab = new int[2500];delete[](tab);int i;cin>>i;return 0;}
4ko are not deallocated why?

$top
Virtual:1040 res:1036
 
Old 05-17-2004, 11:12 AM   #4
Corneflex
LQ Newbie
 
Registered: May 2004
Location: France
Distribution: mandrake 9.2
Posts: 5

Original Poster
Rep: Reputation: 0
I retry with indent

the first one :

Code:
int main(int argc, char *argv[])
 { 
 int i;
 cin>>i;
 return 0;
 }
$top
Virtual: 1036 Res: 1032

The second :

Code:
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:
Code:
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?
 
Old 05-17-2004, 02:18 PM   #5
llama_meme
Member
 
Registered: Nov 2001
Location: London, England
Distribution: Gentoo, FreeBSD
Posts: 590

Rep: Reputation: 30
Basically, you shouldn't rely on top to tell you whether the memory's been deallocated or not. Your code's fine

Alex
 
Old 05-17-2004, 02:35 PM   #6
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
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.

Last edited by jim mcnamara; 05-17-2004 at 02:36 PM.
 
Old 05-17-2004, 04:00 PM   #7
Corneflex
LQ Newbie
 
Registered: May 2004
Location: France
Distribution: mandrake 9.2
Posts: 5

Original Poster
Rep: Reputation: 0
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to delete files that won't delete? di11rod Linux - Security 7 10-19-2005 09:14 PM
The program Screen, "delete" key doesn't work TroelsSmit Linux - Newbie 0 10-13-2004 07:55 AM
Tried to delete file as root but it says I don't have permission to delete it! beejayzed Mandriva 23 03-12-2004 02:46 AM
Does delete work? oldpawn LQ Suggestions & Feedback 1 10-27-2001 01:34 AM
delete doesn't work phek LQ Suggestions & Feedback 4 09-25-2001 12:06 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration