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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:38:36+00:00 2026-05-27T20:38:36+00:00

Say you do: void something() { int* number = new int(16); int* sixteen =

  • 0

Say you do:

void something()
{
   int* number = new int(16);

   int* sixteen = number;
}

How does the CPU know the address that I want to assign to sixteen?

Thanks

  • 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-27T20:38:37+00:00Added an answer on May 27, 2026 at 8:38 pm

    There’s no magic in your example code. Take this snippet, for example:

    int x = 5;
    int y = x;
    

    Your code with pointers is exactly the same – the computer doesn’t need to know any magic information, it’s just copying whatever’s in number into sixteen.

    As to your comment below:

    but how does it know where x or y are in memory. If I ask to copy x into y, how does it know where either of those are.

    In practice, on most machines these days, probably neither of them will be in memory, they’ll be in registers. But if they are in memory, then yes, the compiler will emit code that keeps track of all of those addresses as necessary. In this case, they’d be on the stack, so the machine code would be accessing the stack pointer register and dereferencing it with some compiler-decided offsets that refer to the storage of each particular variable.

    Here’s an example. This simple function:

    int f(void)
    {
      int x = 5;
      int y = x;
      return y;
    }
    

    When compiled with clang and no optimizations, gives me the following output on my machine:

    _f:
     pushq  %rbp               ; save caller's base pointer
     movq   %rsp,%rbp          ; copy stack pointer into base pointer
     movl   $5,0xfc(%rbp)      ; store constant 5 to stack at rbp-4
     movl   0xfc(%rbp),%eax    ; copy value at rbp-4 to register eax
     movl   %eax,0xf8(%rbp)    ; copy value from eax to stack at rbp-8
     movl   0xf8(%rbp),%eax    ; copy value off stack to return value register eax
     popq   %rbp               ; restore caller's base pointer
     ret                       ; return from function
    

    I added some comments to explain what each line of the generated code does. The important things to see are that there are two variables on the stack – one at 0xf8(%rbp) (or rbp-8 to be clearer) and one at 0xfc(%rbp) (or rbp-4). The basic algorithm is just like the original code shows – the constant 5 gets saved into x at rbp-4, then that value gets copied over into y at rbp-8.

    “But where does the stack come from?” you might ask. The answer to that question is operating system and compiler dependent, though. It’s all set up prior to your program’s main function being called, at the same time as other runtime setup required by your operating system takes place.

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

Sidebar

Related Questions

Say I've got something like this public void run(){ Thread behaviourThread = new Thread(abstractBehaviours[i]);
Let's say I want to do something like this void my_printf(char *fmt,...) { char
Let's say you have: void *p = // something; int size = 10; *((char
Say a function takes a void pointer as an argument, like so: int func(void
Say I have a test like: void TestSomething(int someParam) { // Test code }
Say I have two functions that expect ...rest parameters private function a(...myParams):void { trace(myParams.length);
A quick question I've been wondering about for some time; Does the CPU assign
static void Job(Args _args) { int number=10; do { print (strfmt(current number = %1,
Let say that I create an object and run it in a thread, something
struct X { X():mem(42){} void f(int param = mem) //ERROR { //do something }

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.