Thread: Trying to understand memory...

  1. #1
    Registered User CompiledMonkey's Avatar
    Join Date
    Feb 2002
    Location
    Richmond, VA
    Posts
    438

    Trying to understand memory...

    When I run this code it always returns a negative integer, can anybody tell me why? It doesn't seem right to me.

    Code:
    main()
    {
    	int n0;
    	int *n1 = &n0;
    	
    	printf("%d\n", (int)n1);	
    }

  2. #2
    Registered User CompiledMonkey's Avatar
    Join Date
    Feb 2002
    Location
    Richmond, VA
    Posts
    438
    Basically I want to run through the memory of my system and see the values of each in int form. Can I do this? Is there any way to see what application is using that memory block?

  3. #3
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    First of all, don't double-post, it's considered rude. Just go into your first one and edit your second question in. I'm not sure, but I think the answer is always negative because of the integer cast you're doing during your printf() argument, and the bitshifting that goes on with that. As for your question, no, that's just about impossible to do using only one int. Maybe using a long unsinged int you could, but I don't think that would account for the memory segments, or the offset numbers either. If it's possible, I have no idea how to do it. Come to think of it, I probably don't know that much if any more about memory than you do
    Last edited by Draco; 11-04-2003 at 12:25 AM.

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Your system can probably represent integers up to 2147483647 (which is 2^31 - 1). (You have 32 bits to work with, but the high order bit is used as the sign bit, 0 for positive, 1 for negative. The memory address is a hex number too big to be represented as 31 binary digits. I don't think it necessarily HAS to print as a negative integer -- it may just be undefined -- but if you end up with a '1' in the high order bit, it will be interpreted as a negative number.

    You could cast it as an unsigned long integer & it will probably work:
    printf("%lu\n",n1)
    or use printf("%p",n1) & it will be printed in hex form (%p is the conversion spec for a pointer).

    I don't know how you can find out which application is using which memory locations.

    Also, I don't think a modern operating system will allow one application to read from any memory other than its own. (But I could be wrong there; I've never tried it.)

    Edit:
    Thought about it some more. If you print it as a pointer, you'll get a number looking something like 0xbfffdab4. I believe that means the memory segment is bfff and the offset is dab4 (both hex numbers). But if you put them together & print them as a single unsigned long integer, that would probably be a meaningless number, not the true memory location.
    Last edited by R.Stiltskin; 11-04-2003 at 12:33 AM.

  5. #5
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    I read in the back pages of an assembly book I was looking to buy online that they would show you how to allow static variables to go past their scope into the global memory, maybe something like that is possible but on a larger scale. Doesn't seem likely though...

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > printf("%d\n", (int)n1);
    Use %p for printing addresses, and lose the cast
    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.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Use %p for printing addresses, and lose the cast
    Don't lose the cast, change it to (void *). Since the %p flag expects only a void pointer, anything else is technically undefined.
    My best code is written with the delete key.

  8. #8
    Visionary Philosopher Sayeh's Avatar
    Join Date
    Aug 2002
    Posts
    212

    Actually here's why it's negative.

    The reason your value is negative is because you are trying to stuff a 32-bit value into a 16-bit variable.

    Code:
    main()
    {
    	int n0;
    	int *n1 = &n0;
    	
    	printf("%d\n", (int)n1);	
    }
    'n0' is a 16-bit variable, whiel '&n0' is a 32-bit value. The reason it's negative is that the high-bit ended up set, based on where the 32-bit value got truncated.
    It is not the spoon that bends, it is you who bends around the spoon.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Whatever gave you the idea that ints were 16 bit and pointers 32 bit?

    Just as likely that they're both 32 bits, and the pointer just has the msb set, thus giving a negative integer.
    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