I saw this article www.cplusplus.com/reference/new/operator%20delete[]/
and decided to modify it to do something. It was a console application that demonstrates deallocating... it's a single main.cpp file.
My main.cpp consists of the following:
MyClass declaration: (constructor, destructor, 3 ints)
void wait: (for aesthetics)
void waitandloadingtext: (for aesthetics)
int main: this is where I am confused and spent a few hours trying to make sense of what is happening
Am I making [desired] new allocations in RAM that are copies of MyClass? Are these all separate and can I write to them? "pt = new MyClass[desired];"?
Why is std::cin.get() near the end being skipped?
Why is this program giving an error when it reaches the end?
// operator delete[] example
#include <iostream>
#include <string>
#include <time.h>
struct MyClass {
MyClass() { std::cout << "MyClass constructed\n"; }
~MyClass() { std::cout << "MyClass destroyed\n"; }
//std::string name = "";
int x,y,z = 0;
};
void wait(unsigned int mseconds)
{
clock_t goal = mseconds + clock();
while (goal > clock());
}
void waitandloadingtext()
{
wait(1000);
std::cout << "3...";
wait(1000);
std::cout << "2...";
wait(1000);
std::cout << "1..." << std::endl;
wait(1000);
}
int main() {
MyClass * pt;
int desired = 0;
std::cout << "Enter how many you'd like to make: ";
std::cin >> (int)desired;
waitandloadingtext();
pt = new MyClass[desired];
//pt[1].x = 5;
//std::cout << pt[1].x;
for (int i = 1; i <= desired; i++)
{
pt[i].x = std::rand();
pt[i].y = std::rand();
pt[i].z = std::rand();
}
for (int i = 1; i <= desired; i++)
{
std::cout << pt[i].x << "\t" << pt[i].y << "\t" << pt[i].z << std::endl;
}
std::cout << "\nPress any key to delete your classes!\n" << std::endl;
std::cin.get();
delete[] pt;
return 0;
}
I'm compiling with VS2013 Professional on Win 7 64-bit. I've been reading into C++ for the past few months but this is the first time I sat down and actually did something with it. However I spent too many hours figuring this out and I just want to take a 24 hour break. I'd appreciate an explanation if anyone can help out. Thanks.