How does dynamic pointer allocation work?

closed account (Ey80oG1T)
Suppose you have this code:

1
2
3
4
string* pointerVariable = new string[10];

string name = "Squid";
pointerVaribale = &name;


But what does this mean?

In the first line, I allocated space for 10 strings. In the second line, I declared a variable. In the third line, I assigned the pointer to that variable.

However, didn't I allocate space for 10 strings? What does string[10] mean exactly?
Last edited on
string* pointerVariable = new string[10];
Means "I have this identifier that is a string pointer, and I will set it to point to an array of strings somewhere in memory. But at compile time, that memory address isn't allocated yet, so I will find some space in memory that isn't being used, and create that space."

string name = "Squid"
Means "somewhere in memory, allocate space for a string data type, and store a certain value in that memory location".

pointerVariable = &name
Means "instead of having my string pointer point to the 10 arrays of string, I will want to point it to another memory location". Though you do this, this means those 10 strings of arrays aren't deleted. You should probably delete the array of strings first.
Last edited on
closed account (Ey80oG1T)
Oh... so at the start the pointer is pointing at 10 memory locations, however, I changed the pointer so that it only points to 1 memory location?
One shouldn't use dynamic memory at all, unless one is creating a new kind of container. Use a smart pointer.

STL containers and classes like std::string already store their data dynamically, so no need to use new with them. Prefer to use a std::vector instead of plain arrays.
closed account (Ey80oG1T)
Gotcha
This can seem a lot harder than it needs to be. This is somewhat because everyone discusses pointers colloquially. Too much loose terminology.

In the first line, I allocated space for 10 strings.
Line 1 creates 10 new strings. Not merely space for those strings: memory space is allocated and 10 empty strings are constructed, or the operation failed (and an exception is thrown). Assuming success, pointerVariable is left pointing to the beginning of a block of memory containing 10 adjacent string objects.

Oh... so at the start the pointer is pointing at 10 memory locations, however, I changed the pointer so that it only points to 1 memory location?
Line 4 leaves pointerVariable pointing at the beginning of a block of memory containing one string object.
Last edited on
@OP
Probably a bit late but keep in mind that writing some code and trying things out can assist. You won't break anything :)

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <string>

using namespace std;

int main()
{
    int how_many = 0;
    cout << "I want to be able to store some strings" << '\n';
    cout << "How many (make it a lazy 3) ? ";
    cin >> how_many; // ENTER 3
    
    
    
    // MAKE ENOUGH (new) SPACE AND A (*) POINTER TO THE FIRST ONE
    string* pointerVariable_A = new string[how_many];
    string* pointerVariable_B = new string[how_many];
    
    
    
    // POPULATE THE ARRAY - ALTERNATIVE A
    pointerVariable_A[0] = "one";
    pointerVariable_A[1] = "two";
    pointerVariable_A[2] = "three";
    
    for(int i = 0; i < how_many; i++)
        cout << pointerVariable_A[i] << '\n';
    cout << '\n';
    
    
    
    // POPULATE THE ARRAY - ALTERNATIVE B
    *pointerVariable_B = "four";
    *(pointerVariable_B + 1) = "five";
    *(pointerVariable_B + 2) = "six";
    
    for(int i = 0; i < how_many; i++)
    {
        cout << *pointerVariable_B << '\n';
        pointerVariable_B++;
    }
    cout << '\n';
    

    
    string name = "Squid";
    pointerVariable_A = &name;
    cout << *pointerVariable_A << '\n'; // DEREFERENCING (*)
    
    
    
    return 0;
}
Topic archived. No new replies allowed.