the need of pointers for dynamic memory allocation?

I recently learned about pointers and during that whole time I had the question "Why are they necessary? (ever)" I learned you need them to dynamically allocate memory for an array. I realize this is probably only one thing it's necessary for, but as far as it, I don't see why. Why is it necessary to use pointers in order to dynamically allocate memory for an array? I mean why can't you just write the statement cin >> array[size] ?
When we write

1
2
3
4
5
6
7
int i = 0 ;

int main()
{
    int j = 99 ;
    i = j ;
}


i and j are names of two objects of type int. Each name has a binding to a location in memory. The compiler knows all about this binding; for i = j ;, the code it generates is logically equivalent to: pick up the int which is at the memory location identified by the name j and write that value into the int at the memory location identified by the name i.

For objects with a dynamic storage duration, the compiler has no idea where these objects are in memory; we can't ask the compiler to map a name to the memory location for such an object (unless we also tell it where it is in memory).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int i = 0 ;

int main()
{
    int* pointer = new int(99); // the anonymous int is at the address contained in 'pointer'
 
   // once we have the address, if we want, we can create an alias for the unnamed object.
   // in effect we tell the compiler: there is an object of type int at the address contained in pointer.
   // from now on, when we say 'name' we mean this object
    const int& name = *pointer ; 

    i = *pointer ; // assign the value of the unnamed int whose address is in 'pointer' 
                         //to the int identified by 'i'

    i = name ; // assign the value of the int identified by 'name' to the int identified by 'i'
}


EDIT: An array is closely associated with a pointer (which holds the address of a memory location) for the same reason:
int array[20] ; - we have 20 objects of type int, but we do not have a name for each of them.

array[5] ; - the anonymous object which is at a memory location 5 'ints' away from the object right at the beginning of the array. array decays to a pointer to object at position 0, the memory location of the other objects in the array can be specified relative to the memory location of the object right at the beginning.
Last edited on
Allocation in general means marking some part of the memory as used. Allocation can be static or dynamic. Static allocation is very fast but very restricted. You must know how much memory you need and you must know that you will only need it in one function. Dynamic allocation has no such restrictions. You get do decide how much to allocate and when to delete.

Here's a basic task that cannot be done without dynamic memory and pointers: Write a class/struct representing an array that can be resized, without an artificial limit.

As for pointers, if you were to learn more about the lowest level of programming (assembly), you'd see pointers all over the place. Pointers are a big source of errors, though, so higher level languages try to hide them. Also, even if you can get away without pointers now, it will not be like that when your projects become more complex.
Thanks guys.

@JLBorges
Thanks for breaking down the idea of a simple assignment. Connecting that with the idea of allocating and pointers help me understanding why you can't allocate without them.

@hamsterman
Thanks for the extra info. In that case I'll be sure to master them.
Topic archived. No new replies allowed.