Author Topic: (C++) wtf is this code really doing?  (Read 90 times)

0 Members and 1 Guest are viewing this topic.

Offline Alkaline

  • *****
  • Windows User
    View More Badges!

  • Posts: 699
  • Gender: Male
  • Respect: +430
(C++) wtf is this code really doing?
« on: September 07, 2014, 04:40:39 PM »
0
I saw this article
Code: [Select]
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?



Code: [Select]
// 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.
A Man named William Reed's last living testament says that he is going to read his own will.

Will Reed's will will read "Will Reed will read Will Reed's will"

Offline blαh2355

  • Welcome to aperture science!
  • ***
  • League PlayerWindows User
    View More Badges!

  • Posts: 3921
  • Gender: Male
  • May we perform tests on you?
  • Respect: +978
Re: (C++) wtf is this code really doing?
« Reply #1 on: September 07, 2014, 06:45:15 PM »
0
When I try to compile, it gives me this error on line 10.

Code: [Select]
error C2864: 'MyClass::z' : only static const integral data members can be initialized within a classand
Code: [Select]
IntelliSense: data member initializer is not allowed
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: [Select]
// 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;
}

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: [Select]
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.
« Last Edit: September 07, 2014, 06:55:01 PM by blah2355 »