Thanks a lot, Dawei. But:
Quote:
Originally Posted by DaWei
1) You're getting the errors (the only ones you mention in your post) because the pointer returned by new is not a valid pointer for free. Again, use delete.
|
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using std::vector;
using std::string;
using std::cout;
struct TestStruct{
string alpha;
string beta;
vector<string> gamma;
};
struct TestStruct2 {
char alpha[32];
char beta[32];
};
int main (int argc, char* argv[]) {
TestStruct *kkk = new TestStruct;
kkk->beta = "hello world!";
kkk->alpha = "hello again!";
kkk->gamma.push_back("hello third");
kkk->gamma.push_back("hello fourth!!!");
FILE *fp = fopen("myfile.soul","wb");
fwrite(kkk,sizeof(TestStruct),1,fp);
fclose(fp);
delete kkk;
TestStruct *lll = new TestStruct;
fp = fopen("myfile.soul","rb");
fread(lll,sizeof(TestStruct),1,fp);
fclose(fp);
cout << lll->gamma[1];
delete lll;
return 0;
}
Result:
[Session started at 2007-07-14 12:58:33 +0300.]
SoulFramework(396) malloc: *** Deallocation of a pointer not malloced: 0x3007e0; This could be a double free(), or free() called with the middle of an allocated block; Try setting environment variable MallocHelp to see tools to help debug
SoulFramework(396) malloc: *** Deallocation of a pointer not malloced: 0x3007a0; This could be a double free(), or free() called with the middle of an allocated block; Try setting environment variable MallocHelp to see tools to help debug
hello fourth!!!
SoulFramework has exited with status 0.
Maybe I'm missing something... I think I mentioned that using 'delete' has the same result.