Z
zl2k
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:ata2(): regionId(-1), isLandscape(false)
{
for (int i = 0; i < 16; i++)
parameters = 1;
}
Data2::~Data2()
{
}
int main(){
char buffer[512] = "abc.bin";
ofstream myfile;
Data2 *dataArray = new Data2[10];
myfile.open (buffer, ios:ut | 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;
}
valgrind error message:
==16557== 1 errors in context 1 of 1:
==16557== Syscall param writev(vector[...]) points to uninitialised
byte(s)
==16557== at 0x40007F2: (within /lib/ld-2.7.so)
==16557== by 0x5A70707: std::__basic_file<char>::xsputn_2(char
const*, int, char const*, int) (in /usr/lib/libstdc++.so.6.0.9)
==16557== by 0x5A171E9: std::basic_filebuf<char,
std::char_traits<char> >::xsputn(char const*, int) (in /usr/lib/libstdc
++.so.6.0.9)
==16557== by 0x5A42DE0: std:stream::write(char const*, int) (in /
usr/lib/libstdc++.so.6.0.9)
==16557== by 0x81535AD: main (data2.cpp:36)
==16557== Address 0x61540b9 is 9 bytes inside a block of size 1,364
alloc'd
==16557== at 0x4022F14: operator new[](unsigned)
(vg_replace_malloc.c:268)
==16557== by 0x81534AE: main (data2.cpp:31)
--16557--
--16557-- supp: 131 dl-hack3-1
==16557==
==16557== IN SUMMARY: 1 errors from 1 contexts (suppressed: 131 from
1)
==16557==
==16557== malloc/free: in use at exit: 528 bytes in 10 blocks.
==16557== malloc/free: 534 allocs, 524 frees, 49,265 bytes allocated.
==16557==
==16557== searching for pointers to 10 not-freed blocks.
==16557== checked 1,449,340 bytes.
==16557==
==16557== LEAK SUMMARY:
==16557== definitely lost: 0 bytes in 0 blocks.
==16557== possibly lost: 0 bytes in 0 blocks.
==16557== still reachable: 528 bytes in 10 blocks.
==16557== suppressed: 0 bytes in 0 blocks.
Thanks for your comments.
zl2k
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:ata2(): regionId(-1), isLandscape(false)
{
for (int i = 0; i < 16; i++)
parameters = 1;
}
Data2::~Data2()
{
}
int main(){
char buffer[512] = "abc.bin";
ofstream myfile;
Data2 *dataArray = new Data2[10];
myfile.open (buffer, ios:ut | 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;
}
valgrind error message:
==16557== 1 errors in context 1 of 1:
==16557== Syscall param writev(vector[...]) points to uninitialised
byte(s)
==16557== at 0x40007F2: (within /lib/ld-2.7.so)
==16557== by 0x5A70707: std::__basic_file<char>::xsputn_2(char
const*, int, char const*, int) (in /usr/lib/libstdc++.so.6.0.9)
==16557== by 0x5A171E9: std::basic_filebuf<char,
std::char_traits<char> >::xsputn(char const*, int) (in /usr/lib/libstdc
++.so.6.0.9)
==16557== by 0x5A42DE0: std:stream::write(char const*, int) (in /
usr/lib/libstdc++.so.6.0.9)
==16557== by 0x81535AD: main (data2.cpp:36)
==16557== Address 0x61540b9 is 9 bytes inside a block of size 1,364
alloc'd
==16557== at 0x4022F14: operator new[](unsigned)
(vg_replace_malloc.c:268)
==16557== by 0x81534AE: main (data2.cpp:31)
--16557--
--16557-- supp: 131 dl-hack3-1
==16557==
==16557== IN SUMMARY: 1 errors from 1 contexts (suppressed: 131 from
1)
==16557==
==16557== malloc/free: in use at exit: 528 bytes in 10 blocks.
==16557== malloc/free: 534 allocs, 524 frees, 49,265 bytes allocated.
==16557==
==16557== searching for pointers to 10 not-freed blocks.
==16557== checked 1,449,340 bytes.
==16557==
==16557== LEAK SUMMARY:
==16557== definitely lost: 0 bytes in 0 blocks.
==16557== possibly lost: 0 bytes in 0 blocks.
==16557== still reachable: 528 bytes in 10 blocks.
==16557== suppressed: 0 bytes in 0 blocks.
Thanks for your comments.
zl2k