473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

void pointers & void function pointers

Hello,
Is it possible, using casting to promote...

void *Ptr;

To behave like...

void (*FnPtr)(void);

?

i.e. Can the 'void *Ptr' be cast into calling a function once it points to
one?

Cheers

Peter.
Nov 14 '05 #1
3 2522
>Hello,
Is it possible, using casting to promote...
void *Ptr;
To behave like...
void (*FnPtr)(void);
?
i.e. Can the 'void *Ptr' be cast into calling a function once it points to
one?


There is no guarantee of this. MS-DOS provides an example of having
all possible combinations of 16-bit and 32-bit function pointers
and 16-bit and 32-bit data pointers in the "memory models" small,
compact, medium, and large. If you convert a 32-bit pointer to
16-bits there is substantial risk of losing information you can't
get back.

Gordon L. Burditt
Nov 14 '05 #2


Peter Goddard wrote:
Hello,
Is it possible, using casting to promote...

void *Ptr;

To behave like...

void (*FnPtr)(void);

?

i.e. Can the 'void *Ptr' be cast into calling a function once it points to
one?


No, or "not portably." Function pointers can only point
to functions; data pointers can only point to data. If you
try to coerce a pointer of one class to a pointer of the other,
all bets are off -- on some implementations it will work as
you perhaps hoped, while on others you will be unpleasantly
surprised.

(Usually, people who want to make this conversion have
just filled an array with instructions and want to get the
program to execute them. Quite aside from the difficulties
of converting the pointer, on many machines you will face
other obstacles. For example, a machine that "knows" the
I- and D-spaces are distinct may well have separate cache
mechanisms, and may not bother to synchronize them: you may
wind up executing the garbage that occupied the array before
you stored the instructions, which are still sitting in a
data cache and have not yet been flushed back to memory.
Machines that maintain "permission bits" on regions of memory
may not set the "executable" attribute on any data memory you
can access; you'll get some kind of trap when the CPU tries to
fetch in instruction from a non-executable memory area. And
so on, and so on, into endless machine-dependent territory --
and, perhaps, an appreciation of why the C Standard chose not
to try to enter this particular bramble bush.)

--
Er*********@sun.com

Nov 14 '05 #3
Thanks for the replies. I had a nagging feeling that perhaps this was
possible but not advisable.

Cheers

Peter.
"Gordon Burditt" <go***********@burditt.org> wrote in message
news:11*************@corp.supernews.com...
Hello,
Is it possible, using casting to promote...
void *Ptr;
To behave like...
void (*FnPtr)(void);
?
i.e. Can the 'void *Ptr' be cast into calling a function once it points to
one?


There is no guarantee of this. MS-DOS provides an example of having
all possible combinations of 16-bit and 32-bit function pointers
and 16-bit and 32-bit data pointers in the "memory models" small,
compact, medium, and large. If you convert a 32-bit pointer to
16-bits there is substantial risk of losing information you can't
get back.

Gordon L. Burditt

Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

192
by: Kwan Ting | last post by:
The_Sage, I see you've gotten yourself a twin asking for program in comp.lang.c++ . http://groups.google.co.uk/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&th=45cd1b289c71c33c&rnum=1 If you the oh so mighty...
15
by: Stig Brautaset | last post by:
Hi group, I'm playing with a little generic linked list/stack library, and have a little problem with the interface of the pop() function. If I used a struct like this it would be simple: ...
6
by: MackS | last post by:
Hi FAQ 4.9 states that void** cannot be used portably "to pass a generic pointer to a function by reference". I would like to hear from you whether there is anything wrong with the following...
188
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
5
by: Stijn van Dongen | last post by:
A question about void*. I have a hash library where the hash create function accepts functions unsigned (*hash)(const void *a) int (*cmp) (const void *a, const void *b) The insert function...
3
by: bnoordhuis | last post by:
Consider this: int foo(int *a, int *b); int (*bar)(void *, void *) = (void *)foo; How legal - or illegal - is the typecast and are there real-world situations where such code will cause...
2
by: Ray Tayek | last post by:
hi, trying to make an array of function pointers to make delegates with. but the compiler does not like: void (Foo::^p)()=&Foo::bar; i did find an article that showed how to convert a delegate to...
12
by: Bill Pursell | last post by:
The following code generates a compiler warning when compiled with gcc -pedantic: typedef (*FUNC)(int); FUNC f; void * get_f(void) { return &f;
4
by: Oliver Block | last post by:
Hello, sometimes you can read that a function requiring an argument of tpye void ** is submitted something like the following: mystruct **ppval; myfunc((void **)&ppval, ...); I would...
0
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,...
0
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$) { } ...
0
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
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
0
BarryA
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.