ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi,
i have found a c hashmap implementation and i am trying to write a program on it- i try to isolate the useful code snippets. The header file of the hashmap constains the following:
Code:
//hashmap.h
...
typedef void *any_t; //value of any type
extern map_t hashmap_new();
extern int hashmap_get(map_t in, int key, any_t *arg);
...
The way i want the hashmap to be used is to store an integer as a key AND an integer as the corresponding value. I have tried many things (that didn't work), but the last one is the following:
Code:
....
string dec2bin(int* num); //decimal to binary
int bin2dec(string* s); //binary to decimal
...
any_t codeExtracted;
map_t cpress=hashmap_new();
string mixed;
...
if (hashmap_get(cpress,bin2dec(&mixed),&codeExtracted)!=MAP_MISSING)
{
bfOut.PutBit((int)codeExtracted); //line 167
}
Here's the compile output:
lzw2.cpp:167: error: cast from ‘void*’ to ‘int’ loses precision
How can i deal with this?
Last edited by kpachopoulos; 01-23-2007 at 04:28 AM.
if (hashmap_get(cpress,bin2dec(&mixed),&codeExtracted)!=MAP_MISSING)
{
bfOut.PutBit((int)codeExtracted); //line 167
}
Contrary to what others have said I think the pointer to pointer is probably correct and it's not unusual to pass a double pointer to fill with data(normally when memory is allocated in the function).
if put bit requires an int
Code:
bfOut.PutBit((int)**codeExtracted);
if put bit requires a pointer to int
Code:
bfOut.PutBit((int*)*codeExtracted);
[edit]You say this is based on C but is the above code C++ as this post would suggest?
void * is safe to cast to any pointer type, or intptr_t. void * cannot be derefereced. Casting a void * to something other than the original pointer type and then dereferencing it is undefined.
No mater, earlier or later there is a compiler version that takes that as an error even if you find a solution. May be the Object oriented layout of QT can help you.
void is nothing only a word with nothing behind (code, compilation) so void* is an error in its self - it is only a declaration. You only can handle pointers(all names) and values(that is behind a name) nothing else and void is not even NULL.
At least the linker has to solve all names especially extern(s) and globals.
What you also can handle is where a name1 beginns and where it ends by declaring a name2 behind that name1 and using name2 as end reference.
Explain more exactly what you want to do.
void says that the value of Register eax(32bit) rax(64bit) is invalid on return !!! nothing else. But you can put the value of eax in your int if you want - but that would only be the result of the last math operation or value of if().
Thanks, everything is really useful.First of all, i forgot to mention, that the PutBit requires an int as an argument (PutBit(int i)).
@dmail: the hashmap implementation is in C.
"bfOut.PutBit(*(int **)codeExtracted)" seems to be right and the compiler doesn't complain. However, it will take me some time to test it...
Can somebody explain/give an example of what "void * cannot be derefereced" means? I know what references and pointers are, but i cannot clearly understand that... i'm not native english speaking
I think our thread headline is a result of the error you get and totally dismatches your wish the thing should work.
Code:
typedef void *any_t;
Is a pointer to any function (reference). So the use of any_t garants that you don't get something on return. And I think that's not what you want. So take "void *" to the trash and type "int" instead. That would then also not be the right thing, but it makes you better understanding how it works.
You use:
but codeExtracted is yet a pointer, why you give in a pointer to a pointer - I can't believe that hashmap_get can handle that - I can't see what hashmap_get does. Because of that I wrote that above and my other message.
Are you really sure that you don't talk about java ?
The last line is dereferincing(* operator) a void pointer. There's probably a better explanation why it's wrong in K&R than I can spit out, go get a copy and read it cover-to-cover.
dmail: Thinking about it a bit more, I realized that dereferencing a void ** is legal, which may be what you were getting at.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.