Jump to content
  • Advertisement
Sign in to follow this  
Rubiks14

Specific question about pointers to arrays

This topic is 2248 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

#include <iostream>
#include <string>
class Item{
private:
std::string m_name;
int m_value;
public:
Item(std::string name, int value): m_name(name), m_value(value){}
friend std::ostream &operator<<(std::ostream &out, Item i){
out << "Item name: " << i.m_name << "\n";
out << "Value: " << i.m_value << "\n";
return out;
}
};
int main(){
Item *inventory[3] = {new Item("Sword", 200),new Item("Shield", 150),new Item("Potion", 50)};
int arraySize = sizeof(inventory) / sizeof(inventory[0]);
for(int i = 0; i < arraySize; i++){
std::cout << *inventory;
}
std:: cout << "\n\n";
for(int i = 0; i < arraySize; i++){
std::cout << **(inventory + i);
}
delete [] inventory;
}


I have two questions about the code above. Mainly, I'm wondering why my delete [] inventory line returning an error. I know it's that line because if I comment it out it doesn't return an error. It only started erroring after I added the second way of displaying the inventory, which brings me to my second question.

Why am I having to use a pointer to a pointer to get the contents of each item in inventory using pointer notation? I don't have any more than 1 pointer in the program and I only have to use a single pointer in the top loop.

After further experimenting before posting this it appears that the pointer to a pointer is also what is causing the error. It's weird though because when I used that notation in the top section without the bottom section added I was no longer receiving the error.

Share this post


Link to post
Share on other sites
Advertisement

I have two questions about the code above. Mainly, I'm wondering why my delete [] inventory line returning an error. I know it's that line because if I comment it out it doesn't return an error. It only started erroring after I added the second way of displaying the inventory, which brings me to my second question.

You can't delete[] inventory because you didn't allocate it with new[]. You called new three times, one per element of the array, and you need to call delete three times as well. The array itself lives on the stack and doesn't need to be deleted.


Why am I having to use a pointer to a pointer to get the contents of each item in inventory using pointer notation? I don't have any more than 1 pointer in the program and I only have to use a single pointer in the top loop.

This question I don't get. You need to use a pointer to do pointer arithmetic and access different elements of the array, and then you need to dereference once again because the elements of the array are pointers.


After further experimenting before posting this it appears that the pointer to a pointer is also what is causing the error. It's weird though because when I used that notation in the top section without the bottom section added I was no longer receiving the error.

Once you are invoking undefined behavior (like when you call delete[] on a pointer that was not allocated using new[]), all bets are off: the code may or may not crash, and that doesn't mean anything.

Share this post


Link to post
Share on other sites
Thank you for the quick response.

The first part I didn't think about. The second question i figured out after rereading the code again.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!