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 7828447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:21:29+00:00 2026-06-02T10:21:29+00:00

I’ve written a function that loads the bytes off a file and returns a

  • 0

I’ve written a function that loads the bytes off a file and returns a FileData struct that contains the byte buffer and the length of the buffer.

I want the buffer to be deleted as soon as it’s consumed and thrown out of scope.

I’m having trouble getting it to compile due to various casting errors. Also, I’m not sure whether the buffer is being moved correctly rather than copied. I don’t mind the FileData struct itself being copied, since it’s maybe 16 bytes at most.

In general, how do you use smart pointers as class/struct fields? Is that even something you’d do?

This is a bit of a nebulous question, I know, but since I’m having some conceptual difficulties with smart pointers in general, I’m hoping that this example will help me in the right direction.

Here’s what I’ve got so far:

struct FileData
{
    unique_ptr<char[]> buf;
    unsigned int len;
};

FileData LoadFile(string filename)
{
    ifstream str;
    str.open(filename, ios::binary);

    str.seekg(0, ios::end);
    auto len = str.tellg();
    str.seekg(0, ios::beg);

    char* buf = new char[len];

    str.read(buf, len);
    str.close();

    FileData d = { unique_ptr<char[]>(buf), len };

    return d;
}

Edit: Since some people are curious about the error message that I get with this current code, here it is:

error C2248: 'std::unique_ptr<_Ty>::unique_ptr' : cannot access private member declared in class 'std::unique_ptr<_Ty>'
  • 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-02T10:21:30+00:00Added an answer on June 2, 2026 at 10:21 am

    Your code is fine, except for one little detail:

    struct FileData
    {
        unique_ptr<char[]> buf;
        <del>unsigned int</del> <ins>streamoff</ins> len;
    };
    

    The reason it doesn’t compile for you is that your compiler does not yet implement the automatic generation of the special move members. In a fully C++11 conforming compiler your FileData would behave as if:

    struct FileData
    {
        unique_ptr<char[]> buf;
        streamoff len;
    
        FileData(FileData&&) = default;
        FileData& operator=(FileData&&) = default;
        FileData(const FileData&) = delete;
        FileData& operator=(const FileData&) = delete;
        ~FileData() = default;
    };
    

    The defaulted move constructor simply move constructs each member (and similarly for the defaulted move assignment).

    When returning d from LoadFile, there is an implicit move that takes place which will bind to the implicitly defaulted move constructor.

    Use of vector<char> or string as others have suggested will also work. But there is nothing wrong with your code as far as C++11 is concerned.

    Oh, I might tweak it like so: I like to get my resources owned as quickly as possible:

    FileData LoadFile(string filename)
    {
        ifstream str;
        str.open(filename, ios::binary);
    
        str.seekg(0, ios::end);
        auto len = str.tellg();
        str.seekg(0, ios::beg);
    
        FileData d = {unique_ptr<char[]>(new char[len]), len};
    
        str.read(d.buf.get(), d.len);
        str.close();
    
        return d;
    }
    

    If you need to explicitly define the FileData move members, it should look like:

    struct FileData
    {
        unique_ptr<char[]> buf;
        streamoff len;
    
        FileData(FileData&& f)
            : buf(std::move(f.buf)),
              len(f.len)
            {
                f.len = 0;
            }
    
        FileData& operator=(FileData&& f)
        {
            buf = std::move(f.buf);
            len = f.len;
            f.len = 0;
            return *this;
        }
    };
    

    Oh, which brings me to another point. The defaulted move members are not exactly correct since they don’t set len to 0 in the source. It depends on your documentation if this is a bug or not. ~FileData() doesn’t require len to reflect the length of the buffer. But other clients might. If you define a moved-from FileData as not having a reliable len, then the defaulted move members are fine, else they aren’t.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

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.