title: C++: casting to void* and back
body: <p><strong>* ---Edit - now the whole sourse*</strong></p>
  
  <p>When I debug it on the end, "get" and "value" have different values! Probably, I convert to void* and back to User the wrong way?</p>
  
  <pre><code>#include &lt;db_cxx.h&gt;
  #include &lt;stdio.h&gt;
  
  struct User{
  User(){}
  int name;
  int town;
  User(int a){};
  inline int get_index(int a){
      return town;
  } //for another stuff
  };
  int main(){ 
  try {
  DbEnv* env = new DbEnv(NULL);
  env-&gt;open("./", 
      DB_CREATE | DB_INIT_MPOOL | DB_THREAD | 
  DB_INIT_LOCK | DB_INIT_TXN | DB_RECOVER | DB_INIT_LOG, 0);
  Db* datab = new Db(env, 0);
  datab-&gt;open(NULL, "db.dbf", NULL, DB_BTREE, DB_CREATE | DB_AUTO_COMMIT, 0);
  
  Dbt key, value, get;
  char a[10] = "bbaaccd";
  User u;
  u.name = 1;
  u.town = 34;
  key.set_data(a);
  key.set_size(strlen(a) + 1 );
  value.set_data((void*)&amp;u);
  value.set_size(sizeof(u));
  get.set_flags(DB_DBT_MALLOC);
  
  DbTxn* txn;
  env-&gt;txn_begin(NULL, &amp;txn, 0);
  datab-&gt;put(txn, &amp;key, &amp;value, 0);
  datab-&gt;get(txn, &amp;key, &amp;get, 0);
  txn-&gt;commit(0);
  User g;
  g = *((User*)&amp;get);
  printf("%d", g.town);
  getchar();
  return 0;
  }catch (DbException &amp;e){
      printf("%s", e.what());
      getchar();
  }
  </code></pre>
  
  <p><strong>solution</strong></p>
  
  <p>create a kind of "serializator" what would convert all POD's into void* and then will unite these pieces</p>
  
  <p>PS Or I'd rewrite User into POD type and everything will be all right, I hope.</p>
  
  <p><strong>Add</strong></p>
  
  <p>It's strange, but... I cast a defenetly non-pod object to void* and back (it has std::string inside) and it's all right (without sending it to the db and back). How could it be? And after I cast and send 'trough' db defenetly pod object (no extra methods, all members are pod, it's a simple struct {int a; int b; ...}) I get back dirted one. What's wrong with my approach?</p>
  
  <p><strong>Add about week after first 'add'</strong></p>
  
  <p>Damn... I've compiled it ones, just for have a look at which kind of dirt it returnes, and oh! it's okay!... I can't ! ... AAh!.. Lord... A reasonable question (in 99.999 percent of situations right answer is 'my', but... here...) - whos is this fault? My or VSs?</p>