Technology (Read Only) > Programming

(C++) wtf is this code really doing?

(1/1)

Alkaline:
I saw this article
--- Code: ---www.cplusplus.com/reference/new/operator%20delete[]/
--- End code ---
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?




--- Code: ---// 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;
}
--- End code ---

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.

blαh2355:
When I try to compile, it gives me this error on line 10.


--- Code: ---error C2864: 'MyClass::z' : only static const integral data members can be initialized within a class
--- End code ---
and

--- Code: ---IntelliSense: data member initializer is not allowed
--- End code ---

Reading up Data Structures http://www.cplusplus.com/doc/tutorial/structures/  it seems that struct declares an abstract class so you're not supposed to initialize the variable. I got rid of the initialization.

With std::cin.get(), I looked it up and it seems std::cin.ignore().get() works.


--- Code: ---// 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;

};

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.ignore().get();
delete[] pt;
return 0;
}

--- End code ---

It seems to work afterwards when I run it. The problem though, it gives a huge error and dumps a debug code. I think it's some memory corruption or something, not sure though.


--- Code: ---Windows has triggered a breakpoint in help.exe.

This may be due to a corruption of the heap, which indicates a bug in help.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while help.exe has focus.

The output window may have more diagnostic information.
--- End code ---

Navigation

[0] Message Index

Go to full version