regarding memory and pointers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pratap

    #1

    regarding memory and pointers

    consider

    int p=10;
    int *q=&p;

    now my question is how does the compiler (or any type of mechanism)
    bind q and p .i.e. memory location of a variable p is bound in a
    variable q.
    How does this memory mapping take place. Is there any specific
    hardware mechanism that helps this mapping.

  • Chris Dollin

    #2
    Re: regarding memory and pointers

    pratap wrote:
    consider
    >
    int p=10;
    int *q=&p;
    >
    now my question is how does the compiler (or any type of mechanism)
    bind q and p .i.e. memory location of a variable p is bound in a
    variable q.
    The address of the variable `p` is computed and stored into `q`,
    much as in `int x = 10 - 1;` the value of the expression `10 - 1`
    is computed (very likely at compile time, in this case) and stored
    into `x`.
    How does this memory mapping take place.
    It isn't what's usually called "memory mapping".
    Is there any specific hardware mechanism that helps this mapping.
    Typically the ability to do addition.

    No, really. If the code you write above occurs in a function,
    so that `p` is an auto variable, a common [1] implementation is
    to have a register pointing to a stack frame and to have `p`
    stored at some offset [2] in that frame, so that the address of `p`
    is value-of-frame-pointer-plus-offset. Then the initialisation
    of `q` will be `add the frame pointer and the offset and store
    the result in q`.

    [1] But not universal, and not required by the Standard.

    [2] Which could be positive or negative; It Depends.

    --
    Chris "an example, not a definition" Dollin

    Hewlett-Packard Limited Cain Road, Bracknell, registered no:
    registered office: Berks RG12 1HN 690597 England

    Comment

    • Richard

      #3
      Re: regarding memory and pointers

      pratap <pratappai78@gm ail.comwrites:
      consider
      >
      int p=10;
      int *q=&p;
      >
      now my question is how does the compiler (or any type of mechanism)
      bind q and p .i.e. memory location of a variable p is bound in a
      variable q.
      How does this memory mapping take place. Is there any specific
      hardware mechanism that helps this mapping.
      This is a somewhat strange question.

      All you need to know at the C level is that it is possible to "address"
      a variable/memory location. Data is stored at addresses and variables of
      type pointer can "point" to or "address" those addresses.

      Comment

      • karthikbalaguru

        #4
        Re: regarding memory and pointers

        On Oct 9, 3:43 pm, pratap <pratappa...@gm ail.comwrote:
        consider
        >
        int p=10;
        int *q=&p;
        >
        now my question is how does the compiler (or any type of mechanism)
        bind q and p .i.e. memory location of a variable p is bound in a
        variable q.
        It is a software written in such a manner that the address of variable
        'p' is computed and stored into the variable 'q'.
        How does this memory mapping take place. Is there any specific
        hardware mechanism that helps this mapping.
        C compiler is a 'compiler software'. It is not a hardware.
        Here, it does not involve any latch/chip for accessing a variable
        stored in a location.
        It is a software written in such a manner that the address of variable
        'p' is computed and stored into the variable 'q'.

        Your question is not very clear :(:( !!

        Karthik Balaguru

        Comment

        Working...