pointers

I'm in my second semester of c++. We have just finished up with pointers and I'm very confused as to why I would use a pointer than to just use a normal variable.

Could someone explain to me why pointers exist and why I should use them.

Thank you,
Jarrod
If you put everything on the stack, you'll run out of memory. If you want to use more memory, you'll have to use the heap. If you want to use the heap, you have to use new. If you use new, it gives you a pointer to the memory in the heap.

Like this, for example:

int* p = new int[300000000];

I'll be very impressed if you can put an array of 300000000 int values on your stack. :) Depending on your hardware and/or operating system, stacks of just a few kb are not unknown.

Someone else can give you the next reason.


use a pointer than to just use a normal variable.

A pointer IS a normal variable.
Last edited on
Besides that pointers can be used to guarantee that an argument is going to change (although in C++ usually references are used for that) inside a function.

Another useful aspect of using pointers will be understood later (when you would learn about polymorphism) where you can use a pointer as a family of objects rather than as a specific object.
To be more clear instead of referring to an Object1 you could be referring to Object1 and all of its derived objects. Something similar for the build-in types might have been if instead of int you would have also char, long etc. The latter does not work but you can get the picture.

Anyway maybe bad example. The point is pointer are quite useful in C/C++ and you will get that as you learn more.
Represent relationships. By instance, graphs.
pointers are primarly used to alocate memory storage for variables and objects on the heap.

memory is divided into 2 "parts": stack and heap.
since stack is limited to your program you're forced to use heap... so to do so u use pointers.
Hardware commonly uses predefined memory locations for special purposes; for example, to set an LED on or off, or a location that an external link will place data.

As such, you need a way to specify that you want to read that exact memory location. For example, imagine you had a hardware board with a temperature sensor attached, and the temperature was placed directly into memory location 0x1234AAAA as an int.

Here's how you would read that using a pointer:

1
2
int* p = (int*)0x1234AAAA; // set the pointer
int temperature = *p; // now have read the temperature 


If you didn't have pointers, you'd have no way to do this.

The computer you're using to read this is stuffed full of exactly this method of directly accessing known memory locations.
Linked lists aren't possible without pointers.
As Moschops already said, a pointer IS a normal variable that store addresses which "points" to some bytes somewhere in memory.
For example an int * stores an address that points to some position in memory where you say there should be an int. It might be an integer there, but you could also do this:
1
2
3
float f = 4;
int * p = (int*) &f;
cout << *p << endl;

This code allows you to interpret the bytes that represent the float value 4 as an integer. This wont print out 4, as int and float store the value in different ways, it'll just print out some random number.

The code above is just an example, it's nothing you should write.

The main reason to use a pointer is to send the address to some large quantity of data rather than copying the data around. For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
struct DATA {
    char v[1024]; // A 1 kB array with characters
};
void f1(DATA d) {
    // DONT DO THIS!
    // This would COPY the ENTIRE 1 kB array with characters, and would be extremely inefficient
    // if all you want to do in this function is to read the data. Also if you alter d, you'd alter a 
    // temporary copy of a DATA struct.
}
void f2(DATA * p) {
   // Here you receive an address to some existing DATA struct; however you might want to
   // verify that this is a real address, and not for example NULL (0), which is used as a
   // value that tells us this DATA pointer doesnt point at anything.
   // If you would use this address and alter the structure, you would alter the a 
   // NON-temporary DATA struct.
}

int main()
{
     DATA d = {"Some content"};
     f1(d);  // Send a copy of d to f1; f1 CANNOT change d
     f2(&d); // Send the address of d to f2; f2 CAN change d
}


There's also references in C++, which works like pointers, but you can skip some syntax using a reference rather than a pointer.

Hope this helps. There are more differences, but I'll save that for someone else :)).

// Sorglig
Last edited on
References are second names for existing variables, even though they are usually implemented with pointers. ;p
Topic archived. No new replies allowed.