# Trying to understand memory...

This is a discussion on Trying to understand memory... within the C Programming forums, part of the General Programming Boards category; When I run this code it always returns a negative integer, can anybody tell me why? It doesn't seem right ...

1. ## 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. 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. 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

4. 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.

5. 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. > printf("%d\n", (int)n1);
Use %p for printing addresses, and lose the cast

7. >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.

8. ## 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.

9. 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.