Question about variable locations in memory and pointers

This is a discussion on Question about variable locations in memory and pointers within the C++ Programming forums, part of the General Programming Boards category; Lately i've been researching on pointers and memory locations, and something doest seem right to me: I wrote this little ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    84

    Question about variable locations in memory and pointers

    Lately i've been researching on pointers and memory locations, and something doest seem right to me:

    I wrote this little program:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int c1 = 3;
        int a;
        bool sch;
        cout <<"Variable c == " <<c1 <<", and its located at: " <<(int)&c1 <<endl;
        cin >>sch;
        cin.ignore(100, '\n');
        switch (sch)
        {
            case 1:
            {
                cout <<"Change: ";
                cin >>c1;
            } break;
            default: break;
        }
        cout <<"Variable c == " <<c1 <<" Located at: " <<(int)&c1 <<endl;
    
        cin.ignore(100, '\n');
        cin.get();
    }
    The problem here is that when i run it, the variable c1 is always alocated in the same memory position, and my question is, shouldnt it change every time i run the program??

    and another thing, when i open multiple instances of that program, all the instances show c1 located in the same memory space, how is that possible??

    help pls.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,739
    >>The problem here is that when i run it, the variable c1 is always alocated in the same memory position, and my question is, shouldnt it change every time i run the program??
    Open up more, different programs to take up more RAM in your system and then it might change. The OS determines how applications are allocated memory and it can make it seem like it will use the same memory down to the location every time, even if you run the application after a reboot or on a full moon.


    >>and another thing, when i open multiple instances of that program, all the instances show c1 located in the same memory space, how is that possible??

    Multiple instances? If you allow the program to exit the memory will be returned to the system. This is different from a command prompt staying open so that you can see the results.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    84
    Quote Originally Posted by whiteflags View Post
    Multiple instances? If you allow the program to exit the memory will be returned to the system. This is different from a command prompt staying open so that you can see the results.
    yes, but if i open more than one without allowing them to exit (meaning they all still have the memory for the variable c1 alocated) i still get the same memory locations in all of the open instances. How is it possible that 2 or more programs are using the same memory location at the same time (even if both programs are the same)??

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,739
    Well there is another explanation. If you really have multiple instances then the program can share memory. Each process has its own thread and only one thread will be active at a time, so there is no room for conflict, unless you use parallel processing. That opens its own can of worms.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,785
    The program are using virtual memory, not physical memory. The virtual memory is an address located in the process. All processes have their own virtual memory address space. Thus, the same address is never occupied twice between processes, which explains what you see.
    The OS maps the virtual memory to physical memory. But that is not something we need to worry about. I'd suggest reading up about virtual memory if you're intrigued.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    84
    Quote Originally Posted by Elysia View Post
    The program are using virtual memory, not physical memory. The virtual memory is an address located in the process. All processes have their own virtual memory address space. Thus, the same address is never occupied twice between processes, which explains what you see.
    The OS maps the virtual memory to physical memory. But that is not something we need to worry about. I'd suggest reading up about virtual memory if you're intrigued.
    I see, looks like i was confusing both memories, but now i know the diferences.

    tks

  7. #7
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Is windows your OS?
    lol if it is windows, that's why !
    shows you how crap the OS is.
    if it was on Linux or Mac the addresses would be different on each run time.

    So I was told haha
    Last edited by Eman; 10-20-2010 at 03:02 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 12:57 AM
  3. Newbie question: pointers, other program's memory
    By xxxxme in forum C++ Programming
    Replies: 23
    Last Post: 11-25-2006, 12:00 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. Im so lost at . .
    By hermit in forum C Programming
    Replies: 18
    Last Post: 05-15-2002, 01:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21