views:

192

answers:

6

As I understand that (or think I understand), a variable that is a pointer stores just a memory address in it's value.

lets say:

int x = 5;
NSString *str1 = [NSString stringWithCString:"one"];

then the value of x is 5. That's what I see in the Debugger when I put a breakpoint there.

but:

the value of str1 is NOT "one". It's a memory address like bfffd3d0. That's what I see in the "value" field of the debugger, when I stop there. So behind this bfffd3d0 memory address is the NSString object with it's huge data structure (lets assume its huge), lots of instance vars, values, and so on. Is that almost right?

I'm not talking about the memory of the variable name itself, but about the value that variable "references" or points to, or stores, or whatever.

+6  A: 

You are absolutely right, yes.

"behind this bfffd3d0 memory address is the NSString" might be better said "at this bfffd3d0 memory address..."

RichieHindle
how could I think about an memory address more precisely? is that something like an area on a memory chip (big abstraction here) where something is stored?
Thanks
Think of memory (as seen by a running process) as a huge stack of pigeonholes, one above the other. Or as lines on a very long piece of paper. I'm not sure I'm helping...
RichieHindle
+1  A: 

Yes, that's a close enough description.

Re your other question, effectively that call to NSLog passes the address of the variable onto the stack, but the @"%x" tells NSLog to interpret that address as in integer.

Alnitak
+1  A: 

You tagged this as objective-c but 'pointers' are not specific to only objective-c. Other languages like C++ have pointers as well.

Brian Ensink
i just want to make sure that there is no confusione due to any possible difference ;)
Thanks
+1  A: 

If by memory address you mean RAM then it is not entirely correct. A pointer can contain the address to a mapped device (eg. a video card, a network card etc.).

AZ
+2  A: 

More or less, and I'm going to be pedantic here. The value of str1 is, like you say, the location at which "one" is stored, rather than "one" itself.

However, it is not exactly memory address, for a couple of reasons. First, the hardware reason that your application work on virtual memory, and so it never even sees the actual memory addresses. The value (say 0xbfffd3d0) is simply the virtual memory address to which the actual memory address has been mapped. It may not even correspond to a memory address at all. It may be unmapped (so accessing this address will give you a segmentation fault/access violation, or it may be memory-mapped and backed by a file or a hardware device. It is possible that writing to this "address" doesn't actually write data to memory, but instead sends a signal to the GPU.

And second, the language-lawyer reason: Pointers are not addresses. There are operations that would be valid on addresses, but are undefined for pointers. The NULL pointer may point to other addresses than 0. Pointers that do not point to the same array may not be compared for equality, even though it'd make perfect sense to do so on memory addresses.

This link goes into more detail about the situation (specifically for C++, but it is basically the same for C and obj-C.)

But the short answer is that pointers are designed so that they can be implemented bby the compiler as (virtual) memory addresses. So in practice, they tend to behave much the same, but there are differences, and assuming that a pointer is just a memory address will lead to errors at some point.

And in the case you ask about, you're exactly right, a pointer variable stores the location at which data can be found, rather than the data itself.

jalf
+1, great post.
Bastien Léonard
+1  A: 

As far as I understand it a pointer points to a memory address, the value held at that address can be obtained by dereferencing the pointer

int number = 14;
int* ptrNumber = &number; //the value of the pointer is now the address of number

int n=*ptrNumber; //pointer was dereferenced, so n is now the value at the address of ptrNumber (n = 14)
Crippledsmurf