简体   繁体   English

C ++:转换为void *并返回

[英]C++: casting to void* and back

* ---Edit - now the whole sourse* * ---编辑 - 现在整个过程*

When I debug it on the end, "get" and "value" have different values! 当我在最后调试它时,“get”和“value”具有不同的值! Probably, I convert to void* and back to User the wrong way? 可能,我转换为void *并以错误的方式返回User?

#include <db_cxx.h>
#include <stdio.h>

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->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->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*)&u);
value.set_size(sizeof(u));
get.set_flags(DB_DBT_MALLOC);

DbTxn* txn;
env->txn_begin(NULL, &txn, 0);
datab->put(txn, &key, &value, 0);
datab->get(txn, &key, &get, 0);
txn->commit(0);
User g;
g = *((User*)&get);
printf("%d", g.town);
getchar();
return 0;
}catch (DbException &e){
    printf("%s", e.what());
    getchar();
}

solution

create a kind of "serializator" what would convert all POD's into void* and then will unite these pieces 创建一种“序列化器”将所有POD转换为void *然后将这些片段联合起来

PS Or I'd rewrite User into POD type and everything will be all right, I hope. PS或者我将用户重写为POD类型,我希望一切都会好的。

Add

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). 这很奇怪,但是......我将一个非自然的非pod对象转换为void *并返回(它内部有std :: string)并且它没有问题(没有将它发送到db并返回)。 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. 并且在我投射并发送'trough'db defenetly pod对象(没有额外的方法,所有成员都是pod,它是一个简单的结构{int a; int b; ...})我得到了污点。 What's wrong with my approach? 我的做法有什么问题?

Add about week after first 'add' 首次'添加'后大约一周添加

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? ...... AAh!......主...一个合理的问题(在99.999%的情况下,正确答案是'我',但......这里......) - 这是谁的错? My or VSs? 我的还是VS?

Unless User is a POD this is undefined in C++. 除非UserPOD,否则这在C ++中是未定义的。

Edit: 编辑:

Looking at db_cxx.h , aren't you supposed to do call get_doff() , get_dlen() , and get_data() or something on Dbt instead of just casting (and assigning) it to the user type? 看看db_cxx.h ,你不应该在Dbt上调用get_doff()get_dlen()get_data()或者其他东西,而不是只是将它转换(并分配)给用户类型吗?

Since there is no check of the return value of put(), it could well be there is an error which prevented updating. 由于没有检查put()的返回值,因此可能存在阻止更新的错误。 The documentation indicates quite a few error condtions: 文档指出了相当多的错误条件:

You are almost definitely NOT supposed to cast 'get' directly to a User. 你几乎肯定不应该直接向用户施放'get'。 Instead, extract the data you stored and then cast that. 相反,提取您存储的数据然后转换它。 We can't know for sure unless you share with us the definition of Dbt. 除非您与我们分享Dbt的定义,否则我们无法确定。 Making a guess, based on what we can see: 根据我们可以看到的内容进行猜测:

datab->get(txn, &key, &get, 0);
void* data = get.get_data();
User g = *((User*)data);

Tell us more about Dbt and we can probably help you out more. 告诉我们更多有关Dbt的信息,我们可以帮助您解决更多问题。

I would do something like this: 我会做这样的事情:

User               user;
std::stringstream  dbStotrStream;

dbStoreStream << user;  // Serialize user
std::string        dbStore(bdStoreStream.str());
value.set_data(dbStore.c_str());  
value.set_size(dbStore.lenght());

////  Put in DB

Then extracting it would look like this: 然后解压缩它将如下所示:

//// Get from DB

std::string        dbStore(get.get_data(),get.get_date() + get.get_size());
std::stringstream  dbStoreStream(dbStore);
User              outUser;

 dbStoreStream >> outUSer;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM