0

When we create a array of integers we do it like this:

int main() {
int x;
cout << "Enter size of array"
cin >> x;
int* myArray;
myArray = new int[x]
}

we assign the asterisks next to array, we are assigning it as a array of pointers right?

If I make a array of nodes where:

struct Node {
string Name;
int Age;
}

I ran some code to experiment with it and understand it more and I tried to do just like the array sample but create a array with nodes.

int main() {
Node* sumShit[5];
    Node* America = new Node();
    America->age = 16;
    America->Name = "America";
    sumShit[0] = America;
    Node* Japan = new Node();
    Japan->age = 15;
    Japan->Name = "Japan";
    sumShit[1] = Japan;

    cout << "[" << sumShit[1]->Name << ", " << sumShit[1]->age << "]";

}

Everything printed out fine with pointers but then I did it also without pointers, where I just stored node properties in the Node:

Node myNodeShit[5];
    Node Poop;
    Poop.age = 16;
    Poop.Name = "Poop";
    myNodeShit[0] = Poop;
    sortArrayName(myNodeShit, 5);
    printArray(myNodeShit, 5);

And this also worked, however whats the advantages to using pointers and just storing it within the node. When it comes to algorithms, sorting and using memory, is there a preferred way. Im trying to figure why it would be better to have it as a array of pointers to nodes vs a array of nodes.

5
  • As for why using pointers may be better than arrays, think about if you declare an array inside a function, and return a pointer to that array, then that can lead to undefined behavior as the array goes out of scope when the function returns and the pointer is no longer valid. If you allocated memory with new inside the function, and then returned that pointer, then it will be okay since the memory will still be allocated when the function returns. Commented Oct 21, 2014 at 8:09
  • Please mind your language. Commented Oct 21, 2014 at 8:23
  • 1
    Any reason why you aren't using the STL containers ? Commented Oct 21, 2014 at 8:37
  • I do not know how to use STL containers. Commented Oct 21, 2014 at 21:58
  • And thanks Joachim, that makes sense, I'm going to try STL containers now. Commented Oct 22, 2014 at 0:11

1 Answer 1

1

Using pointers allows for greater flexibility in updating / modifying existing data because the data is only declared once in memory. From here, you can use pointers to make changes from anywhere in your code.

Additionally, using pointers conserves memory whereas creating the actual nodes will make copies of said node. This becomes apparent when you're passing the array into a function. In this case the array of nodes (without pointers) becomes a local copy pushed onto the call stack. When the function returns, you'll lose any modification you made to the node. Conversely, using pointers will save the node's state.

When in doubt, try to use pointers where you can.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, that was really clear. When in doubt, use pointers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.