CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    std::vector::reserve()

    This actual object type is more complicated but I still see the problem, even with a simple int -so here goes:-

    Code:
    std::vector<int> vect;
    int capacity = vect.capacity(); // capacity == 0
    int* contents = &vect[0];       // asserts
    The above code asserts when built with MSVC (and presumably also with gcc). I guess that's understandable because all we've done is to declare a vector - BUT...

    Code:
    std::vector<int> vect;
    vect.reserve(3);
    int capacity = vect.capacity(); // capacity is now 3
    int* contents = &vect[0];       // still asserts
    With gcc, std::vector::reserve() apparently does reserve (and assign) some actual memory - but for MSVC it doesn't. Is there something similar to reserve() that'll reserve (i.e. assign) some actual memory?
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: std::vector::reserve()

    The assert is correct even if reserve is used. All reserve() does is to allocate memory (which MSVC does). It doesn't add any elements. If you just reserve and don't add any elements, then .size() will be zero and hence accessing [0] is undefined behaviour (which can change between compilers). The capacity of a vector is different to its size. The only criteria is that capacity() >= size(). The difference between capacity() and size() is the number of elements that can be pushed on to the vector without memory re-allocation happening. Using .reserve() just prevents re-allocation until that number of elements have been added. resize() allocates memory as needed and sets .size() to the given size. Only elements from 0 to .size() - 1 are accessible. Elements from .size() to capacity() - 1 are not accessible. Consider:

    Code:
    std::vector<int> vect;
    vect.reserve(3);
    
    int* contents = vect.empty() ? nullptr : &vect[0];
    If you always want [0] to exist, then you need to re(size) rather than reserve.

    Code:
    std::vector<int> vect;
    vect.resize(3);
    int* contents = &vect[0];       // now OK as has 3 elements
    or

    Code:
    std::vector<int> vect(3);
    int* contents = &vect[0];       // now OK as has 3 elements
    Last edited by 2kaud; March 2nd, 2021 at 07:36 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: std::vector::reserve()

    [Moved to new thread...]
    Attached Images Attached Images  
    Last edited by John E; March 2nd, 2021 at 08:34 AM.
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  4. #4
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: std::vector::reserve()

    John, please don't ask different questions in the same post. It's better to start a new post for each new question.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured