Get struct address from struct "method"

This is a discussion on Get struct address from struct "method" within the C Programming forums, part of the General Programming Boards category; Hi, if I have some struct thing like Code: struct Foo { void (*method)(); } void foo_method() {} // and ...

  1. #1
    gna
    gna is offline
    Registered User
    Join Date
    Jan 2010
    Posts
    5

    Get struct address from struct "method"

    Hi,

    if I have some struct thing like

    Code:
    struct Foo {
      void (*method)();
    }
    
    void foo_method() {}
    
    // and later, in to-be-runned-code
    struct Foo foo;
    foo.method = &foo_method;
    foo.method();
    My question: Is there any chance for figuring out the memory address of `foo` in `foo_method` (which has been called from/as member of `foo`)?

    I've highlighted "any" to emphasize that I'm not scared about "ugly" methods to do this, not about code execution right before the call, not by Assembler code execution and so on.

    Thanks! :-)

    P.S.: I'm not interested in using such hacks in productive code... it's just academic interest ;-)

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    The memory address of "foo" doesn't exist until foo_method is called and the local variables are allocated memory on the stack.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    gna
    gna is offline
    Registered User
    Join Date
    Jan 2010
    Posts
    5
    Sorry, but I don't get
    • why the memory address of foo "doesn't exist until foo_method is called" (foo exists before, so its address exists, too?)
    • what I should do with the second information about the local variables on the stack.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Oh I see, you are calling foo_method after the variable is created. In that case, can't you just pass the address of "foo" to foo_method as a parameter? Or store the address in a local variable?
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't need the & operator, since the name of a function, like the name of an array, can be used as a pointer to that object; at least in this case. Also, () in function definitions is different than () in function declaration. A () in a prototype isn't the same as () in the actual function.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    gna
    gna is offline
    Registered User
    Join Date
    Jan 2010
    Posts
    5
    bithub, no, that would be too simple (and not the stuff I requested in my first question).

    quzah, I know the & thing but I like to write that operator down explicitly, just for readability, you know. The other thing, you're right, it should have been `void (*method)(void);`.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Quote Originally Posted by gna View Post
    bithub, no, that would be too simple (and not the stuff I requested in my first question).
    Of course, simple is overrated.

    If you really want, you can get the address of the stack pointer (using embedded assembly) and add or subtract (depending if your implementation's stack grows up or down) values to get the address of "foo" on the stack. Of course the offset from the stack pointer depends on a lot of factors which will change depending on what compiler you use, what architecture you are using, and what compiler optimizations are set. I ran a little test on my machine, and found that the magic number (in my case) was 56 bytes with optimizations turned off, and 4 bytes with them turned on.
    bit∙hub [bit-huhb] n. A source and destination for information.

  8. #8
    gna
    gna is offline
    Registered User
    Join Date
    Jan 2010
    Posts
    5
    Quote Originally Posted by bithub View Post
    Of course, simple is overrated.
    Like said before, this is for academic/testing interest/purposes only.

    If you really want, you can get the address of the stack pointer (using embedded assembly) and add or subtract (depending if your implementation's stack grows up or down) values to get the address of "foo" on the stack. Of course the offset from the stack pointer depends on a lot of factors which will change depending on what compiler you use, what architecture you are using, and what compiler optimizations are set. I ran a little test on my machine, and found that the magic number (in my case) was 56 bytes with optimizations turned off, and 4 bytes with them turned on.
    So I would crawl through the call stack... but how do I recognize the memory range that belongs to the `foo` struct in the stack if there are other objects/variables stored?

  9. #9
    Registered User
    Join Date
    Dec 2009
    Posts
    2
    hi gna, ur code is quite interesting but can u please expllain it a bit more so that i (am a beginner ;-) ) can understand it in a better way

  10. #10
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Quote Originally Posted by gna View Post
    but how do I recognize the memory range that belongs to the `foo` struct in the stack if there are other objects/variables stored?
    Make the first member of "foo" to be an integer, and assign it a unique value. Then just scan the stack memory looking for that value in your function. Of course it is impossible to choose a value that is 100% unique on the stack, but that's why this is an exercise in futility
    bit∙hub [bit-huhb] n. A source and destination for information.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,169
    Quote Originally Posted by quzah View Post
    You don't need the & operator, since the name of a function, like the name of an array, can be used as a pointer to that object; at least in this case. Also, () in function definitions is different than () in function declaration. A () in a prototype isn't the same as () in the actual function.


    Quzah.
    I, too, consider explicitly using & good practice. I do believe the reason it isn't required on functions is due to backwards compatibility.

    But gna, is there a reason you are using C for this? Is there no chance of using a language that supports member functions natively such as C++?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  12. #12
    gna
    gna is offline
    Registered User
    Join Date
    Jan 2010
    Posts
    5
    bithub, hm, thanks, I'll try.

    Elysia, yes I could, but like said before, that would be too simple.

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,169
    Oh, I must have missed that.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with structs and malloc!
    By coni in forum C Programming
    Replies: 20
    Last Post: 09-14-2009, 05:38 PM
  2. Writing array, to file
    By zootreeves in forum C Programming
    Replies: 9
    Last Post: 09-08-2007, 05:06 PM
  3. I thought pointers were pointers...
    By keira in forum C Programming
    Replies: 19
    Last Post: 08-15-2007, 11:48 PM
  4. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 07:18 AM
  5. Binary Search Tree
    By penance in forum C Programming
    Replies: 4
    Last Post: 08-05-2005, 05:35 PM

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