473,485 Members | 1,399 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

reinterpret cast problem.

Hi,

I'm trying to port a piece of code from Windows to Linux, the following
segment illustrates the coding problemI have: Under Linux, the
reinterpret_cast line
doesn't compile, the compiler says ISO C++ doesn allow a cast from
pointer-to-object
to pointer-to-function. However, I can use the plain-old-C-style cast and
the code compiles and works. I guess I have two questions:-

1. What is the sanctified way to write this type of code.
2. What is the rational behind the GNU compiler message ( presuming the
compiler is correctly interpreting the standard).

Thanks,

dave.

#include <stdio.h>

static int foo()
{
printf("hello\n");
return 0;
}

typedef int fnc_t();

int main(int argc, char* argv[])
{
void* fv = (fnc_t*)foo;

fnc_t* fnc = reinterpret_cast<fnc_t*>(fv); // illegal under GNU/linux

fnc();
return 0;
}
Jul 23 '05 #1
5 1901
"Dave Townsend" <da********@comcast.net> wrote in message
news:qN********************@comcast.com...
I'm trying to port a piece of code from Windows to Linux, the following
segment illustrates the coding problemI have: Under Linux, the
reinterpret_cast line
doesn't compile, the compiler says ISO C++ doesn allow a cast from
pointer-to-object
to pointer-to-function. However, I can use the plain-old-C-style cast and
the code compiles and works. I guess I have two questions:-

1. What is the sanctified way to write this type of code.
Just as 'void*' can be used to store the address of any object, 'void (*)()'
can store the address of any function pointer.
typedef void (*voidF)();
You can safely cast any function pointer to voidF and back.
2. What is the rational behind the GNU compiler message ( presuming the
compiler is correctly interpreting the standard).


Does the reinterpret_cast generate a warning or an error?
I'm not sure it should be an error, but it sure is a valid warning:
it is formally not portable to convert a function pointer to void*
and back (even though current platforms typically use a common
address space and register set for both data and code).
I hope this helps,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- e-mail contact form
Jul 23 '05 #2
Dave Townsend wrote:
Hi,

I'm trying to port a piece of code from Windows to Linux, the
following segment illustrates the coding problemI have: Under Linux,
the reinterpret_cast line
doesn't compile, the compiler says ISO C++ doesn allow a cast from
pointer-to-object
to pointer-to-function. However, I can use the plain-old-C-style
cast and the code compiles and works. I guess I have two questions:-
First, there are a number of compilers available for Windows and Linux. I gather
you're using GCC, but it wouldn't hurt to mention it. Also, the version is often
useful.
1. What is the sanctified way to write this type of code.
2. What is the rational behind the GNU compiler message ( presuming
the compiler is correctly interpreting the standard).

Thanks,

dave.

#include <stdio.h>

static int foo()
{
printf("hello\n");
return 0;
}

typedef int fnc_t();

int main(int argc, char* argv[])
{
void* fv = (fnc_t*)foo;

fnc_t* fnc = reinterpret_cast<fnc_t*>(fv); // illegal under
GNU/linux

fnc();
return 0;
}


GCC is correct. You can't reinterpret_cast between pointers to objects and
pointers to functions. If you need to be able to store either a function pointer
or an object pointer in a single variable, you can use a union:

union any_pointer
{
void* obj_ptr;
void (*func_ptr)();
};

Jonathan
Jul 23 '05 #3
Jonathan Turkanis wrote:
First, there are a number of compilers available for Windows and
Linux. I gather you're using GCC,
on Linux, I mean
but it wouldn't hurt to mention it.
Also, the version is often useful.

Jul 23 '05 #4
Dave Townsend wrote:
Hi,

I'm trying to port a piece of code from Windows to Linux, the following
segment illustrates the coding problemI have: Under Linux, the
reinterpret_cast line
doesn't compile, the compiler says ISO C++ doesn allow a cast from
pointer-to-object
to pointer-to-function.

It doesn't compile under Windows with gcc either:

C:\c>g++ temp.cpp -o temp.exe
temp.cpp: In function `int main(int, char**)':
temp.cpp:13: error: invalid conversion from `int (*)()' to `void*'
temp.cpp:15: error: ISO C++ forbids casting between pointer-to-function and
pointer-to-object
temp.cpp:21:2: warning: no newline at end of file

C:\c>
However, I can use the plain-old-C-style cast and
the code compiles and works. I guess I have two questions:-

1. What is the sanctified way to write this type of code.
2. What is the rational behind the GNU compiler message ( presuming the
compiler is correctly interpreting the standard).

Thanks,

dave.

#include <stdio.h>

static int foo()
{
printf("hello\n");
return 0;
}

typedef int fnc_t();

int main(int argc, char* argv[])
{
void* fv = (fnc_t*)foo;

fnc_t* fnc = reinterpret_cast<fnc_t*>(fv); // illegal under GNU/linux

fnc();
return 0;
}


Others have provided explanations for the rest. The above would be
better if it was:
#include <iostream>

namespace
{
int foo()
{
std::cout<<"hello\n";

return 0;
}
}

typedef int fnc_t();
int main()
{

fnc_t *fnc = foo;

fnc();
return 0;
}


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #5
On Tue, 15 Feb 2005 22:39:49 -0800, "Dave Townsend"
<da********@comcast.net> wrote in comp.lang.c++:
Hi,

I'm trying to port a piece of code from Windows to Linux, the following
segment illustrates the coding problemI have: Under Linux, the
reinterpret_cast line
doesn't compile, the compiler says ISO C++ doesn allow a cast from
pointer-to-object
to pointer-to-function. However, I can use the plain-old-C-style cast and
the code compiles and works. I guess I have two questions:-

1. What is the sanctified way to write this type of code.
There isn't any. There is absolutely no defined conversion between a
pointer to any type of object and a pointer to any type of function.
Not in either C or C++.

And in fact there are implementations where such conversions are
literally impossible, as pointers to functions are wider than pointers
to any type of object. Bits are truncated and there is no way to get
them back.
2. What is the rational behind the GNU compiler message ( presuming the
compiler is correctly interpreting the standard).
The rational is that in C++, as inherited from C, there are objects
and there are functions. They are apples and oranges, they do not
mix. In either language, conversion of a pointer to function to a
pointer to object, in either direction, with or without a cast, is
completely undefined.

The real problem is sloppy interfaces, both in Windows and *nix, where
they use a pointer to void to hold pointers to functions. This is not
and never has been defined behavior. A union of two pointers could
have been used with no runtime overhead in either execution time or
space on implementations where the two types are the same size, but
it's probably too late now.
Thanks,

dave.

#include <stdio.h>

static int foo()
{
printf("hello\n");
return 0;
}

typedef int fnc_t();

int main(int argc, char* argv[])
{
void* fv = (fnc_t*)foo;

fnc_t* fnc = reinterpret_cast<fnc_t*>(fv); // illegal under GNU/linux
Actually, it's illegal under any compiler. But since it's undefined
behavior, no diagnostic is required.
fnc();
return 0;
}


--
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
Jul 23 '05 #6

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

Similar topics

5
9708
by: A | last post by:
Hi, I have not been able to find an example for using the reinterpret cast. Any ideas? Regards, A
1
1683
by: Andreas Müller | last post by:
my problem is probably simple to fix but I don't now how, i have a thread function void *display(struct Camera *camera) and i try to create that thread via pthread_create(&draw_thread,...
28
2997
by: Tamir Khason | last post by:
Follwing the struct: public struct TpSomeMsgRep { public uint SomeId;
5
1616
by: cdg | last post by:
I am trying to write a section of code that uses a worker thread and would need a "post message" to post a "long" integer to the receiving function. However, I am not sure how to write the cast...
1
2033
by: Muhammad Haseeb | last post by:
plz help me how to use the reinterpret cast operator in filing in c++
0
1319
by: NiveditaB06 | last post by:
it is recommended not to use reinterpret cast that is mostly used for type converting one pointer type to other totally different type( like int ).After casting one member variable to another type...
5
2877
by: Jun | last post by:
Hello, I've code like : =========================================== class A{ public : // create print content friend std::ostream& operator<< (std::ostream& os, const A& a);
0
7090
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,...
0
7116
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
7161
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...
1
6825
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7275
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...
1
4857
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
4551
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
1376
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 ...
1
595
muto222
php
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.