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

  • Home
  • SEARCH
  • 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 8583977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:39:46+00:00 2026-06-11T21:39:46+00:00

void Test2() { int c=8; int b=7; int d=9; int *a; a = &b;

  • 0
void Test2()
{   
    int c=8;
    int b=7;
    int d=9;
    int *a; 

    a = &b;
    a+=sizeof(int); //I supposed that *a should points on variable d after this 

    cout << "b\t" << &b << "\t" << b << endl;
    cout << "a\t" << a  << "\t" << *a << endl;
    cout << "c\t" << &c  << "\t" << c << endl;
    cout << "d\t" << &d  << "\t" << d << endl;
}

I supposed that *a should points on variable d because b and d (as I thought) lie nearby in the stack of local variables. But *a points on another address so *a!=d
My question is why so? Is it the feature of Visual Studio 2010 or something else?

  • 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-06-11T21:39:47+00:00Added an answer on June 11, 2026 at 9:39 pm

    No, it’s a feature of C++ called undefined behavior. You can’t do pointer arithmetics outside an array (or one position over the bound of the array) you own.

    You could get this to work by a += 1 because a is already a int*, so += 1 will make it point to the next integer. a+=sizeof(int) will move it sizeof(int) integers to the right.

     +------+------+------+------+------+
     |      |      |      |      |      |      
     +------+------+------+------+------+
        ^      ^                    ^
        |      |                    |
        a     a+1               a+sizeof(int) (assuming sizeof(int) == 4)
    

    Again, technically it’s undefined.

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

Sidebar

Related Questions

I have two pages, test1.aspx and test2.aspx test1.aspx has this protected void Page_Load(object sender,
class test{ int x; public: //public functions getx() and putx() }; void main() {
static void testlock() { for(int i=0;i<10000000;i++) { float f=2.0/i; } } static void TEST()
Given: template<class T> struct test { void foo(int b); void testFunc( int a )
Say I have a test like: void TestSomething(int someParam) { // Test code }
I am loading a certificate from string like this: public static void Test() {
Consider a class that is supposed to make parameter suggestion, given some clues, and
I could not figure out how to dereference this pointer... sizeof(shapetest2->tripsName) in this line
I have a simple struct: typedef struct { void *things; int sizeOfThings; } Demo;
test.cu: #include <iostream> #include book.h __global__ void add( int a, int b, int *c

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.