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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:54:47+00:00 2026-05-16T03:54:47+00:00

I have a std::vector of values for which I know the maximum size, but

  • 0

I have a std::vector of values for which I know the maximum size, but the actual size will vary during usage:

void setupBuffer(const size_t maxSize) {
  myVector.reserve(maxSize);
}

void addToBuffer(const Value& v) {
  myVector.push_back(v);

  if (myVector.size() == maxSize) {
    // process data...
    myVector.clear();
  }
}

However, in setupBuffer, I need to obtain a pointer to the start of myVector’s data. I’m using a third party library where I must cache this pointer up front for use in a call made during the “process data…” section.

void setupBuffer(const size_t maxSize) {
  myVector.reserve(maxSize);

  cachePtr(&(myVector[0])); // doesn't work, obviously
}

I don’t want to resize() the vector up front, as I want to use vector.size() to mean the number of elements added to the vector.

So, is there any way to obtain the pointer to the vector’s buffer after allocation (reserve()) but before it has any elements? I would imagine the buffer exists (and won’t move as long as I restrict the number of push_back’d values)….maybe this isn’t guaranteed?

  • 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-16T03:54:48+00:00Added an answer on May 16, 2026 at 3:54 am

    The vector buffer will not be moved after a call to reserve unless you exceed the reserved capacity. Your problem is getting the pointer to the first element. The obvious answer is to push a single fake entry into the vector, get the pointer to it, and then remove it.

    A nicer approach would be if the library accepted a functor rather than a pointer, which it would call when it needed to access the buffer – you could make the functor put off getting the address until the buffer had some real contents. However, I realise you don’t have the luxury of rewriting the library.

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

Sidebar

Related Questions

i have a std::vector<int> and a second container holding iterators or indexes (no keys,
I have a typedef boost::variant<int, float, double, long, bool, std::string, boost::posix_time::ptime> variant which I
Hello I have a function which signature is std::string f(double x, double param1, double
I have the following code, which does some iterator arithmetic: template<class Iterator> void Foo(Iterator
I have the following code, which loops through an array of menuoptions and on
I have a program that heavily uses std::map . Under Windows, much more memory
A problem of value types with external resources (like std::vector<T> or std::string ) is
I have the following function, which generates an integer in a given range: 18
Wow I've been all over the place with questions today and I apologize if
I'm currently trying to implement the A* pathfinding algorithm using C++. I'm having some

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.