jeudi 1 janvier 2015

When insert a new item in stl map, occurs an overwrite to first element


Vote count:

0




Firstly, I have the following code:



// id, value are buffer variables to store the text that returns from `strcpy()`
char *id = (char*)malloc(sizeof(char*) * 20);
char *value = (char*)malloc(sizeof(char*) * 20);

sql->open(dbFile);
sql->query("SELECT * FROM categories");
while(sql->fetch() != SQLITE_DONE){

// copy the text that returns from `sql->getValue`, then convert that text from `const unsigned char*` to `const char*`
strcpy(id, reinterpret_cast<const char*>(sql->getValue(0)));
strcpy(value, reinterpret_cast<const char*>(sql->getValue(1)));

// insert the values that in `id`,`value` as new item
records.insert(pair<char*, char*>(id, value));
}
free(id);
free(value);


The idea of the previous code is copy the text that returns this method sql->getValue(0) by using strcopy(), then convert that text from const unsigned char* to const char*, then store these text in the id, value variables, then insert id, value as a new item. The previous steps occur on each data that gets from database.


Clarify the idea more:

For example, There are three rows fetched from database (fruits, vegetables, sweets).

Naturally, when insert these three data into the map container, the number of items that in the container will be three items first(fruits), second(vegetables), third(sweets).


in my case, when check of the number of items, I find there is one item only, and the item is sweets.


Now, I amazed, where is the rest of items ?



asked 56 secs ago

Lion King

3,055






When insert a new item in stl map, occurs an overwrite to first element

Aucun commentaire:

Enregistrer un commentaire