A89: Re: Storing pointers to functions in allocated memory...


[Prev][Next][Index][Thread]

A89: Re: Storing pointers to functions in allocated memory...




> Okay. When I compile my program, I get the following error on the third line
> "Invalid lvalue in assignment", and I can't see why it won't work. I've tried
> different methods, and some compiled but crashed the calc (at least I _THINK_
> it was this line that crashed it, because this is the only line that messes
> with pointers...). This is my latest variation which I _think_ is the closest
> to being correct. I know I somehow need to dereference it at least once, but
> that simply causes more errors (can't dereference a void pointer).
> old_hook_handle is of type HANDLE and old_hook is of type EVENT_HANDLER. If
> the code isn't clear, I'm trying to take the pointer to a function which is
> stored in EV_hook and store that pointer to memory that I allocated.
>
> //<code>
> old_hook=EV_hook;
> old_hook_handle=HeapAlloc(8);  //8 bytes should be 4 bytes
>                                                 //more than I need, but just
>                                                //to be sure...
> (void *)((void *)HeapDeref(old_hook_handle))=old_hook; //add a * between
>  //the two void casts
>  //to dereference the
>  //first (innermost) pointer
> //</code>
>
> It's probably just some stupid mistake I made (I've been programming for
> around 6 hours straight today and am in desperate need of some sleep :) ),
> but thanks for any help anyone can give me.

If you dereference it twice you'll get "pointer to the address that's contained
in the first longword old_hook_handle is dereferenced to", i.e. a random value
that happened to be there when you allocate that memory.

To dereference a void pointer and get the value stored there you use the
"pointer to", i.e. a single "*":
  *((HANDLE *)HeapDeref(old_hook_handle))=old_hook;

Or you could use something else, like:
  memcopy(HeapDeref(old_hook_handle),&old_hook,sizeof(HANDLE));
which should work but is slower.

DISCLAIMER: All of the code above is completely untested.

--
 / Niklas Brunlid
Check out Prosit for the TI-89 / TI-92+ at http://prosit.ticalc.org
Random PQF Quote follows:

FLORIDA (or FLORIDIA): A place where may be found ALLIGATORS, LONG-NECKED
TURTLES and SPACE SHUTTLES. An interesting place which is warm and wet and
there are geese. BACON, LETTUCE AND TOMATO SANDWICHES may be found here
also. A lot more interesting than many other places. The shape when seen
from the air is like a bit stuck on a bigger bit.
        -- From A Scientific Encyclopedia for the Enquiring Young Nome
           by Angalo de Haberdasheri
           (Terry Pratchett, Wings)





References: