Logo C++ Online Judge

C++

TA:
统计

This problem uses classes and destructors. You may leave it until after the object-oriented programming lecture.

LevelDB is a C++ library for storing data. The function below writes some text to a file. A developer proposes removing the explicit Close() call because the file object's destructor already closes it. Review this proposed cleanup.

Status represents success or failure; s.ok() is true on success. Append, Sync, and Close each return a Status. NewWritableFile creates a file object, or returns an error. RemoveFile removes the named file. Slice provides the data to write. These API notes are supplied for this exercise; you need not know how LevelDB stores data.

static Status DoWriteStringToFile(Env* env, const Slice& data,
                                  const std::string& fname, bool should_sync) {
  WritableFile* file;
  Status s = env->NewWritableFile(fname, &file);
  if (!s.ok()) {
    return s;
  }
  s = file->Append(data);
  if (s.ok() && should_sync) {
    s = file->Sync();
  }
  if (s.ok()) {
    s = file->Close();
  }
  delete file;  // Will auto-close if we did not close above
  if (!s.ok()) {
    env->RemoveFile(fname);
  }
  return s;
}
-  if (s.ok()) {
-    s = file->Close();
-  }
   delete file;  // Will auto-close if we did not close above

This is the destructor used by the POSIX implementation. fd_ >= 0 means it still holds an open file. delete file invokes this destructor.

~PosixWritableFile() override {
  if (fd_ >= 0) {
    // Ignoring any potential errors
    Close();
  }
}
Source attribution and license

Source excerpts: LevelDB, util/env.cc and util/env_posix.cc, revision bb74ef739973a70ca9f0d90788a1da89b788c0ad. The proposed deletion is authored for this exercise; it is not a claimed upstream change.

Copyright (c) 2011 The LevelDB Authors. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  • Neither the name of Google Inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

提交

Written answers receive AI rubric assessment.

Up to 3 submission attempts per student. Sign in to see your remaining attempts.

Each submission includes all questions. Review your answers before submitting.

Short explanations are welcome. You may leave answers blank; unanswered questions receive zero credit.

1. Would you accept this as a behavior-preserving cleanup? Leave a short review comment. (100 points)
Drafts are saved in this browser every 30 seconds.