Question about pointers to DOUBLEs (VC++6.0)
Hi.
I'm having a problem with parameter passing when it comes to DOUBLE or double and a pointer to this parameter. I have a double variable holding a value and I make a pointer to this value. The problem is: when I pass this pointer to the function, it won't recognize it as a valid double value and crash the extension at this point.
If I call this function this way, it'll work:
Code:
myFunction(1, "Hello!", 20.95);
But this will NOT:
Code:
void* pointer = NULL;
double argument = 20.95;
pointer = reinterpret_cast<double*>(&argument);
myFunction(1, "Hello!", pointer);
I MADE A MISTAKE. IT'S THE POINTER WHICH IS PASSED AS ARGUMENT. SORRY
The third parameter expects a DOUBLE and reinterpret_cast works for almost all variable types I tested(int, long, ulong, BOOL, BSTR, LPSTR) but it seems it doesn't work the same way with DOUBLEs.
Am I passing the pointer in a wrong way into the function? I wanna pass the value of 20.95 directly as in the first example above but I need this pointer. Please help.
Thanks for your attention. :)
Re: Question about pointers to DOUBLEs (VC++6.0)
Does it crash as soon as it calls the function or when you in the function casts it back?
Shouldn't it be pointer = reinterpret_cast<void*>(&argument); instead?? Does it crash if you do a static cast or a C "(void*)argument"-cast?
Re: Question about pointers to DOUBLEs (VC++6.0)
Hi, z33z.
I tried static_cast as well. I create this void* pointer because I don't know which variable type it will be passed as argument and the function accepts any types. It works if the argument is an int or LPSTR and I covnert the pointer like this:
Code:
//A pointer to an int
pointer = reinterpret_cast<int*>(argument);
//A pointer to a C string
pointer = reinterpret_cast<LPSTR*>(argument);
//A pointer to a bool value
pointer = reinterpret_cast<BOOL*>(argument);
All above work successfully when passing the pointer as a parameter:
Code:
myFunction(1, "Hello!", pointer);
I'm stuck here because I need to pass a simple double value to a function like this like 20.95 but it's accepted as a wrong parameter, I'm sure and the function fails only in this case. I know it's something to do with the way doubles and floats are stored in memory and retrieved by the pointer but I can't understand how.
Why wouldn't it work like the var types I mentioned above? :(
Thanks for your help so far.
Re: Question about pointers to DOUBLEs (VC++6.0)
But still it should be
pointer = reinterpret_cast<void*>(&argument);
instead of
pointer = reinterpret_cast<double*>(&argument);
Re: Question about pointers to DOUBLEs (VC++6.0)
Really? :D
Thank you!
I'll try that later.
*offers beer*
Re: Question about pointers to DOUBLEs (VC++6.0)
Really! And don't like beer.. ;)
What does the debugger say is the reason for the crash? If you're debugging it in VS.
Re: Question about pointers to DOUBLEs (VC++6.0)
:D
Is there a way to stop the execution of a program from a given line and show the values of the variables while executing the extension? I mean, a breakpoint inside the program.
I'm 98% sure that it's how I'm reinterpret_casting the pointer to the double value. I replaced the type with <void*> like you advised but still the same error. Only when passing doubles (although it comes from a DOUBLE variable and not a double). Everything is fine with other parameters, even strings, integers and BSTR strings. I do the same thing with a DOUBLE and it doesn't accept the parameter.
Since I'm using a COM interface to interact with other objects, the error message doesn't come from VC++ but from the object itself but it tells me that it's a wrong parameter. I can pass 10 integers, 10 strings and it'll work just fine.
If I could make a breakpoint inside the program that would show me the value inside the pointer after I typecasted it, I think this would make things clearer. Is there a way?
Thank you, z33z. Your help is much appreciated.
Re: Question about pointers to DOUBLEs (VC++6.0)
Make a breakpoint as normal by hitting F9 on the correct line, then search for CopyRun.bat in the official SDK docs for what to do next in order to debug :)
Re: Question about pointers to DOUBLEs (VC++6.0)
If I understood your problem correctly you are passing a pointer instead of a value, and thats causing the problem.
Try using:
Code:
myFunction(1, "Hello!", *((double*)pointer));
And see if it solves your problem.
Re: Question about pointers to DOUBLEs (VC++6.0)
Jamie: Thank you. I'll try that. :)
Werbad: The problem is I can't modify the way the function is called because at the time it's called, I don't know which variable it is which is passed as argument. It could be a double or it could be anything else. So I just call it like:
myFunction(1, "Hello!", pointer);
where pointer can be anything depending on what parameters are passed. If I could make the pointer act for doubles just the same for all other variables before the pointer is passed. I tried just about anything, even unsafe methods(although some say that reinterpret_cast may cause some troubles). Thanks for your help though. :(