That's pretty much it. I need to use a class with a private data member pointer, and use that pointer to create an array and then use that array to put the object in it, then take care of deleting the pointer.
use that pointer to create an array and then use that array to put the object in it, then take care of deleting the pointer.
use that pointer to create an array
pointer = newint[1000];
that array to put the object in it
pointer[0] = someIntegerObject;
then take care of deleting the pointer.
delete [] pointer; // Deallocate memory, lose forever what was just stored in the array This bit seems a bit odd - why throw it all away immediately after creating it?
What I'm trying to simulate is an online shopping cart. I'd like to create my item objects and use dynamic memory to add these items to an on demand created array. Thats probably where I'm lost. I can't keep the data I put in the pointer created array.
If you're not forced to use new and delete, just use a vector. It takes care of its own memory, expanding as needed.
1 2 3 4 5
// Make a vector of objects, named contentsOfShoppingCart
vector<objects> contentsOfShoppingCart;
contentsOfShoppingCart.push_back(someObject); // add someObject to end of vector
contentsOfShoppingCart.push_back(someOtherObject); // add someOtherObject to end of vector
Right now, I've got my container class creating a private array of size 100. What I would want to do is change that from a static declaration in my class into a pointer....
I use my pointer to create an array the size of one for adding a single object to it. But when I try to add my item object to the item array, it doesn't keep the data saved to the object array.
So you pass this function the item you want to store, and it copies it to a brand new local variable, which is promply destroyed when the function ends.
I suggest that instead of copying it to a brand new local variable, you instead store it in the array you made, perhaps like this:
If you use the same pointer to point to a new array with a new size, you lose the array you were originally pointing at. You'll need to copy everything from the original array you're pointing to, to a temporary array, say, Temp[size]...then you'll have to delete the memory your pointer was pointing at, and then use the new operator, and copy all the information from Temp[] to your new block of data.
You could try creating another pointer to your original block of data, but the moment you use your original pointer to delete it, it's essentially the same as deleting your old pointer as well. That's why the idea of using some sort of temporary spot is a better bet. Lemme know how it goes!
I would recommend a different method, maybe just an inline method or something. Destructors have special meaning to the compiler, calling the destructor causes your destructor function to execute once, but, after the closing brace (}) of your destructor, the compiler will do a few special things. For one, destructors are built for the sole purpose of destroying an object, and de-allocating it from memory. I believe once the destructor is called, it's like giving the compiler the "OK" to store new information at the address the object was allocated at.
So to sum up, NO, do not explicitly call the destructor. Definitely not from within another local function. The only time it is safe to explicitly call a destructor is from an outside function, and is usually only ever necessary when your object has been mapped into some absolute address you're aware of.
dynamically allocated objects, for example, when you use the delete keyword on them, the compiler will call the destructor of that object.
EDIT:
Also, if your class derives any other classes, a call to the destructor will also trigger calls to all the base classes' destructors as well.
So what I want to do is use my pointer to point at a new array of objects, with the bigger size, then copy from the smaller array to the larger one, with a for loop, and then delete the memory used for the smaller array?
Err..... would I need to copy my original array into a temporary local array?
void Container::expandStorage ()
{
Item* newArray = new Item[size*2]; // Create new array, twice the size
memcpy (newArray, Items, size * sizeof(Item)); // Copy old into new
size = size * 2;
delete Items; // delete old array
Items = newArray; // set Items pointer to point to the new array
}
So memcpy would be a faster way to copy from one array to another?
EDIT :
Also, when I'm in my IDE and I use the debugger, usually you need to delete that memory you used in the heap, does that memory, if not deleted, take up actual memory space and cause actual problems outside your IDE?