Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8458259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:01:46+00:00 2026-06-10T13:01:46+00:00

C# coder just wrote this simple C++ method to get text from a file:

  • 0

C# coder just wrote this simple C++ method to get text from a file:

static std::vector<std::string> readTextFile(const std::string &filePath) {
    std::string line;
    std::vector<std::string> lines;
    std::ifstream theFile(filePath.c_str());
    while (theFile.good()) {
    getline (theFile, line);
        lines.push_back(line);
    }
    theFile.close();
    return lines;
}   

I know this code is not efficient; the text lines are copied once as they are read and a second time when returned-by-value.

Two questions:

(1) can this code leak memory ?
(2) more generally can returning containers of objects by value ever leak memory ? (assuming the objects themselves don’t leak)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T13:01:47+00:00Added an answer on June 10, 2026 at 1:01 pm
    while (theFile.good()) {
     getline (theFile, line);
        lines.push_back(line);
    }
    

    Forget about efficiency, this code is not correct. It will not read the file correctly. See the following topic to know why:

    • What's preferred pattern for reading lines from a file in C++?

    So the loop should be written as:

    while (getline (theFile, line)) {
        lines.push_back(line);
    }
    

    Now this is correct. If you want to make it efficient, profile your application first. Try see the part which takes most CPU cycles.


    (1) can this code leak memory ?

    No.

    (2) more generally can returning containers of objects by value ever leak memory ?

    Depends on the type of the object in the containers. In your case, the type of the object in std::vector is std::string which makes sure that no memory will be leaked.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just wrote some code to test the behavior of std::equal, and came away
In Visual Studio, I just created a simple Unit Test to test a method
This is probably something simple, but I just can't see it. I've got my
Below is a simple method I wrote (extremely simplified down so I hope it
I'm just mucking around with C as a learner, and wrote this little function
I'm just wondering if there is a best case to write this code: $('#set_duration_30').click(function(event)
This is just a question on how to write my code for a COM
How do you guys write comments on some simple code that you use just
This question spans both serverfault and stackoverflow so I just picked this one. I
I'm currently using below method to call an external php file with some variables

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.