Thread: ofstream memory bashing

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    51

    ofstream memory bashing

    I am not sure if this should go in the linux or C++ section, but...

    I am working on a program that creates a ofstream and writes to it. Anyway, upon tracing through the program, we found that when ofstream was called, it trashed some of the memory (and the contents of a char array). Does anyone have any ideas about this?

    ofstream outFile("filename.dat",ios::binary);


    it is on amd64 dual core running suse 10.1


    thanks...

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    This line most definitely doesn't trash anything. We need more code. But most likely, the issue is too complex to diagnose remotely.

    And since, so far, there's nothing Linux-specific here, it should be in C++.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Moved.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    51
    I know it doesn't, however I can watch as the data in memory changes when the call returns. I cannot trace through the sys call to see what is happening.

    As far as the code goes, I really cannot post it. Still, I fail to see how anything else should matter if while going through the debugger, the memory contains one thing, the ofstream call happens and returns and the memory now contains something else.

    I am somewhat novice so I would accept any explaination to the contrary...

    All that being said, what else could it be?

    Thanks all for your time...

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well if you've generally made a mess of memory, then any part of your code could look like it's messing with the data belonging to any other part of the code.

    You're observing an effect, but when it comes to memory corruption, this is very seldom the cause.

    You could try
    gcc -g prog.c -lefence

    Then
    gdb a.out
    run


    With any luck, that should trap on the first instruction which does something 'iffy' with allocated memory (like using it after being freed or stepping off the end).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    std::ofstream by default truncates or overwrites a previously existing file. Use std::ios::app

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    51
    Quote Originally Posted by Salem View Post
    Well if you've generally made a mess of memory, then any part of your code could look like it's messing with the data belonging to any other part of the code.

    You're observing an effect, but when it comes to memory corruption, this is very seldom the cause.

    You could try
    gcc -g prog.c -lefence

    Then
    gdb a.out
    run


    With any luck, that should trap on the first instruction which does something 'iffy' with allocated memory (like using it after being freed or stepping off the end).
    I see what you are saying...I will give it a try. Thanks for you help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 01-13-2008, 02:14 AM
  2. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  3. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  4. Shared Memory - shmget questions
    By hendler in forum C Programming
    Replies: 1
    Last Post: 11-29-2005, 02:15 AM
  5. Using ofstream in private of a class-errors
    By loobian in forum C++ Programming
    Replies: 3
    Last Post: 12-13-2003, 10:06 PM