Thread: view memory location of variable

  1. #1
    Registered User
    Join Date
    Jul 2022
    Posts
    17

    view memory location of variable

    Is it possible to view memory location of variable in visual studio without printf statement

    Code:
    #include<stdio.h>
    
    
    #define size  5 
    #define set   1
    #define Reset 0
    
    int flag1;
    
    struct Node1
    {
        int Number1;
        char Rank1;
        float price1;
        double salary1;
        char Name1[5];
        int* ptr1;
    }svar;
    
    union Node2
    {
        int Number2;
        char Rank2;
        float price2;
        double salary2;
        char Name2[size];
        int* ptr2;
    }uvar;
    
    
    void wait()
    {
        for (int i = 0; i < 10; i++);
    }
    
    
    void foo1(int* PTR)
    {
        *PTR = 100;
    
    
    }
    
    
    void foo2()
    {
        flag1 = set;
        wait();
        flag1 = Reset;
    
    
    }
    
    
    int main()
    {
        int Number;
        char Rank;
        float price;
        double salary;
        char Name[size];
        int *ptr;
    
    
        Number = 2;
        printf("%d", &Number);
        Rank = 'A';
        price = 230.23;
        salary = 44000.540;
        //Name[size] = "VIJA";
        ptr = &Number;
    
    
        svar.Number1 = 10;
        svar.Rank1 = 'B';
        svar.price1 = 540;
        svar.salary1 = 11240;
    
    
        uvar.Number2 = 20;
        uvar.Rank2 = 'C';
        uvar.price2 = 740;
        uvar.salary2 = 12240;
    
    
        foo2();
    
    
        if (Rank == 'A')
        {
            printf("success");
        }
        else
        {
            printf("fialure");
        }
    
    
        foo1(&Number);
            
        return 0;
    }
    view memory location of variable-memory-jpg

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Can't you just write &Number in the address bar of the memory display?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2022
    Posts
    17
    Quote Originally Posted by Salem View Post
    Can't you just write &Number in the address bar of the memory display?
    I get two different memory location of variable

    print statement display memory location 1383070532 of Number on terminal

    address bar display of memory 0x000000633418F744 of Number

    confused because there should be only one memory location Number

    Attachment 16497

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well there is only one address.

    Your %d is the wrong format for displaying addresses.
    1. It's in decimal, so that's a source of confusion.
    2. Your addresses are 64-bit, so most likely you're only seeing the lower 32 bits when printing it at a decimal integer - yet more confusion.

    Try it with
    Code:
    printf("%p\n", (void*)&Number);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jul 2022
    Posts
    17
    Quote Originally Posted by Salem View Post
    for displaying addresses.

    Try it with
    Code:
    printf("%p\n", (void*)&Number);
    yes it worked, Now the problem is that there can be many variables in a program like we can see the value of all the variables at once in the watch window, can I see all the memory locations at once

    Now every time I enter the address of the variable in the memory address bar, only then I get its location. I can't see all memory address
    Last edited by king12; 09-11-2022 at 05:51 AM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    TBH, the address of a variable is seldom as interesting as it's contents.

    Once the stack frame is established, your local variables never change address, but their contents will change.

    Some local variables might not even have allocated space due to compiler optimisations, yet the debugger will be able to determine the value of the variable just fine.

    For globals, there's also this option.
    /Fm (Name Mapfile) | Microsoft Docs

    > can I see all the memory locations at once
    Your memory dump is looking at several hundred bytes of memory.
    What you don't have is a clear association of which bits of memory correspond to which variable.

    For example, given
    Code:
    int main()
    {
        int Number;
        char Rank;
    Knowing the address of Number tells you nothing about the address of Rank.

    For sure, you'll learn a few 'tricks' given the current compiler.
    But change compiler, and the 'rules' as to how the compiler chooses to layout local variables on the stack are likely to change.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. variable memory location with ampersand vs pmap
    By setevoy in forum C Programming
    Replies: 1
    Last Post: 10-23-2014, 01:37 PM
  2. Memory location of variable
    By justine in forum C Programming
    Replies: 9
    Last Post: 11-27-2012, 05:01 PM
  3. allocate apecified memory location for a c variable
    By BharathKumar in forum Linux Programming
    Replies: 5
    Last Post: 06-01-2007, 03:47 PM
  4. Memory Location
    By SomC in forum C Programming
    Replies: 6
    Last Post: 05-08-2004, 02:54 AM
  5. memory location
    By xlordt in forum C Programming
    Replies: 3
    Last Post: 10-21-2002, 09:39 AM

Tags for this Thread