Thread: Accessing variables with memory address

  1. #31
    Registered User ITAmember's Avatar
    Join Date
    Mar 2009
    Location
    Nebraska
    Posts
    72
    Now I want to call a function pointer using the same method I used on the int and float, here's the relevant code:

    Code:
    void printer()
    {
    	printf("in printer\n");
    }
    ...
    void *test_funcptr = malloc(8);
    *(int*)((int*)test_funcptr+1) = (int)&printer;
    *((*what_to_put_here)()((int*)test+1))
    I don't know what to put at the what_to_put_here on the last line.

  2. #32
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Somebody please PLEASE say something about offsetof.

  3. #33
    Registered User ITAmember's Avatar
    Join Date
    Mar 2009
    Location
    Nebraska
    Posts
    72
    Could you explain how I could use it?

  4. #34
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    since the compiler can put padding anywhere between members in a struct it wants to, assuming it's right after the first member is not strictly conforming. use offsetof:
    Code:
    #include <stdlib.h>
    
    struct x {
        int x;
        float y;
    } s;
    
    float *fp = (float*) (offsetof(struct x, y) + (char*) &s);
    sorry I didn't just say it.

  5. #35
    Registered User ITAmember's Avatar
    Join Date
    Mar 2009
    Location
    Nebraska
    Posts
    72
    Can I use that even though I ditched the structs? Regarding the function pointer problem, I have this code

    *(*printer)()((int*)test+1);

    where printer refers to a function that takes no arguments and this is the error message I get when I compile

    error C2064: term does not evaluate to a function taking 1 arguments

    Any ideas?

  6. #36
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by ITAmember View Post
    Can I use that even though I ditched the structs? Regarding the function pointer problem, I have this code

    *(*printer)()((int*)test+1);

    where printer refers to a function that takes no arguments and this is the error message I get when I compile

    error C2064: term does not evaluate to a function taking 1 arguments

    Any ideas?
    If printer doesn't take any arguments, what is ((int*)test+1) doing there then?

  7. #37
    Registered User ITAmember's Avatar
    Join Date
    Mar 2009
    Location
    Nebraska
    Posts
    72
    ((int*)test+1) gets the memory address where the function pointer is stored

    EDIT:

    BTW I'm trying to call printer.

  8. #38
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by ITAmember View Post
    ((int*)test+1) gets the memory address where the function pointer is stored

    EDIT:

    BTW I'm trying to call printer.
    printer had better be the memory address where your function is stored, or else you can't call it.

  9. #39
    Registered User ITAmember's Avatar
    Join Date
    Mar 2009
    Location
    Nebraska
    Posts
    72
    After some quick changes it does. New code

    (*((int*)test+1))();

    Now I get

    error C2064: term does not evaluate to a function taking 0 arguments

    Closer, I still don't get what's up with it though

  10. #40
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    test is an int*. Dereferencing it gives, not a pointer to a function, but an int.

    Edit: trying to process the bits and pieces of your code, I think you're trying to do something like this:
    Code:
    void print_int(void * int_ptr) { 
        printf("%d\n", *((int *)int_ptr);
    }
    void print_float(void *float_ptr) { ... }
    .
    .
    .
    void (*printer)(void*);
    printer = print_int;
    printer(int_ptr);
    printer = print_float;
    printer(float_ptr);
    but I don't think any of us are very sure about what you want.
    Last edited by tabstop; 06-27-2009 at 04:56 PM.

  11. #41
    Registered User ITAmember's Avatar
    Join Date
    Mar 2009
    Location
    Nebraska
    Posts
    72
    Here's the whole code with the relevant code in red

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void printer()
    {
    	printf("in printer\n");
    }
    
    int main()
    {
    	int test;
    	void *test_int = malloc(8);
    	void *test_float = malloc(8);
    	void *test_funcptr = malloc(8);
    
    	*(int*)test_int = 0;
    	*(int*)((int*)test_int+1) = 1;
    	*(int*)test_float = 1;
    	*(float*)((int*)test_float+1) = 2;
    	*(int*)test_funcptr = 2;
    	*(int*)((int*)test_funcptr+1) = (int)&printer;
    
    	test = (int)test_funcptr;
    
    	printf("trying to call printer from test_funcptr\n");
    	(*((int*)test+1))();
    
    	printf("assigning test_int to test\n\n");
    	test = (int)test_int;
    	printf("the value of test = %i, the type of test = %i\n", *((int*)test+1), *(int*)test);
    	printf("adding 762 to test\n");
    	*((int*)test+1) += 762;
    	printf("the value of test = %i, the type of test = %i\n", *((int*)test+1), *((int*)test));
    	printf("\nassigning test_float to test\n\n");
    	test = (int)test_float;
    	printf("the value of test = %f, the type of test = %i\n", *(float*)((int*)test+1), *((int*)test));
    	printf("adding 1.234 to test\n");
    	*(float*)((int*)test+1) += 1.234;
    	printf("the value of test = %f, the type of test = %i\n\n", *(float*)((int*)test+1), *((int*)test));
    
    	free(test_int);
    	free(test_float);
    	free(test_funcptr);
    	return 0;
    }
    test holds the memory address of the function pointer as an int. I want to call that function using only test and casting it to the function pointer type.

  12. #42
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You say "casting it to the function pointer type" -- and you have "(int *)" there. I don't think "int" and "function pointer" mean the same thing.

    I haven't tried it. Possibly (void ()) is the cast you want. Also note that compilers are explicitly allowed to destroy function pointers stored as other kinds of pointers.

  13. #43
    Registered User ITAmember's Avatar
    Join Date
    Mar 2009
    Location
    Nebraska
    Posts
    72
    Notice the (int*) in ((int*)test+1) casts test to an int to allow the +1 to move the memory address down 32 bits. That (int*) part is just to get the right memory address. Once I get that memory address I need to dereference it and cast to the function pointer right? That's where I'm stuck.

  14. #44
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by ITAmember View Post
    Notice the (int*) in ((int*)test+1) casts test to an int to allow the +1 to move the memory address down 32 bits. That (int*) part is just to get the right memory address. Once I get that memory address I need to dereference it and cast to the function pointer right? That's where I'm stuck.
    So dereference and cast.

    The cast will probably be way easier if you typedef, something like:
    Code:
    typedef void (*my_function_ptr)();
    so you just have to cast it to my_function_ptr, rather than "void (*)()". (Yes, that's what the type is, a pointer to a function (hence the (*)) returning nothing (hence the void) taking no parameters (hence the ()).

  15. #45
    Registered User ITAmember's Avatar
    Join Date
    Mar 2009
    Location
    Nebraska
    Posts
    72
    It works! Working code snippet without the typedef ((void(*)())*((int*)test+1))(); thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutex and Shared Memory Segment Questions.
    By MadDog in forum Linux Programming
    Replies: 14
    Last Post: 06-20-2010, 04:04 AM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  3. Printing out the memory address
    By Scalpel78 in forum C++ Programming
    Replies: 16
    Last Post: 01-09-2007, 11:01 AM
  4. Changing memory address
    By tzpb8 in forum C Programming
    Replies: 3
    Last Post: 07-26-2006, 09:50 AM
  5. Pointer to specific memory address
    By elnerdo in forum C++ Programming
    Replies: 10
    Last Post: 05-19-2006, 07:35 AM