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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:19:23+00:00 2026-05-25T03:19:23+00:00

I am new so I more than likely missing something key. I am using

  • 0

I am new so I more than likely missing something key.

I am using std::vector to store data from a ReadFile operation.

Currently I have a structure called READBUFF that contains a vector of byte. READBUFF is then instantiated via a private type in a class called Reader.

class Reader{
public:
void Read();
typedef struct {
std::vector<byte> buffer;
} READBUFF;

private:
READBUFF readBuffer;
}

Within Read() I currently resize the array to my desired size as the default allocator creates a really large vector [4292060576]

void Reader::Read()
{
readBuffer.buffer.resize(8192);
}

This all works fine, but then I got to thinking I’d rather dynamically NEW the vector inline so I control the allocation management of the pointer. I changed buffer to be: std::vector* buffer. When I try to do the following buffer is not set to a new buffer. It’s clear from the debugger that it is not initialized.

void Reader::Read()
{
 key.buffer =  new std::vector<byte>(bufferSize);
}

So then I tried, but this behaves the same as above.

void Reader::Read()
{
std::vector<byte> *pvector = new std::vector<byte>(8192);
key.buffer = pvector;
}

Main first question is why doesn’t this work? Why can’t I assign the buffer pointer to valid pointer? Also how do I define the size of the inline allocation vs. having to resize?

My ultimate goal is to “new up” buffers and then store them in a deque. Right now I am doing this to reuse the above buffer, but I am in essence copying the buffer into another new buffer when all I want is to store a pointer to the original buffer that was created.

 std::vector<byte> newBuffer(pKey->buffer);
 pKey->ptrFileReader->getBuffer()->Enqueue(newBuffer);

Thanks in advance. I realize as I post this that I missing something fundamental but I am at a loss.

  • 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-05-25T03:19:24+00:00Added an answer on May 25, 2026 at 3:19 am

    You shouldn’t be using new in this case. It causes you to have to manage the memory manually, which is never something you should want to do for many reasons1. You said you want to manage the lifetime of the vector by using new; in reality, the lifetime of the vector is already managed because it’s the same as the object that holds it. So the lifetime of that vector is the lifetime of the instance of your Reader class.

    To set the size of the vector before it gets constructed, you’ll have to make a constructor for READBUFF:

    // inside the READBUFF struct (assuming you're using a normal variable, not a pointer)
    READBUFF() { } // default constructor
    READBUFF(int size) : buffer(size) { } // immediately sets buffer's size to the argument 
    

    and use an initialization list in Reader‘s constructor:

    // inside the Reader class
    Reader() : readBuffer(8092) { }
    

    Which will set the readBuffer.buffer‘s size to 8092.


    If you really want to use new just for learning:

    key.buffer =  new std::vector<byte>(bufferSize);
    

    This will work fine, but you shouldn’t be doing it in the Read function, you should be doing it in the object’s constructor. That way any member function can use it without having to check if it’s NULL.

    as the default allocator creates a really large vector [4292060576]

    No, it doesn’t (if it did, you could have one vector on your entire computer and probably your computer would crash). It incrementally resizes the storage up when you add things and exceed the capacity. Using resize like you are doing is still good though, because instead of allocating a small one, filling it, allocating a bigger one and copying everything over, filling it, allocating a bigger one and copying everything over, etc. you are just allocating the size you need once, which is much faster.

    1 Some reasons are:

    1. You have to make sure to allocate it before anyone else uses it, where with a normal member variable it’s done automatically before your object has a chance to use it.
    2. You have to remember to delete it in the destructor.
    3. If you don’t do the above 2 things, you have either a segfault or a memory leak.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my new job more people are using Python than Perl, and I have
I read that the new delegate method can attach more than one event to
More times than I'd like to admit I've had people new to a project
I am new to prototype and finding it a lot more difficult than jquery.
I am relatively new to using MSpec and as I write more and more
Disclaimer: completely new to Python from a PHP background Ok I'm using Python on
I have a project dealing with video conferencing using the Kinect (or, more likely,
I need add a new user group for mediawiki. The new group has more
New to this library (no more familiar with BeautifulSoup either, sadly), trying to do
The new OS (3.0+) adds more features which speed up the development (like working

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.