Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What points to a pointer?

As I understand pointers contain the address of data at another memory location?
When an app is running how is the location of pointers kept track of? Why bother keeping track of the pointer, why not just directly keep track of address the pointer holds?

Additionally if I have the following code:

NSString *string = @"hello";
string = @"bye";

I am changing the value stored in the pointer named string (is it the pointer that is named string or the NSString object?) to the address of the new string ("bye"), right?
So how would I go about changing directly the object stored at the address held by the pointer?

(Also what is the correct terminology to use where I have used "keep track of"?)

Thanks

like image 582
Jake Foram Avatar asked Feb 18 '26 01:02

Jake Foram


2 Answers

Why bother keeping track of the pointer, why not just directly keep track of address the pointer holds?

Object references in objective C are actually pointers, so each time you use an object (such as NSString), you use a pointer to it - (NSString *)

I am changing the value stored in the pointer named string (is it the pointer that is named string or the NSString object?) to the address of the new string ("bye"), right?

Right.

So how would I go about changing directly the object stored at the address held by the pointer?

In the case of such strings, they are immutable, and you can't change them, in case of other objects, you call their methods, or set their properties.

like image 118
MByD Avatar answered Feb 19 '26 15:02

MByD


When an app is running how is the location of pointers kept track of?

Pointers are stored as any other variable; they typically take the same size as an unsigned long, but this is by no means guaranteed, just to give you an idea of how they are implemented. Compilers are free to do a huge variety of optimizations, so the pointers may be stored in memory, they may be stored in registers, or they may exist only as hypothetical entities if they are optimized away.

Consider the following code:

void foo(void) {
    char *c;
    char buf[100];
    for (c=buf; c < buf+100; c++ {
        c = '0';
    }
}

In this case, the variable c is being used to write an ASCII 0 character to every character in the buf array. c may exist only in a register, because it does not live beyond this function. (There are better ways of writing this code.)

Consider the following code:

struct foo {
    char name[10];
    struct foo *next;
}

The member next in this case is a pointer to further struct foo objects -- say, a linked list of these things. These pointers must be stored in memory, because they are part of the contract of these objects -- they have to be there. There is no way around these pointers, either -- the objects they point to can be replaced with other objects on the programmer's whim. And, since the number of these objects is determined entirely at runtime, the compiler can't just keep track of the addresses in its symbol tables, as it would for stack-allocated variables.

So how would I go about changing directly the object stored at the address held by the pointer?

This is complicated by your example's use of "foo" strings in the code. These are saved in read-only memory in the process address space, so you cannot modify them. (Surprise!) If you initialize the strings with another method, you can modify the data via the pointer:

char *c = malloc(10);
strcpy(c, "hello");
c[0] = 'H';
printf("c: %s\n", c);

This will overwrite the h with H in the allocated space available via the c pointer. Accessing pointers as if they were arrays is the same re-writing the pointer access like this:

c[0] = 'f';
c+0 = 'f';

And, in fact, array accesses are pretty similar -- the name of the array is the same as a pointer to its first element.

It's a little complicated; the book Expert C Programming covers pointers in astonishing detail and is well worth the money.

like image 22
sarnold Avatar answered Feb 19 '26 15:02

sarnold



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!