-
August 10th, 2009, 04:55 PM
#1
several conceptual questions
Hi!
I want to ask you why when I write something in file it deletes everything inside.
For ex.
Code:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream dat;
dat.open("data.txt",ios::out);
dat<<"test"<<endl;
dat.close();
dat.open("data.txt",ios::in);
char c[5];
dat>>c;
cout<<c<<endl;
system("PAUSE");
}
In the start I open Notepad, and write "dfsdfdsfsdfsd".
When, dat<<"test"<<endl was executed, everything inside in the file was replaced with single "test".
(2) Why I must delete dynamically allocated memory, shouldn't compiler need to do it?
Thanks in advance.
-
August 10th, 2009, 05:04 PM
#2
Re: several conceptual questions
1) Because you told it to.
2) How is the compiler meant to know when you want to free the memory?
If you dont want to lose all data in file, use ios::app, I believe.
Last edited by Amleto; August 10th, 2009 at 05:06 PM.
-
August 10th, 2009, 05:34 PM
#3
Re: several conceptual questions
Originally Posted by Amleto
1) Because you told it to.
2) How is the compiler meant to know when you want to free the memory?
If you dont want to lose all data in file, use ios::app, I believe.
Thanks for the reply.
Because its the end of the program. How the compiler know to delete the memory for other variables for example?
-
August 10th, 2009, 06:59 PM
#4
Re: several conceptual questions
Because its the end of the program. How the compiler know to delete the memory for other variables for example?
All memory is (usually) freed at the end of the program, but by the operating system - not by your program.
The issue however is not freeing memory when you're program ends, it's freeing it while it's running. Memory that you allow to leak in your program is lost forever (until your program ends)- it can't be reused in your program or used by any other program. Do that enough, and you'll start to degrade the performance of the user's computer due to lack of memory. Keep doing it even longer and you eventually get crashes and other fun things.
-
August 11th, 2009, 03:54 AM
#5
Re: several conceptual questions
Aah, I understand now. Thank you very much.
So the dynamic memory is used while the program is running, and I can delete it before the program ends, so while system("pause") is keeping the program from end, the memory will be released, right?
-
August 11th, 2009, 11:21 AM
#6
Re: several conceptual questions
Originally Posted by StGuru
Aah, I understand now. Thank you very much.
So the dynamic memory is used while the program is running, and I can delete it before the program ends, so while system("pause") is keeping the program from end, the memory will be released, right?
That depends when you delete. If you delete dynamic memory before your system("pause"), then it will already be released by the time you hit the pause.
If you delete it after, it will be deleted after returning from the pause, and exiting.
System pause does absolutely nothing to your program, except freeze it while spawning an external command shell. Generally, system() calls are frowned upon.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|