Memory addressing question

This is a discussion on Memory addressing question within the C Programming forums, part of the General Programming Boards category; Forgive me if this sounds like a newbie question. Any time you obtain a virtual stack address from a pointer, ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    14

    Memory addressing question

    Forgive me if this sounds like a newbie question. Any time you obtain a virtual stack address from a pointer, such as doing something like:

    printf("%d\n", &var);

    what is this address relative to by default? Is it the stack segment, the extra segment, what? Is there a way to obtain the complete physical address? Thanks

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    It depends how you create the variable...

    Code:
    char z[150];  // will be in the program's data segment 
    
    int SomeFunc(voic)
    {
        char s[150];   // will be on the stack
    
        char* s = malloc[150];  / /will be a pointer on the stack, directing you to the heap.
    }

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,158
    Quote Originally Posted by stevenswj View Post
    what is this address relative to by default? Is it the stack segment, the extra segment, what? Is there a way to obtain the complete physical address? Thanks
    You're talking in terms that might have been current in 1993...

    It's not relative to any segment. It's just a virtual address. On an x86 processor, it's technically relative to SS but on all modern operating systems the segmentation system isn't used anyway, so the particular default segment is irrelevant.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    Btw, you are printing addresses with %p, right? And casting them to void* first?
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by stevenswj View Post
    Forgive me if this sounds like a newbie question. Any time you obtain a virtual stack address from a pointer, such as doing something like:

    printf("%d\n", &var);

    what is this address relative to by default? Is it the stack segment, the extra segment, what?
    As noted before, it depends on whether the variable has been defined (inside) outside of any function.
    And what's the "extra" segment? Program's are divided into 3 segments/sections - text, data, and stack.
    Quote Originally Posted by stevenswj View Post
    Is there a way to obtain the complete physical address? Thanks
    Not unless you're working on a system w/o an OS or an OS w/o the address translation functionality built into it.
    And if that's the case, the printf() output is indeed the physical address else you're looking at the virtual address.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    Quote Originally Posted by itCbitC View Post
    As noted before, it depends on whether the variable has been defined (inside) outside of any function.
    And what's the "extra" segment? Program's are divided into 3 segments/sections - text, data, and stack.
    It doesn't matter. It's still just a virtual address, relative to nothing. Or to 0, if you will.
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by Elysia View Post
    It doesn't matter. It's still just a virtual address, relative to nothing. Or to 0, if you will.
    IBTD, it matters when trying to program a microcontroller w/o an OS, as data values can be installed at absolute memory addresses.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    We are not discussing non-OS here. All the information provides thus far is with regards to a modern operating system with virtual memory addressing. In which case, it doesn't matter.
    If you strip away that, then there are simply no rules regarding what is relative to where or how.
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Elysia... Going back to the answer I gave our friend.

    The first example --what might be called a Global variable-- actually occupies space in the program image itself. Usually the compiler groups them together in a "data segment".

    The second example is created on the program's stack at run time as the procedure is entered. This is not part of the program's image, but does occupy space on the stack.

    The third example using Malloc creates two bits of memory... the first on the stack (because the pointer is inside a procedure) and the second in the shared memory heap.

    This I know from experience in windows shell... I had a small program with a large array; about 1050 long ints. At first I was just making it global but a kind soul on another forum suggested that I should probably use malloc... my program size immediately dropped by 4k... Which was significant in a program that was only 16k to begin with.

    So, yes, it does matter.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by Elysia View Post
    We are not discussing non-OS here. All the information provides thus far is with regards to a modern operating system with virtual memory addressing. In which case, it doesn't matter.
    Not so sure about that unless the op sheds more light on what s/he is doing.
    Quote Originally Posted by Elysia View Post
    If you strip away that, then there are simply no rules regarding what is relative to where or how.
    If you strip away all that, then there are no "relative" rules, just "absolute".

  11. #11
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by CommonTater View Post
    This I know from experience in windows shell... I had a small program with a large array; about 1050 long ints. At first I was just making it global but a kind soul on another forum suggested that I should probably use malloc... my program size immediately dropped by 4k... Which was significant in a program that was only 16k to begin with.
    The only reason why program size dropped by 4k is because storage for malloc() is allocated at runtime, instead of at compile time.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by itCbitC View Post
    The only reason why program size dropped by 4k is because storage for malloc() is allocated at runtime, instead of at compile time.
    Yes, I know that. In fact that was my point... If you're going to create large blocks of memory use malloc instead of producing a monster program that's mostly empty space.

  13. #13
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by CommonTater View Post
    Yes, I know that. In fact that was my point... If you're going to create large blocks of memory use malloc instead of producing a monster program that's mostly empty space.
    But an un-initialized global array, no matter how big, won't consume any space in the program.

  14. #14
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by itCbitC View Post
    But an un-initialized global array, no matter how big, won't consume any space in the program.
    Aren't static variables initialized to 0 implicitly?

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by msh View Post
    Aren't static variables initialized to 0 implicitly?
    In C, nothing is implicitly initialized.

    If you want "int A = 0;" you have to say so.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer memory question
    By Edo in forum C++ Programming
    Replies: 5
    Last Post: 01-21-2009, 02:36 AM
  2. Memory question
    By John_L in forum Tech Board
    Replies: 8
    Last Post: 06-02-2008, 10:06 PM
  3. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 02:15 PM
  4. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM
  5. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM

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