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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:13:10+00:00 2026-05-23T19:13:10+00:00

I thought that the correct type to use to store the difference between pointers

  • 0

I thought that the correct type to use to store the difference between pointers was ptrdiff_t.

As such, I’m confused by the way that my STL (msvc 2010) implements it’s std::vector::size() function. The return type is size_t (this is mandated by the standard, as far as I understand it) and yet it’s computed as the difference of pointers:

// _Mylast, _Myfirst are of type pointer
// size_type, pointer are inherited from allocator<_Ty>
size_type size() const 
{
    return (this->_Mylast - this->_Myfirst);
}

Obviously, there’s a bit of meta-magic that goes on in order to determine exactly what types size_type and pointer are. In order to be “sure” what types they are I checked this:

bool bs = std::is_same<size_t, std::vector<int>::size_type>::value;
bool bp = std::is_same<int * , std::vector<int>::pointer>::value;
// both bs and bp evaluate as true, therefore:
//   size_type is just size_t
//   pointer is just int*

Compiling the following with /Wall gives me a signed-to-unsigned mismatch for mysize2, but no warnings for mysize1:

std::vector<int> myvector(100);
int *tail = &myvector[99];
int *head = &myvector[ 0];
size_t mysize1 = myvector.size();
size_t mysize2 = (tail - head + 1);

Changing the type of mysize2 to ptrdiff_t results in no warning.
Changing the type of mysize1 to ptrdiff_t results in an unsigned-to-signed mismatch.

Obviously I’m missing something…

EDIT: I’m not asking how to suppress the warning, with a cast or a #pragma disable(xxx). The issue I’m concerned about is that size_t and ptrdiff_t may have different allowable ranges (they do on my machine).

Consider std::vector<char>::max_size(). My implementation returns a max_size equal to std::numeric_limits<size_t>::max(). Since vector::size() is creating an intermediate value of type ptrdiff_t before casting to size_t it seems that there could be problems here – ptrdiff_t is not big enough to hold vector<char>::max_size().

  • 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-23T19:13:10+00:00Added an answer on May 23, 2026 at 7:13 pm

    Generally speaking, ptrdiff_t is a signed integral type of the same size as size_t. It must be signed so that it can represent both p1 - p2 and p2 - p1.

    In the specific case of the internals of std::vector, the implementor is effectively deriving size() from end() - begin(). Because of the guarantees of std::vector (contiguous, array based storage), the value of the end pointer will always be greater than the value of the begin pointer, and thus there is no risk of generating a negative value. In fact, size_t will always be able to represent a larger positive range than will ptrdiff_t, as it doesn’t have to use half its range to represent negative values. Effectively, this means that the cast in this case from ptrdiff_t to size_t is a widening cast, which has well defined (and intuitively obvious) results.

    Also, note that this is not the only possible implementation of std::vector. It could just as easily be implemented as a single pointer and a size_t value holding the size, deriving end() as begin() + size(). That implementation would also resolve your max_size() concern. In reality, max_size is never actually attainable–it would require your program’s entire address space to be allocated for the vector’s buffer, leaving no room for the begin()/end() pointers, function call stack, etc.

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

Sidebar

Related Questions

I thought that there was some way in .net 3.0 to give an array
I thought that I'll be clever and use subquery to get my report in
Is there any way to compare two functions in Haskell? My thought is that
I thought that it would be fun to present some classic CS problems and
I thought that to clone a List you would just call: List<int> cloneList =
I have always thought that the .equals() method in java should be overridden to
I long thought that in C, all variables had to be declared at the
I have always thought that the terms internationalization and localization (and their funny abbreviations
I was trying to get my head around XAML and thought that I would
For a long time ago, I have thought that, in java, reversing the domain

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.