View Single Post
Old Jul 14th, 2007, 5:54 AM   #5
Soulstorm
Hobbyist Programmer
 
Soulstorm's Avatar
 
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 239
Rep Power: 3 Soulstorm is on a distinguished road
Thanks a lot, Dawei. But:

Quote:
Originally Posted by DaWei View Post
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.
cpp Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <fstream>
  5.  
  6. using std::vector;
  7. using std::string;
  8. using std::cout;
  9.  
  10. struct TestStruct{
  11. string alpha;
  12. string beta;
  13. vector<string> gamma;
  14. };
  15.  
  16. struct TestStruct2 {
  17. char alpha[32];
  18. char beta[32];
  19. };
  20.  
  21. int main (int argc, char* argv[]) {
  22.  
  23. TestStruct *kkk = new TestStruct;
  24. kkk->beta = "hello world!";
  25. kkk->alpha = "hello again!";
  26. kkk->gamma.push_back("hello third");
  27. kkk->gamma.push_back("hello fourth!!!");
  28.  
  29. FILE *fp = fopen("myfile.soul","wb");
  30.  
  31. fwrite(kkk,sizeof(TestStruct),1,fp);
  32. fclose(fp);
  33. delete kkk;
  34.  
  35. TestStruct *lll = new TestStruct;
  36.  
  37. fp = fopen("myfile.soul","rb");
  38. fread(lll,sizeof(TestStruct),1,fp);
  39.  
  40. fclose(fp);
  41. cout << lll->gamma[1];
  42. delete lll;
  43.  
  44. return 0;
  45. }

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.
__________________
Project::Soulstorm (personal homepage)
Soulstorm is offline   Reply With Quote