Sign in to follow this  
Rubiks14

Specific question about pointers to arrays

Recommended Posts

Rubiks14    143
[code]#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[i];
}
std:: cout << "\n\n";
for(int i = 0; i < arraySize; i++){
std::cout << **(inventory + i);
}
delete [] inventory;
}
[/code]

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
alvaro    21247
[quote name='Rubiks14' timestamp='1333077923' post='4926548']
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.
[/quote]
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.

[quote name='Rubiks14' timestamp='1333077923' post='4926548']
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.
[/quote]
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.

[quote name='Rubiks14' timestamp='1333077923' post='4926548']
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.
[/quote]
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
Rubiks14    143
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this