Hi,
I wanted to know how do function pointers sometime access illegal
memory access ? Could any one give me an example ?
Thanks,
Roshni 9 7041
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)
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.
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" <Mi**********@invalid.invalid> 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 wrote: "Michael Mair" <Mi**********@invalid.invalid> 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 re*******@gmail.com 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)
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 re*******@gmail.com 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" <ni******************@hotmail.com> 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) ks***@mib.org <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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: RoSsIaCrIiLoIA |
last post by:
why not to build
a malloc_m() and a free_m() that *check* (if memory_debug=1) if
1) there are some errors in bounds of *all* allocated arrays
from them (and trace-print the path of code that make...
|
by: jacob navia |
last post by:
In C, we have read-only memory (const), read/write memory
(normal data), and write only memory.
Let's look at the third one in more detail.
Write only memory is a piece of RAM that can only...
|
by: swarsa |
last post by:
Hi All,
I realize this is not a Palm OS development forum, however, even though
my question is about a Palm C program I'm writing, I believe the topics
are relevant here. This is because I...
|
by: Eric A. Johnson |
last post by:
Hi,
The book I am studying from, which comes from 1994, makes mention of far
memory (i.e., that memory beyond 64K). I am under the belief, however, that
far memory no longer needs any special...
|
by: s.subbarayan |
last post by:
Dear all,
I happen to come across this exciting inspiring article regarding
memory leaks in this website:
http://www.embedded.com/story/OEG20020222S0026
In this article the author mentions:...
|
by: lovecreatesbeauty |
last post by:
Hello experts,
1. Does C guarantee the data layout of the memory allocated by malloc
function on the heap. I mean, for example, if I allocate a array of 100
elements of structure, can I always...
|
by: nkrisraj |
last post by:
Hi,
I have a following structure:
typedef struct
{
RateData rdr;
int RateID;
char RateBalance;
} RateInfo;
|
by: hurry |
last post by:
In order to avoid declaring static variables in a function I was asked
to write a scratch memory.
Reserve a block of memory outside the function and assigning pointers
to the memory locations as...
|
by: goose |
last post by:
Hello all
I've written a wrapper for malloc and friends. Its
available from
http://www.lelanthran.com/downloads/os_mem/index.php
The reason for doing writing this so that newbies
can...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
| |