473,804 Members | 2,249 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2555
>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.supe rnews.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
9034
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 programmer that you pretend to be, why don't you just write some? (And oh, void main is still not allow by the C++ standard.) Seeming as how you tried to quote the standard in an attempt to pretend you're right, I'll quote the standard to once...
15
4171
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: struct node { struct node *next; void *data; };
6
1705
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 approach when you need a function to be able to modify a (void*) arg it has been passed: struct pointer { void *p;
188
17466
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
5
3527
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 accepts a void* key argument, and uses the functions above to store this argument. It returns something (linked to the key) that the caller can store a value in. The actual key argument is always of the same pointer type (as seen in the caller,...
3
3995
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 trouble? I don't mean trouble like in 'not compiling' but trouble like in 'crashing hard'.
2
1305
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 a function pointer, but i sorta want to go the other way around. thanks class Foo { public: void bar() {} };
12
5473
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
1719
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 expect that one would instead use
0
9714
marktang
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...
0
9594
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10346
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9173
agi2029
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...
1
7635
isladogs
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...
0
5531
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...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4308
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
2
3832
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.