Pointing a function pointer to a variable?

This is a discussion on Pointing a function pointer to a variable? within the C++ Programming forums, part of the General Programming Boards category; Hi, all How do you “point” a function pointer to an array of byte code? I have converted a series ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    162

    Unhappy Pointing a function pointer to a variable?

    Hi, all

    How do you “point” a function pointer to an array of byte code? I have converted a series of assembly instructions to byte codes (I don’t want to use inline assembly), and want my function pointer to point to that array of byte codes. The problem is that the VC++ compiler won’t cast from chars to the defined function pointer. The following code might explain better:

    Code:
    int (* FuncPtr)(int x, int y); // the function pointer
    char ByteCode[] = {...}; // the byte code array
    
    FuncPtr = ByteCode; // point the function pointer to the byte code array
    The problem is that VC++ won’t covert from char array to the function definition. I tried adding “(void *)” when pointing but it still wouldn’t convert.
    We haven't inherited Earth from our parents; instead we have borrowed her from our children - old Indian saying.

  2. #2
    Registered User
    Join Date
    Dec 2002
    Posts
    162
    But it is possible to do it with "(void *)" in Dev-C++

    FuncPtr = (void *) ByteCode;
    We haven't inherited Earth from our parents; instead we have borrowed her from our children - old Indian saying.

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    162
    Thanks that worked
    We haven't inherited Earth from our parents; instead we have borrowed her from our children - old Indian saying.

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    162
    I have read that this is very risky (mixing code and data), why is that?
    We haven't inherited Earth from our parents; instead we have borrowed her from our children - old Indian saying.

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    162
    How can I prevent this or somehow add my array of byte codes to the instruction memory area of my program during runtime? I don't want to use inline assembly, becouse I want the array of byte codes to be variable, becouse the array will load the byte codes from a file.
    Last edited by Aidman; 07-06-2003 at 01:05 PM.
    We haven't inherited Earth from our parents; instead we have borrowed her from our children - old Indian saying.

  6. #6
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    What you're doing is basically making a simple (relatively speaking) version of a self-modifying .exe... or at least you're trying. I don't know much ASM, but perhaps you could try taking this one step farther by having your code actually modify the .exe. You'd probably want a big section that was full of nop (no operation) to record over with the new data, but I really have no idea if this will work like you want. In theory, I think it should, but in practice, self modifying executables that are anything but trivial is an unpredictable type of thing.

    Having said that... unless you're trying this just for fun or to see if it can be done, it would probably be better to find another way to solve the problem. If you're doing this to satisfy your intellectual curiousity - bravo, carry on. If you're trying to write a serious program that you need to work - find another way.

    Good luck.
    Away.

  7. #7
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    I thought the text segment was read-only in 32-bit protected mode? Or is this totally OS dependant? *looks at vVv*

  8. #8
    btq
    btq is offline
    julie lexx... btq's Avatar
    Join Date
    Jun 2002
    Posts
    161
    I am no Windows programmer, but the Unix FAQ has an example of how to make the text segment writable using the POSIX mprotect() function.
    IIRC, on windows you can call the function VirtualProtect to
    to make a text-segment writable for these types of operations.

    /btq
    ...viewlexx - julie lexx

  9. #9
    Registered User
    Join Date
    Dec 2002
    Posts
    162
    don't know much ASM, but perhaps you could try taking this one step farther by having your code actually modify the .exe. You'd probably want a big section that was full of nop (no operation) to record over with the new data, but I really have no idea if this will work like you want.
    I had tried that on another program before and it worked (atleast in windows). But becouse I want the number of bytecodes to be variable, it will be difficult to adjust to all sizes.


    Have you considered stuffing that code into a DLL and loading it into memory using LoadLibrary() and extracting function addresses from it using GetProcAddress()?
    I am thinking about that, it seems like the only way. But one difficulty would be to make the dll in assembler, thats very messy.
    We haven't inherited Earth from our parents; instead we have borrowed her from our children - old Indian saying.

  10. #10
    btq
    btq is offline
    julie lexx... btq's Avatar
    Join Date
    Jun 2002
    Posts
    161
    what is it more exactly that you want to do ?
    Are you writing self-modifying-code ?
    If so, putting the code on the stack and calling it is
    perfectly accebtable(like you did).

    Although if you just want some asm-code and don't
    want to use inline-asm there are several better ways:
    Use a dll (already mentioned), it isn't harder
    than writing an asm-application. Or you can link with the .obj
    which comes from the asm-code you want to execute.

    hmmm...I guess my real question is: what is it you're trying to
    do ?


    /btq
    ...viewlexx - julie lexx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 12:45 PM
  2. Replies: 9
    Last Post: 06-09-2008, 10:53 AM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 04:36 AM
  4. Compiler "Warnings"
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 04-24-2005, 02:09 PM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21