Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > illegal memory access with function pointers

Reply
Thread Tools

illegal memory access with function pointers

 
 
Roshni
Guest
Posts: n/a
 
      01-03-2006
Hi,

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

Thanks,
Roshni

 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      01-03-2006
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)
 
Reply With Quote
 
 
 
 
Michael Mair
Guest
Posts: n/a
 
      01-03-2006
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.
 
Reply With Quote
 
Michael Mair
Guest
Posts: n/a
 
      01-03-2006
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.
 
Reply With Quote
 
Malcolm
Guest
Posts: n/a
 
      01-03-2006
"Michael Mair" <(E-Mail Removed)> 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).


 
Reply With Quote
 
reetdipti@gmail.com
Guest
Posts: n/a
 
      01-04-2006

Malcolm wrote:
> "Michael Mair" <(E-Mail Removed)> 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

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      01-04-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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)
 
Reply With Quote
 
reetdipti@gmail.com
Guest
Posts: n/a
 
      01-04-2006
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

 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      01-04-2006

(E-Mail Removed) 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

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-05-2006
"Nick Keighley" <(E-Mail Removed)> 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) (E-Mail Removed) <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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
Memory leak even after deleting memory pointers from vector cham C++ 5 09-25-2008 10:30 AM
Smart pointers and member function pointers n2xssvv g02gfr12930 C++ 3 11-27-2005 10:51 AM
void pointers & void function pointers Peter Goddard C Programming 3 05-16-2005 09:44 PM
Template specialization of pointers with function pointers Phil C++ 1 09-16-2003 02:17 AM



Advertisments