|
|
|
|
|
|
| Author |
Message |
silentwizardlord@hotmail. *nix forums beginner
Joined: 08 Feb 2005
Posts: 2
|
Posted: Tue Feb 08, 2005 6:03 pm Post subject:
Re: Incorrectly storing Ints?
|
|
|
Thank you Michael for your very prompt, and accurate reply.
I tested the code:
player p;
memcpy(&p, data.get_data(), sizeof(player));
int money = p.money;
And it worked perfectly. I knew it was something simple.
Once again, thank you
Micah |
|
| Back to top |
|
 |
Michael Cahill *nix forums Guru Wannabe
Joined: 26 May 2005
Posts: 219
|
Posted: Tue Feb 08, 2005 4:11 am Post subject:
Re: Incorrectly storing Ints?
|
|
|
Hi Micah,
I'm not exactly sure what's causing your problem, but here are a few
things to check. When you do the put:
ret = db2.put(0,&key2,&data2,DB_NOOVERWRITE);
are you sure that ret == 0? You didn't show that code. If the key
already exists with different data, you'd get DB_KEYEXIST and the
database would be unchanged.
When you do the get:
player *pmon;
pmon = (player *)data.get_data();
int money = pmon->money;
You are making an assumption that the data in the Dbt is aligned. In
other words, you are assuming that it is okay to cast it to a pointer
of a different type, which may not be valid in this case. Instead, try
this:
player p;
memcpy(&p, data.get_data(), sizeof(player));
int money = p.money;
You can avoid copying the data using the DB_DBT_USERMEM flag on the
data Dbt passed in to the Db::get call.
I hope this helps.
Michael. |
|
| Back to top |
|
 |
silentwizardlord@hotmail. *nix forums beginner
Joined: 08 Feb 2005
Posts: 2
|
Posted: Tue Feb 08, 2005 3:26 am Post subject:
Incorrectly storing Ints?
|
|
|
Hello,
I want to start out by saying I've spent the better half of today
googling for any information I could find. I've spent most of this
night searching this group for any information. So I have researched
this some.
Here is my problem. I have no problem storing and recalling char
pointers. But every attempt to store and retrieve an int has failed.
Well, not so much failed as returned the wrong int. I feed my code an
int with the value of 10000, and I get an int with the value of
21474952, a slight difference.
Here is some of the code.
//BEGINING OF CODE
class player{
public:
int money;
};
....
player newplayer;
newplayer.money = 10000;
std::cout << "IN: MONEY: " << newplayer.money << std::endl; // IN:
MONEY: 10000
....
Db db2(0, 0);
db2.set_error_stream(&std::cerr);
db2.set_errpfx("sm.h");
db2.set_pagesize(1024);
db2.set_cachesize(0, 32 * 1024, 0);
Dbt key2("cppclass",(strlen("cppclass") + 1));
Dbt data2(&newplayer,sizeof(newplayer));
db2.open(NULL,tmppath.c_str(),"maininfo", DB_BTREE, DB_CREATE, 0664);
try {
ret = db2.put(0,&key2,&data2,DB_NOOVERWRITE);
}
catch (DbException &dbe) {
std::cerr << "sm.h: " << dbe.what() << "\n";
}
db2.close(0);
....
Db db(0, 0);
db.set_error_stream(&std::cerr);
db.set_errpfx("sm.h");
db.set_pagesize(1024);
db.set_cachesize(0, 32 * 1024, 0);
Dbt key((char *)"cppclass", (strlen("cppclass") + 1));
Dbt data;
db.open(NULL, tmppath.c_str(),"maininfo", DB_BTREE, DB_CREATE, 0664);
try {
db.get(NULL,&key, &data, 0);
}
catch (DbException &dbe) {
std::cerr << "sm.h: " << dbe.what() << "\n";
}
db.close(0);
player *pmon;
pmon = (player *)data.get_data();
int money = pmon->money;
std::cout << "OUT: Money: " << money << std::endl; // OUT: Money:
21474952
//END OF CODE
I'm on Win32(XP) using MinGW32, BerkeleyDB 4.3 (locally built with
MinGW) I'm sure it is just a problem with my coding, but I cannot find
a single helpful tutorial, example, FAQ, forum, or anything that solves
my problem.
Also, the program compiles and links just fine, I'm just not getting
the value I tell it.
Thanks in advance.
Micah |
|
| Back to top |
|
 |
Google
|
|
| Back to top |
|
 |
|
|
The time now is Thu Jan 08, 2009 5:09 am | All times are GMT
|
|
Web Advertising | Credit Cards | Mobile Phone | Loans | Loans
|
|
Copyright © 2004-2005 DeniX Solutions SRL
|
|
|
|
Other DeniX Solutions sites:
Unix/Linux blog |
electronics forum |
medicine forum |
science forum |
|
|
Privacy Policy
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|