Hi,
I wanted to know how do function pointers sometime access illegal
memory access ? Could any one give me an example ?
Thanks,
Roshni 9 7084
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**********@i nvalid.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**********@i nvalid.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_Keit h) 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
the error and exit) just when malloc_m and free_m start.
(I use a 1100 static array for write the path (like a queue)
of called function, operator, etc and write using '+' where is
find the memory error)
2) if the pointer to free is alredy...
|
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
be written to, since its contents are undefined.
The program is allocating a new piece of data, and
the previous contents aren't relevant. This memory
|
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 believe the problem centers
around my handling of strings, arrays, pointers and dynamic memory
allocation. Here is the problem I'm trying to solve:
I want to fill a list box with a list of Project Names from a database
(in Palm this is more...
|
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 statements, such as _fmalloc,
farmalloc, far char pointers, etc., to differentiate it from near memory. I
know that this is true under Win32. Is this true in the ANSI standard, as
well? Also, should I assume that any statements dealing with far...
|
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:
"At a certain point in the code you may be unsure if a particular
block is no longer needed. If you free() this piece of memory, but
continue to access it (probably via a second pointer to the same
| |
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 reference a correct/valid structure
member upon that allocated memory?
If I allocate memory, for example like this way:
|
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 per convenience and access them.
I was told this would save some memory. I dont understand the logic
behind this, as i`ve declared variables as global (assuming i`ve
declared the block in main() ) this would always b a residual data for
access at...
|
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 finally get answers to the following
newbie questions:
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
| |
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |