illegal memory access with function pointers

Discussion in 'C Programming' started by Roshni, Jan 3, 2006.

  1. Roshni

    Roshni Guest

    Hi,

    I wanted to know how do function pointers sometime access illegal
    memory access ? Could any one give me an example ?

    Thanks,
    Roshni
     
    Roshni, Jan 3, 2006
    #1
    1. Advertising

  2. Roshni said:

    > Hi,
    >
    > I wanted to know how do function pointers sometime access illegal
    > memory access ? Could any one give me an example ?


    Always glad to oblige.

    int main(void)
    {
    typedef int (f)(void);
    f *p = (f *)0x12345678UL;
    (*p)();
    return 0;
    }

    Example run:

    $> ./foo
    Segmentation fault (core dumped)

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Jan 3, 2006
    #2
    1. Advertising

  3. Roshni

    Michael Mair Guest

    Roshni wrote:
    > Hi,
    >
    > I wanted to know how do function pointers sometime access illegal
    > memory access ? Could any one give me an example ?


    What do you mean?
    1) function pointers not pointing to functions
    2) accessing storage you do not "own" when using function
    pointers
    ....

    Your question is not exactly clear.

    However, this may help you:
    void qux (int foo, double bar)
    {
    ....
    }
    .....

    void (*example)(int, double) = NULL;
    ....
    if (baz) {
    example = qux;
    }
    ....
    (*example)(1, 42.0);

    Leaving out the initialization of example leads
    to a similar situation.


    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
     
    Michael Mair, Jan 3, 2006
    #3
  4. Roshni

    Michael Mair Guest

    Richard Heathfield wrote:
    > Roshni said:
    >
    >>Hi,
    >>
    >>I wanted to know how do function pointers sometime access illegal
    >>memory access ? Could any one give me an example ?

    >
    > Always glad to oblige.


    Always? Impressive :)

    <snip: nice example>

    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
     
    Michael Mair, Jan 3, 2006
    #4
  5. Roshni

    Malcolm Guest

    "Michael Mair" <> wrote
    >> I wanted to know how do function pointers sometime access illegal
    >> memory access ? Could any one give me an example ?

    >
    > What do you mean?
    > 1) function pointers not pointing to functions
    > 2) accessing storage you do not "own" when using function
    > pointers
    >

    The function pointer could point to non-executable code, causing the machine
    to refuse to load it into an instruction pointer register.

    The function pointer could point to non-existent memory, causing an error
    when the machine tries to fetch an instruction from the non-existent place.

    The function pointer could point to garbage, causing random data to be
    interpreted as instructions and executed. This will almost certainly lead to
    a crash.

    The function pointer to point to a function with a human introduced error in
    it, which cause the illegal memory access. (This is the same a regular
    memory access error).
     
    Malcolm, Jan 3, 2006
    #5
  6. Roshni

    Guest

    Malcolm wrote:
    > "Michael Mair" <> wrote
    > >> I wanted to know how do function pointers sometime access illegal
    > >> memory access ? Could any one give me an example ?

    > >
    > > What do you mean?
    > > 1) function pointers not pointing to functions
    > > 2) accessing storage you do not "own" when using function
    > > pointers
    > >

    > The function pointer could point to non-executable code, causing the machine
    > to refuse to load it into an instruction pointer register.
    >
    > The function pointer could point to non-existent memory, causing an error
    > when the machine tries to fetch an instruction from the non-existent place.
    >
    > The function pointer could point to garbage, causing random data to be
    > interpreted as instructions and executed. This will almost certainly lead to
    > a crash.
    >
    > The function pointer to point to a function with a human introduced error in
    > it, which cause the illegal memory access. (This is the same a regular
    > memory access error).


    Thank you for all your replies. I wanted the example where function
    pointer could point to non-existent memory.

    Thanks,
    Roshni
     
    , Jan 4, 2006
    #6
  7. said:

    > Thank you for all your replies. I wanted the example where function
    > pointer could point to non-existent memory.


    Oh, you mean mine. Well, you are most welcome to it. Please return it when
    you've finished with it, so that other people can benefit from the same
    example afterwards.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Jan 4, 2006
    #7
  8. Roshni

    Guest

    Hi,
    Thank you for your response.

    void foo()
    {
    int a;
    a=2;

    }

    int main(void)
    {
    void (*a)();
    a = &foo;
    a();
    a = (&foo) - 20;
    a();
    return 0;
    }

    Is this a valid proram which tries to access illegal memory space ?

    Thanks,
    Rosh
     
    , Jan 4, 2006
    #8
  9. wrote:
    > Hi,
    > Thank you for your response.
    >
    > void foo()
    > {
    > int a;
    > a=2;
    >
    > }
    >
    > int main(void)
    > {
    > void (*a)();
    > a = &foo;
    > a();
    > a = (&foo) - 20;
    > a();
    > return 0;
    > }
    >
    > Is this a valid proram which tries to access illegal memory space ?


    well, maybe. You aren't permitted to do pointer arithmetic on function
    pointers. That is &foo - 20 is not defined by the standard. It exhibits

    undefined behaviour. Your C implementation is permitted to do whatever
    it pleases.

    But there's a good chance it will crash. Think about it, do you really
    expect 'a' to be pointing at a sensible piece of code after doing
    *that*?

    --
    Nick Keighley
     
    Nick Keighley, Jan 4, 2006
    #9
  10. "Nick Keighley" <> writes:
    [...]
    > well, maybe. You aren't permitted to do pointer arithmetic on function
    > pointers. That is &foo - 20 is not defined by the standard. It exhibits
    > undefined behaviour. Your C implementation is permitted to do whatever
    > it pleases.


    It's not just undefined behavior, it's a constraint violation,
    requiring a diagnostic (see C99 6.5.6p3).

    Once the diagnostic is issued, an implementation is free to compile
    and run the program, which *then* produces undefined behavior. (One
    of the infinitely many things the C implementation is permitted to do
    is to document the behavior of arithmetic on function pointers.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jan 5, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Phil
    Replies:
    1
    Views:
    666
    llewelly
    Sep 16, 2003
  2. Replies:
    1
    Views:
    12,883
    gemel
    Mar 22, 2007
  3. Peter Goddard

    void pointers & void function pointers

    Peter Goddard, May 16, 2005, in forum: C Programming
    Replies:
    3
    Views:
    529
    Peter Goddard
    May 16, 2005
  4. n2xssvv g02gfr12930

    Smart pointers and member function pointers

    n2xssvv g02gfr12930, Nov 26, 2005, in forum: C++
    Replies:
    3
    Views:
    487
    n2xssvv g02gfr12930
    Nov 27, 2005
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    714
Loading...

Share This Page