pointers - Convert (void **) to object in C++ -
i trying convert (void**) object queue in c++.
in 1 file hashtablevoid.cc use method find to:
bool hashtablevoid::find( const char * key, void ** data) { // add implementation here int = hash(key); hashtablevoidentry * node = _buckets[i]; while(node){ if(strcmp(node->_key,key) == 0) { *data = node->_data; return true; } node = node->_next; } return false; } and in ircserver.cc used
void ircserver::sendmessage(int fd, const char * user, const char * password, const char * args) { //get user's room //write room messages char * temp; temp = strdup(args); void ** q; queue<char*> data; const char * room = //found room; communication.find(room, q); data = (queue<char*>) ((char *)q); data.push(temp); //make sure 100 elements or less in list while(data.size() > 100) data.pop(); } i creating void ** q pass parameter communication. variable communication hashtable key room name , value unique queue of messages. having trouble converting void object queue. can't change find method.
two problems can see directly:
first call find without initializing q, doesn't point anywhere. in find function when dereference *data dereference unknown pointer leading undefined behavior. think you're supposed declare q single pointer, , pass address of find function (emulating pass reference). like
void *q; ... communication.find(room, &q); however, since you're programming in c++ don't see reasong use double-pointer emulate pass reference, since c++ have built-in:
bool hashtablevoid::find( const char * key, void *& data); the above declares argument data reference pointer.
the second problem assignment pointer q variable data:
data = (queue<char*>) ((char *)q); what have (without changes above), q pointer pointer, , try use single pointer, try cast value. pointer not value, , can never be.
maybe mean
data = *reinterpret_cast<std::queue<char*>*>(q); // after change detailed above
Comments
Post a Comment