Hi there, I'm having trouble dealing with dynamic arrays and was wondering if someone could tell me where the problem lies. I'm new to C++.
Basically, in the following code, the dynamic array of Fuzzy objects works fine. But when you uncomment out the bit that actually assigns values to these new objects then the program crashes during runtime.
I'm assuming it's something to do with the memory allocation but I can't work out what. Is this dynamic array procedure something that's not suitable for use with objects? If not, what would be the correct way of going about it?
Thanks very much for any help!
#include <iostream>
#include <fstream>
using std::ifstream;
using std::ofstream;
#include <new>
#include <windows.h>
#include <cstdlib>
#include <conio.h>
#include <cstdio>
#include <time.h>
#include <ctime>
using namespace std;
class Fuzzy{
public:
static int n;
Fuzzy () { n++; };
~Fuzzy () { n--; };
int position[48];
int score;
};
int Fuzzy::n = 0;
int main () {
int i;
Fuzzy *ent;
while(1){
cout << "How many objects? ";
cin >> i;
ent = new (nothrow) Fuzzy[i];
if( ent == 0 ){
cout << endl << "No memory!";
}
/*for( int n = 0; n < Fuzzy::n; n++){
for( int i = 0; i < 48; i++ ){
ent[n].position[i] = 24;
}
ent[n].score = 567;
}*/
cout << ent[0].n << endl;
}
system("pause");
return 0;
}