Casting from a void pointer 
Author Message
 Casting from a void pointer

I'm having trouble recovering an int value from a function that requires the
data be passed as an void pointer.  It goes like this:

void FunctionB(void* Parameter) {
    int* pValue = (int*)Parameter;
    ...

Quote:
}

void FunctionA(int iValue) {
    FunctionB((void*)&iValue);

Quote:
}

At least that's one variation.  What I want is too recover the integer
stored in 'iValue' from within FunctionB() and I'm having ZERO luck at
getting the pointer stuff correct.  Can someone please pass on some help?!

Thanks for your time!



Sat, 02 Apr 2005 23:30:21 GMT  
 Casting from a void pointer

Quote:
> I'm having trouble recovering an int value from a function that requires the
> data be passed as an void pointer.  It goes like this:
> void FunctionB(void* Parameter) {
>     int* pValue = (int*)Parameter;
>     ...
> }
> void FunctionA(int iValue) {
>     FunctionB((void*)&iValue);
> }
> At least that's one variation.  What I want is too recover the integer
> stored in 'iValue' from within FunctionB() and I'm having ZERO luck at
> getting the pointer stuff correct.  Can someone please pass on some help?!
> Thanks for your time!

What's stopping you from writing something like:
int iValue = *Parameter;
inside FunctionB()?

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/
"Hasta la Vista, Abie!"
   - Bart Simpson



Sat, 02 Apr 2005 23:33:38 GMT  
 Casting from a void pointer

Quote:

>> I'm having trouble recovering an int value from a function that requires the
>> data be passed as an void pointer.  It goes like this:
>> void FunctionB(void* Parameter) {
>>     int* pValue = (int*)Parameter;
>>     ...
>> }
>> void FunctionA(int iValue) {
>>     FunctionB((void*)&iValue);
>> }
>> At least that's one variation.  What I want is too recover the integer
>> stored in 'iValue' from within FunctionB() and I'm having ZERO luck at
>> getting the pointer stuff correct.  Can someone please pass on some help?!
>> Thanks for your time!
> What's stopping you from writing something like:
> int iValue = *Parameter;
> inside FunctionB()?

Pointers to void not being dereferencable, that's what. I *meant*,
natch: What's stopping you from writing something like:
int iValue = *pValue;
inside FunctionB()?

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/
"The large yellow ships hung in the sky in exactly the same way that bricks
don't."
   - Douglas Adams



Sat, 02 Apr 2005 23:34:45 GMT  
 Casting from a void pointer

Quote:

> Pointers to void not being dereferencable, that's what. I *meant*,
> natch: What's stopping you from writing something like:
> int iValue = *pValue;
> inside FunctionB()?

The interface to FunctionB() is not under my influence - it only accepts
void pointers. Is there not someway to cast the pointer to another type, and
then retrieve the value??  As:

    int* pInt = (int*)pVoid;
    int iInt = *pInt;

That doesn't seem to work - but anything similair???!



Sat, 02 Apr 2005 23:58:45 GMT  
 Casting from a void pointer

Quote:

>> Pointers to void not being dereferencable, that's what. I *meant*,
>> natch: What's stopping you from writing something like:
>> int iValue = *pValue;
>> inside FunctionB()?

> The interface to FunctionB() is not under my influence - it only accepts
> void pointers. Is there not someway to cast the pointer to another type, and
> then retrieve the value??  As:
>     int* pInt = (int*)pVoid;
>     int iInt = *pInt;
> That doesn't seem to work - but anything similair???!

Could you please tell me *what* is stopping this:

void FunctionB(void *pVoid) {
  int *pInt = (int*)pVoid;
  int iInt = *pInt;
  /* do stuff with iInt */

Quote:
}

from working? Does it compile correctly? Does it run correctly?
What exactly happens when you try it? "That doesn't seem to work"
is a bit unhelpful as a failure diagnostic.

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/
"Immanuel Kant but Genghis Khan."
   - The Official Graffitist's Handbook



Sun, 03 Apr 2005 00:02:44 GMT  
 Casting from a void pointer

Quote:
> Could you please tell me *what* is stopping this:

> void FunctionB(void *pVoid) {
>   int *pInt = (int*)pVoid;
>   int iInt = *pInt;
>   /* do stuff with iInt */
> }

The result is that 'iInt' ends up having a value way out of line - 5962964 -
even though I've set iValue (in FunctionA()) to be 20 or 3, etc.


Sun, 03 Apr 2005 00:08:53 GMT  
 Casting from a void pointer

Quote:

> I'm having trouble recovering an int value from a function that requires the
> data be passed as an void pointer.  It goes like this:

> void FunctionB(void* Parameter) {
>     int* pValue = (int*)Parameter;
>     ...
> }

> void FunctionA(int iValue) {
>     FunctionB((void*)&iValue);
> }

> At least that's one variation.  What I want is too recover the integer
> stored in 'iValue' from within FunctionB() and I'm having ZERO luck at
> getting the pointer stuff correct.  Can someone please pass on some help?!

#include <stdio.h>

void FunctionB(void *Parameter)
{
  int *pValue = Parameter;
  int Value = *pValue;
  printf("%d\n", Value); /* see? :-) */

Quote:
}

void FunctionA(int iValue)
{
  FunctionB(&iValue);

Quote:
}

--

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton


Sun, 03 Apr 2005 00:05:22 GMT  
 Casting from a void pointer
Thanks a million, Richard - I guess it's obvious I'm a noob!

You're help is deeply appreciated!


Quote:

> #include <stdio.h>

> void FunctionB(void *Parameter)
> {
>   int *pValue = Parameter;
>   int Value = *pValue;
>   printf("%d\n", Value); /* see? :-) */
> }

> void FunctionA(int iValue)
> {
>   FunctionB(&iValue);
> }

> --



Sun, 03 Apr 2005 00:24:15 GMT  
 Casting from a void pointer

Quote:

> I'm having trouble recovering an int value from a function that requires the
> data be passed as an void pointer.  It goes like this:

> void FunctionB(void* Parameter) {
>     int* pValue = (int*)Parameter;
>     ...
> }

> void FunctionA(int iValue) {
>     FunctionB((void*)&iValue);
> }

> At least that's one variation.  What I want is too recover the integer
> stored in 'iValue' from within FunctionB() and I'm having ZERO luck at
> getting the pointer stuff correct.  Can someone please pass on some help?!

The casts to and from void pointer are unnecessary, that will be done
implicitly.

On the whole, you are almost there, everything you have should work ok.
As far as I can tell from the standard, its perfectly alright to take
the address of a parameter. It's essentially an automatic variable
declared at the start of the function.

Unfortunately, your FuncB is incomplete, so we can't tell what your
actual problem is. Don't leave out code. You don't know what is wrong,
so don't presume to cut down the information you give us.

To access the value of a pointer, use the dereferencing operator (*).

Quote:
> void FunctionB(void* Parameter) {
>     int* pValue = Parameter;

      printf ("Value passed was %d\n", *pValue); /* access the pointer
value */          > }

Brian Rodenborn



Sun, 03 Apr 2005 00:21:32 GMT  
 Casting from a void pointer

Quote:
>> Could you please tell me *what* is stopping this:

>> void FunctionB(void *pVoid) {
>>   int *pInt = (int*)pVoid;
>>   int iInt = *pInt;
>>   /* do stuff with iInt */
>> }

> The result is that 'iInt' ends up having a value way out of line - 5962964 -
> even though I've set iValue (in FunctionA()) to be 20 or 3, etc.

This is interesting. Could you post the code which exhibits this
problem? I am suspecting a mismatch between type widths or byte
alignments.

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/
"Stronger, no. More seductive, cunning, crunchier the Dark Side is."
   - Mika P. Nieminen



Sun, 03 Apr 2005 00:44:49 GMT  
 Casting from a void pointer

Quote:

> > The result is that 'iInt' ends up having a value way out of line -
5962964 -
> > even though I've set iValue (in FunctionA()) to be 20 or 3, etc.

> This is interesting. Could you post the code which exhibits this
> problem? I am suspecting a mismatch between type widths or byte
> alignments.

The bulk of the code is really too large too post, however I can give you a
little more:

void FTPSendTrigger(void* pVoid) {
        int* pInt = (int*)pVoid;
        int iInt = *pInt;    // iInt is now incorrect
        cout << "\n" << iInt;
        // FTP Code follows, but is cut out

Quote:
}

void FTPTriggerPostActive(int iIndex) {
        // Use FTP info to establish connection using WinInet
        _beginthread(FTPSendTrigger, 8192,  &iIndex);     // iIndex is the
Selected ROW in a List View control (Win32)
Quote:
}



Sun, 03 Apr 2005 01:35:04 GMT  
 Casting from a void pointer

Quote:

> > Pointers to void not being dereferencable, that's what. I *meant*,
> > natch: What's stopping you from writing something like:
> > int iValue = *pValue;
> > inside FunctionB()?

> The interface to FunctionB() is not under my influence - it only accepts
> void pointers. Is there not someway to cast the pointer to another type, and
> then retrieve the value??  As:

>     int* pInt = (int*)pVoid;
>     int iInt = *pInt;

> That doesn't seem to work - but anything similair???!

    It ought to work, assuming `pVoid' is the `void*' argument
passed to FunctionB().  Are you, perhaps, calling FunctionB()
in some peculiar way?  You've been posting paraphrases of your
code thus far; maybe it's time to whittle your code down to the
shortest complete fragment that demonstrates the problem, and
then post that exact fragment.

    By the way, you could also have written

        int iInt = *(int*)pVoid;

... instead of the two lines you've shown.  But if the two-line
form isn't working, neither will this: the problem lies somewhere
else.

--



Sun, 03 Apr 2005 01:30:43 GMT  
 Casting from a void pointer

Quote:
> You've been posting paraphrases of your
> code thus far; maybe it's time to whittle your code down to the
> shortest complete fragment that demonstrates the problem, and
> then post that exact fragment.

O.k., here's the minimalist version that is still exhibiting the problem.
Compiler is Watcom v11.0c C/C++...

#include <stdlib.h>
#include <iostream.h>
#include <windows.h>
#include <process.h>

void FunctionB(void* pVoid) {
        int* pInt = (int*)pVoid;
        int iInt = *pInt;
        cout << "\nFunctionB() iInt: " << iInt;

Quote:
}

void FunctionA(int iIndex) {
        cout << "\nSTART ==> FunctionA() iIndex: " << iIndex;
        _beginthread(FunctionB, 8192, &iIndex);
        cout << "\nEND   ==> FunctionA() iIndex: " << iIndex;

Quote:
}

void main(int argc, char* argv[]) {
        int iIndex = 10;
        cout << "\nSTART ==> main() iIndex: " << iIndex;
        FunctionA(iIndex);
        cout << "\nEND   ==> main() iIndex: " << iIndex;

Quote:
}

The output is as follows:

START ==> main() iIndex: 10
START ==> FunctionA() iIndex: 10
END   ==> FunctionA() iIndex: 10
END   ==> main() iIndex:
FunctionB() iInt: 424352810



Sun, 03 Apr 2005 02:58:44 GMT  
 Casting from a void pointer

Quote:
>> You've been posting paraphrases of your
>> code thus far; maybe it's time to whittle your code down to the
>> shortest complete fragment that demonstrates the problem, and
>> then post that exact fragment.
> O.k., here's the minimalist version that is still exhibiting the problem.
> Compiler is Watcom v11.0c C/C++...

This is C++ code, and relies on some Windows-specific functionality.
Fortunately, the C++- and Windows-specific parts appear to be only
minor.

Quote:
> #include <stdlib.h>
> #include <iostream.h>
> #include <windows.h>
> #include <process.h>
> void FunctionB(void* pVoid) {
>         int* pInt = (int*)pVoid;
>         int iInt = *pInt;
>         cout << "\nFunctionB() iInt: " << iInt;
> }
> void FunctionA(int iIndex) {
>         cout << "\nSTART ==> FunctionA() iIndex: " << iIndex;
>         _beginthread(FunctionB, 8192, &iIndex);

I suspect that whatever is at fault is happening here at
_beginthread(). Since ISO C has no idea what _beginthread() does,
we cannot help you further in the context of this newsgroup.
What happens if you abandon the _beginthread() call and instead
just write this?
FunctionB(&iIndex);

Quote:
>         cout << "\nEND   ==> FunctionA() iIndex: " << iIndex;
> }
> void main(int argc, char* argv[]) {

Illegal form of main(). Both in C and in C++. Use int main(int argc,
char *argv[]) instead.

Quote:
>         int iIndex = 10;
>         cout << "\nSTART ==> main() iIndex: " << iIndex;
>         FunctionA(iIndex);
>         cout << "\nEND   ==> main() iIndex: " << iIndex;
> }

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/
"C++ looks like line noise."
   - Fred L. Baube III


Sun, 03 Apr 2005 03:06:37 GMT  
 Casting from a void pointer

Quote:

>>You've been posting paraphrases of your
>>code thus far; maybe it's time to whittle your code down to the
>>shortest complete fragment that demonstrates the problem, and
>>then post that exact fragment.

> O.k., here's the minimalist version that is still exhibiting the problem.
> Compiler is Watcom v11.0c C/C++...

> #include <stdlib.h>
> #include <iostream.h>
> #include <windows.h>
> #include <process.h>

> void FunctionB(void* pVoid) {
>         int* pInt = (int*)pVoid;
>         int iInt = *pInt;
>         cout << "\nFunctionB() iInt: " << iInt;
> }

> void FunctionA(int iIndex) {
>         cout << "\nSTART ==> FunctionA() iIndex: " << iIndex;
>         _beginthread(FunctionB, 8192, &iIndex);
>         cout << "\nEND   ==> FunctionA() iIndex: " << iIndex;
> }

Threads! That changes a lot.
iIndex will be destroyed, when FunctionA terminates.
If FunctionB looks at the address you gave it, nothing useful is there.

You must ensure that iIndex still exists when FunctionB is called.
For instance:

void FunctionA(int *piIndex) {
...
  _beginthread(FunctionB, 8192, piIndex);
...

Quote:
}

void main(int argc, char* argv[]) {
   int iIndex = 10;
...
   FunctionA(&iIndex);
...

Quote:
}

Jirka


Sun, 03 Apr 2005 03:05:59 GMT  
 
 [ 23 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Type-cast from a void pointer

2. casting of struct element void pointer casting

3. Casting function pointer to void pointer

4. casting void pointer to be a pointer to a function

5. casting void pointer to be a pointer to a function

6. Casting pinning pointer to void *

7. Casting void pointers

8. Casting function pointer to void*

9. Is explicit casting of void * pointer ever necessary?

10. casting void pointers

11. casting void pointers

12. casting void pointer

 

 
Powered by phpBB® Forum Software