473,467 Members | 1,488 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Valid Function Pointer Casting

I need a macro to compute an offset of an object relative to a base
address. The problem is the object can sometimes be a function pointer
with a base address of 0 but I don't know in advance that the object is
a function. Can it be done?

#define BREF(b,p) ((size_t)((p) ? (char *)(p) - (char *)(b) : 0))

The following code demonstrates that strict c89 doesn't accept casting
a pointer to a function to a pointer to char which makes my above code
invalid.

typedef int (*fn)(void);

void
test(fn f)
{
char *g = (char *)f;
}
int
main(void)
{
test(main);
return 0;
}

cc -std1 q.c
cc: Info: q.c, line 6: In the initializer for g, "f" of type "pointer
to function () returning void", is being converted to "pointer to char".
Such a cast is not permitted by the standard. (nonstandcast)
char *g = (char *)f;
--------------------------^
Nov 14 '05 #1
3 2348
"Michael B Allen" <mb*****@ioplex.com> wrote in message
news:pa**********************************@ioplex.c om...
I need a macro to compute an offset of an object relative to a base
address. The problem is the object can sometimes be a function pointer
with a base address of 0 but I don't know in advance that the object is
a function. Can it be done?
Not portably.
#define BREF(b,p) ((size_t)((p) ? (char *)(p) - (char *)(b) : 0))

The following code demonstrates that strict c89 doesn't accept casting
a pointer to a function to a pointer to char which makes my above code
invalid.


That's correct. Consider architectures where code and data are in seperate
memory areas (eg many microcontrollers): such a conversion is nonsense.
(Usually, if the cast isn't nonsense, it will do what you expect.)

That may not be the only problem: when subtracting pointers, the result is
only defined by the standard if the pointers point to elements of the same
array or one past the last element of the array. (But, similarly, the result
is often what you expect even if this requirement isn't met.)

Alex
Nov 14 '05 #2

"Michael B Allen" <mb*****@ioplex.com> wrote in message > cc -std1 q.c
cc: Info: q.c, line 6: In the initializer for g, "f" of type "pointer
to function () returning void", is being converted to "pointer to char".
Such a cast is not permitted by the standard. (nonstandcast)
char *g = (char *)f;
--------------------------^


Note that the diagnostic output by the compiler is only
an informational. There is still an object produced, and
your program will run as expected. If you wish you can
disable this diagnostic. One way to to do this is
to add -msg_disable nonstandcast to the command line

Ed Vogel
DEC/Compaq/HP C/C++ Engineering
Nov 14 '05 #3
On Sun, 20 Mar 2005 01:51:59 -0500, Michael B Allen
<mb*****@ioplex.com> wrote in comp.lang.c:
I need a macro to compute an offset of an object relative to a base
address. The problem is the object can sometimes be a function pointer
with a base address of 0 but I don't know in advance that the object is
a function. Can it be done?
There is a flaw in your design or your reasoning. There can be no
object that is relative to a function.
#define BREF(b,p) ((size_t)((p) ? (char *)(p) - (char *)(b) : 0))

The following code demonstrates that strict c89 doesn't accept casting
a pointer to a function to a pointer to char which makes my above code
invalid.
No version of the C standard has ever defined a conversion between any
type of pointer to object and any type of pointer to function. They
are not convertible, just as you can't convert a banana to a
watermelon.

This is despite the fact that several operating system interfaces
(POSIX, Windows) define exactly that.
typedef int (*fn)(void);

void
test(fn f)
{
char *g = (char *)f;
}
int
main(void)
{
test(main);
return 0;
}

cc -std1 q.c
cc: Info: q.c, line 6: In the initializer for g, "f" of type "pointer
to function () returning void", is being converted to "pointer to char".
Such a cast is not permitted by the standard. (nonstandcast)
char *g = (char *)f;
--------------------------^


You have two choices. The first is to ignore or disable the warning,
which will work on some platforms but not all, for in some cases the
pointers might literally have different sizes. The second is to
reconsider your design and find a better way to implement it. If you
tell us the underlying problem you are trying to solve, we might be
able to help you with that.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #4

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

Similar topics

3
by: ken | last post by:
I am getting this error from a gcc compile and I was wondering whether this was 100% valid. This seems a little extreme to me the c++ cast operators appear to only work on objects which defeats...
23
by: James Aguilar | last post by:
Someone showed me something today that I didn't understand. This doesn't seem like it should be valid C++. Specifically, I don't understand how the commas are accepted after the function...
41
by: Alexei A. Frounze | last post by:
Seems like, to make sure that a pointer doesn't point to an object/function, NULL (or simply 0) is good enough for both kind of pointers, data pointers and function pointers as per 6.3.2.3: 3 An...
4
by: msolem | last post by:
I have some code where there are a set of functions that return pointers to each other. I'm having a bit of a hard time figuring out the correct type to use to do that. The code below works but...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
5
by: WittyGuy | last post by:
How to typecast a "function pointer" to "const void*" type in C++ way? int MyFunction (double money); // Function prototype const void* arg = (const void*)MyFunction; // type casting...
8
by: Gamma | last post by:
I'm trying to inherit subclass from System.Diagnostics.Process, but whenever I cast a "Process" object to it's subclass, I encounter an exception "System.InvalidCastException" ("Specified cast is...
20
by: MikeC | last post by:
Folks, I've been playing with C programs for 25 years (not professionally - self-taught), and although I've used function pointers before, I've never got my head around them enough to be able to...
7
by: ghulands | last post by:
I am having trouble implementing some function pointer stuff in c++ An object can register itself for many events void addEventListener(CFObject *target, CFEventHandler callback, uint8_t...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.