0

EDIT: Problem solved. @sth asked whether or not I was forgetting to delete the object I might have created with "new". He was right! I was forgetting it. And fixing that indeed fixed the issue. I guess it's something that Visual Studio was doing for me and Linux wasn't.

EDIT 2: Code is removed due to this being a school project and potential issues of plagiarism even though I wrote the code myself. Also the issue seems to be unrelated to the specifics of my code and more of a general c++ use case of new and delete.

Exact same code behaves differently on Visual Studio 2013 (installed on Windows) and ssh server of my school in which I use g++ to compile the code. Code compiles without an error and works as intended except for 1 part. It writes 1 line less when writing the contents of an array to a file line by line.

If the array has 10 elements Visual Studio compiled code creates the "sorted.txt" file with 10 lines and all of them hold exactly 1 integer. No empty lines. Ssh compiled code still creates 10 lines however the last line is empty. So there is actually 1 integer missing in my "sorted.txt" file. My assignment will be compiled on this server when being graded so this might be a problem.

Kenster
  • 23,465
  • 21
  • 80
  • 106
GoktugO
  • 23
  • 4
  • 2
    Does the program somehow get terminated without properly cleaning up the `FileManager` object? – sth Oct 15 '15 at 15:55
  • Run valgrind on Linux. You may found the `FileManager` instance leaking. – timrau Oct 15 '15 at 15:56
  • @sth I'm not sure if I can check that. The code executes without errors and the ssh terminal simply asks me to write new commands. It doesn't give me any information. – GoktugO Oct 15 '15 at 15:56
  • 1
    Please **[edit]** your question with a [mcve] or [SSCCE (Short, Self Contained, Correct Example)](http://sscce.org) – NathanOliver Oct 15 '15 at 15:58
  • Suggestion: Rather than exposing `numberArray` as a public member, make it private and implement `int & operator[](int index){return numberArray[index]}`. – user4581301 Oct 15 '15 at 16:04
  • 1
    Does your program call a function like `exit()`/`abort()` /... to terminate itself? Or do you allocate the `FileManager` with `new`, but never `delete` it? – sth Oct 15 '15 at 16:08
  • @user4581301 I have that code already in the project but it is not being used. I am sorting up to 1 million numbers using different algorithms from a static class so I chose to make the numberArray public in an attempt to gain some time by removing call of two functions each time as the algorithm sorts the array. I am aware that writing the array as private is the preferred way of doing it. – GoktugO Oct 15 '15 at 16:08
  • A function that trivial will be compiled down to the array access by any modern compiler. You lose nothing and gain a better level of encapsulation. – user4581301 Oct 15 '15 at 16:12
  • @user4581301 alright, noted. – GoktugO Oct 15 '15 at 16:13

3 Answers3

2

Your code contains logic to avoid writing a newline at the end of the last line. That's silly, and it feels a bit construed. You need the newline.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • We are given a "data.txt" file which includes randomly placed integers. I am suppose to sort those integers and create a "sorted.txt" file which contains the sorted integers. The original "data.txt" file has no line at the end so I was just trying to mimic that, simply for the presentation. Is there any reason that last line is necessary? – GoktugO Oct 15 '15 at 16:05
  • Why would he *need* the newline? – sth Oct 15 '15 at 16:06
  • `std::endl;` forces a flush of the stream. Alf, should the output stream not flush on close at its destruction? Assuming the program isn't crashing on exit and not destroying. Could be a deeper problem. – user4581301 Oct 15 '15 at 16:07
  • @user4581301: Yes *if* the stream object is destroyed then it's flushed. For the case where it's not destroyed I'm not sure about the C++ level. – Cheers and hth. - Alf Oct 15 '15 at 16:16
1

When I first saw your code, I thought that it was strange that you are closing the stream based on the bool operator of the stream. You can read about what bool operator does Here. And, as described Here, the bool operator can return false even if the stream is still open. Consider closing the stream based on this condition:

if (output.is_open ())

rather than

if (output)
Community
  • 1
  • 1
Algirdas Preidžius
  • 1,769
  • 3
  • 14
  • 17
1

std::ofstream is a buffered stream. If it is not properly closed or flushed, some buffered data might not be written to disk.

In your case the std::ofstream would be cleaned up whenever the containing FileManager is destroyed. So likely that doesn't happen. It might be that the FileManager is dynamically allocated with new and never gets destroyed with delete, or that the program is abnormally terminated, for example with the C library function exit().

Only the last line is affected by this problem since std::endl flushes the stream and forces the previous data to be written to disk.

sth
  • 222,467
  • 53
  • 283
  • 367