CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2009
    Posts
    82

    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.

  2. #2
    Join Date
    Apr 2008
    Posts
    725

    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.

  3. #3
    Join Date
    Mar 2009
    Posts
    82

    Re: several conceptual questions

    Quote Originally Posted by Amleto View Post
    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?

  4. #4
    Join Date
    Aug 2007
    Posts
    858

    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.

  5. #5
    Join Date
    Mar 2009
    Posts
    82

    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?

  6. #6
    Join Date
    Apr 2005
    Posts
    107

    Re: several conceptual questions

    Quote Originally Posted by StGuru View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured