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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:35:45+00:00 2026-05-16T02:35:45+00:00

Is the following code 100% portable? int a=10; size_t size_of_int = (char *)(&a+1)-(char*)(&a); //

  • 0

Is the following code 100% portable?

int a=10;
size_t size_of_int = (char *)(&a+1)-(char*)(&a); // No problem here?

std::cout<<size_of_int;// or printf("%zu",size_of_int);

P.S: The question is only for learning purpose. So please don’t give answers like Use sizeof() etc

  • 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-16T02:35:46+00:00Added an answer on May 16, 2026 at 2:35 am

    From ANSI-ISO-IEC 14882-2003, p.87 (c++03):

    “75) Another way to approach pointer
    arithmetic is first to convert the
    pointer(s) to character pointer(s): In
    this scheme the integral value of the
    expression added to or subtracted from
    the converted pointer is first
    multiplied by the size of the object
    originally pointed to, and the
    resulting pointer is converted back to
    the original type. For pointer
    subtraction, the result of the
    difference between the character
    pointers is similarly divided by the
    size of the object originally pointed
    to.”

    This seems to suggest that the pointer difference equals to the object size.

    If we remove the UB’ness from incrementing a pointer to a scalar a and turn a into an array:

    int a[1];
    size_t size_of_int = (char*)(a+1) - (char*)(a);
    
    std::cout<<size_of_int;// or printf("%zu",size_of_int);
    

    Then this looks OK. The clauses about alignment requirements are consistent with the footnote, if alignment requirements are always divisible by the size of the object.

    UPDATE: Interesting. As most of you probably know, GCC allows to specify an explicit alignment to types as an extension. But I can’t break OP’s “sizeof” method with it because GCC refuses to compile it:

    #include <stdio.h>
    
    typedef int a8_int __attribute__((aligned(8)));
    
    int main()
    {
     a8_int v[2];
    
     printf("=>%d\n",((char*)&v[1]-(char*)&v[0]));
    }
    

    The message is error: alignment of array elements is greater than element size.

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

Sidebar

Related Questions

Consider the following code snippet: std::vector<int> v; v.reserve(100); v.insert(v.end(), 100, 5); v.erase(v.begin(), v.end()); std::cout
I have seen the following code: [DefaultValue(100)] [Description(Some descriptive field here)] public int MyProperty{...}
Consider the following code: try { int *i = NULL; i[100] = 20; catch
I am facing the following code: char buf[100]; char buf2[100]; strcpy( buf, áéíóúç );
Let's say I have the following code : ThreadPool.SetMinThreads(100, 100); for (int i =
I have the following code: #include <iostream> int main() { int n = 100;
I have the following code: int intNumber1 = 100; object intNumber2 = 100; bool
The following code works fine: for(int i=0; i<500; i++) { for(int j=0; j<100; j++)
The following code: org.apache.commons.lang.StringEscapeUtils.unescapeHtml(&#72;&#101;&#108;&#108;&#111;&#32;&#87;&#111;&#114;&#108;&#100;); gives: Hello World But I'd like to know how to
I have the following code; void* buffer = operator new(100); unsigned char* etherhead =

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.