Why pointers? WHY?!?!!?!

closed account (N6A4jE8b)
Sorry, but countless tutorials, views of source codes and even use of pointers myself and I still can't see what they really are useful for. I mean I know what they do, but what I don't know is why they're really needed.

But I need to know because if their use is required then I must know exactly what they're useful for and why they may be used.
Maybe you don't have to worry about it now?
Dynamic Memory
closed account (N6A4jE8b)
Maybe you don't have to worry about it now?


But if I will need to worry about it eventually then it's essential I know about it to some extent as of now.
It is possible to code an application with no pointers involved, to some extent.

The problem arises with a dynamic memory. C++ provides pointers to manipulate any memory, and does not provide a special abstraction of the dynamic memory. Thus, if you have to involve the dynamic memory you are forced to utilize pointers.

For example, when you receive and operate with the object generated by the outer code, you are inevitably operating with the dynamic memory. This is a common technique in COM. DLL generates objects implementing the interface you've requested and places objects or substitutions in the shared dynamic memory. You are operating with them via pointers.
Last edited on
Physical hardware very, very often uses specific memory locations for designated purposes. For example, it is very common for an embedded hardware board to contain an LED for signalling purposes. That LED will usually be a direct representation of a single memory location; that memory location has a specific, fixed address. That address value will usually be provided by the manufacturer, often as a #define in a header file somewhere.

#define SIGNAL_LED_MEM 0x4521FA47

The physical hardware in your PC does much the same; specific memory locations have specific purposes. The device drivers simply must be able to place values in the appropriate memory locations. This is done with pointers. Using the same example as above;

1
2
3
4
5
6
int* p_led;
p_led = SIGNAL_LED_MEM; // Could have said p_led = 0x4521FA47
*p_led = 0; // LED OFF
*p_led = 1; // LED ON
pause(1000); // Wait so the user can see the LED has come on.
*p_led = 0; // LED OFF again 


There is no way to write into specific memory address 0x4521FA47 without pointers.

Everything said above regarding dynamic memory is also true, and is another reason why pointers are so useful.


Last edited on
without pointers you can only copy data and if you have enough data you don't want it since it will slow down you program to an unacceptable extend.

and arrays imply pointers (to the first field)
They are not needed in general. They are needed in Algol-like languages like Pascal, C or C++.
So if you don't like pointers, don't give up. All is not lost. Learn OCaml or Haskell (look ma, no pointers!) :)


without pointers you can only copy data and if you have enough data you don't want it since it will slow down you program to an unacceptable extend.


BS. Lack of pointers doesn't imply lack of sharing (pass by reference / pass-by-pointer can be performed by the runtime system transparently, without programmer ever knowing it)
Last edited on
They are also popularly used when you don't know the size of an array that you need to initialise. If you were loading a '.wav' file for example, the file stores a value indicating the size of the wave data. When you load the wave data into a byte buffer, you are gonna need to allocate memory. BEFORE you run the program, you have no idea how much memory you are gonna need to store the waveform, because it differs from file to file. Pseudo code for this would look something like:

FILE WaveFile = OpenFile();

int WaveSize = GetWaveSize(WaveFile);

// Note that here you cannot do what you prefer to do:
// char Buffer[WaveSize];
// because the compiler doesn't know how much memory needs to be allocated, the program
// must deal with this memory dynamically.
char *Buffer = new char[WaveSize];

// Also, dont forget to DELETE any 'new' piece of memory when you are done with it!.
delete [] Buffer;
And for polymorphism:

 
Parent* foo = new Child;
Even we are not able to do polymorphism without addresses in C++. However, it is possible to avoid pointers and dynamic memory via references. We can operate with pure OOP abstractions and know nothing about memory, I suppose.

The code example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include<iostream>

struct someInterface {
    virtual int interfaceMethod(int,int)=0;
    virtual ~someInterface() { };
};

struct AClass: public virtual someInterface {
    virtual int interfaceMethod(int,int);
    virtual ~AClass() { };
};

struct BClass:  public virtual someInterface {
    virtual int interfaceMethod(int,int);
    virtual ~BClass() { };
};

int AClass::interfaceMethod(int rInt_,int lInt_) {
    return rInt_ - lInt_;
}

int BClass::interfaceMethod(int rInt_,int lInt_) {
    return lInt_ - rInt_;
}

int subtraction(someInterface& _IRef_,int rInt_,int lInt_) {
    return _IRef_.interfaceMethod(rInt_,lInt_);
}

int main() {
    AClass aObj;
    BClass bObj;
    
    std::cout << subtraction(aObj,5,3) << std::endl;
    std::cout << subtraction(bObj,5,3) << std::endl;

    return 0;
}


Here, the same interface in two different classes AClass and BClass implemented in a different way. My function subtraction rely on the interface only. And via virtual tables the implementation is overloaded for different classes.
Topic archived. No new replies allowed.