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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:14:33+00:00 2026-05-23T14:14:33+00:00

I wanted to try pointer arithmetic in c#. I have used an asp.net web

  • 0

I wanted to try pointer arithmetic in c#. I have used an asp.net web app, expecting that it wouldn’t matter if it was this or a console app.

here is what I tried:

public class memorytest
{
    public class Sample
    {
        public int A;
        public int B;
        public int C;
        public int D;
    }

   public static unsafe void Main()
   {
    Sample s = new Sample {A = 1, B = 2, C = 3, D = 4};
    int a = 1;
    int b = 2;
    int* pA = &a;
    int* pB = &b;
    Debug.WriteLine("{0:x16}",* pB);
    Debug.WriteLine("{0:x16}",*(pB - 1));
    Debug.WriteLine(*pA);
    Debug.WriteLine("{0:x16}",*(pB - 2));
    }

}

The result was like this:

0000000000000002,
0000000004b5ca00,
1,
0000000004b5c9fc,

I was obviously expecting the second one to be the number 1. Does anybody understand this?

  • 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-23T14:14:33+00:00Added an answer on May 23, 2026 at 2:14 pm

    First off, if you want to learn how the stack is laid out, it seems like it would be a lot easier to start the debugger and look at the stack in the debugger.

    Assuming that for some reason you want to keep on writing programs to examine their own stack state: you stopped experimenting too early. The program you should have written is:

    Debug.WriteLine("{0:x16}",*(pB - 1));
    Debug.WriteLine("{0:x16}",*(pB + 0));    
    Debug.WriteLine("{0:x16}",*(pB + 1));
    

    and then you would have gotten the output

    00000badf00dd00d  <-- some pointer value
    0000000000000002  <-- contents of b
    0000000000000001  <-- contents of a
    

    and you would have learned that stacks grow in the opposite direction that you think they do. In many architectures pushing something onto the stack decreases the stack pointer.

    Of course, as others have pointed out, we make no guarantees whatsoever as to how stuff is laid out on the stack, or even if stuff goes onto the stack in the first place; local variables may be placed on the heap in some situations, and local variables whose addresses are never taken may be enregistered. For example, what happens if you don’t take the address of “a”? Does it still end up on the stack? Maybe not!

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

Sidebar

Related Questions

I have wanted to try GAE since launch, but coming from ASP .NET and
After I've seen this PDC session I wanted to try to learn a bit
Inspired by this question , I wanted to try my hand at the latest
I am working on some scraping app, i wanted to try to get it
I have a quick little application and wanted to give a try to develop
I have a program that generates a PDF as output. If I send this
I have just compiled GCC 4.6.0, and I wanted to try the new features
I wanted to try to deploy our project for .net 3.5 client framework (so
I wanted to try out an example you can find here: http://php.net/manual/en/function.include.php , but
I found this link and I wanted to try the carousel example out. I

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.