• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Proper way to cast function pointers.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Proper way to cast function pointers.


  • Subject: Proper way to cast function pointers.
  • From: Steve Checkoway <email@hidden>
  • Date: Thu, 12 Oct 2006 01:55:10 -0700

I'm not positive that this is the best list for this since it's more of a c++ issue but it is related to apple's g++ and so forth. If there's a more appropriate list (such as darwin-dev), I'll go there.

I'm looking for the proper way to cast a function pointer in c++. In particular an OpenGL entry point. Writing code similar to <http:// developer.apple.com/qa/qa2001/qa1188.html> (which uses deprecated functions), I have something like the following:

#include <mach-o/dyld.h>
#include <cstdlib>
#include <cstdio>
#include <inttypes.h>
#include <string>
using namespace std;

static void *NSGLGetProcAddress( const string &name )
{
NSSymbol symbol = NULL;
string symbolName( '_' + name );
const uint32_t count = _dyld_image_count();
const uint32_t options = NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR;


for( uint32_t i = 0; i < count && !symbol; ++i )
symbol = NSLookupSymbolInImage ( _dyld_get_image_header(i), symbolName.c_str(), options );
return symbol ? NSAddressOfSymbol( symbol ) : NULL;
}
typedef void (*glFunc)();
int main()
{
void *addr = NSGLGetProcAddress( "glBlendEquation" );
glFunc glBlendEquation = reinterpret_cast<glFunc>( addr );
return !glBlendEquation;
}


The glFunc typedef is supposed to be just any generic function (is there a better way to do this such as void (*)(...) perhaps?). The problem is that NSAddressOfSymbol returns a void * which is not a function pointer. When I compile, I get:

$ g++ e.cc -Wall -pedantic -framework OpenGL
e.cc: In function ‘int main()’:
e.cc:26: warning: ISO C++ forbids casting between pointer-to-function and pointer-to-object


This makes sense given that a function pointer need not even be the same size as an object pointer. Is there a correct way to do this cast?

--
Steve Checkoway



Attachment: smime.p7s
Description: S/MIME cryptographic signature

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Proper way to cast function pointers.
      • From: Jesper Papmehl <email@hidden>
    • Re: Proper way to cast function pointers.
      • From: Scott Ribe <email@hidden>
  • Prev by Date: Using HFS paths in the standard C libraries
  • Next by Date: Re: Proper way to cast function pointers.
  • Previous by thread: Re: Using HFS paths in the standard C libraries
  • Next by thread: Re: Proper way to cast function pointers.
  • Index(es):
    • Date
    • Thread