By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,134 Members | 1,942 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,134 IT Pros & Developers. It's quick & easy.

Function pointer question

P: n/a
Can anything bad happen by using a function pointer to a static
function in a different module?

Below is an example:

//FILE: main.cpp
#include <iostream>

typedef int(*func_ptr)(void);
func_ptr func();

int main(int argc, char *argv[])
{
func_ptr x=func();

int y=x();

std::cout << "y=" << y << std::endl;

return 0;
}
// FILE: func.cpp
static int func2(void);

typedef int(*func_ptr)(void);

func_ptr func()
{
return &func2;
}

static int func2(void)
{
// return some random int from mem;
int a;
int b;

return (a+b)*723;
}

Jun 7 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Adrian wrote:
Can anything bad happen by using a function pointer to a static
function in a different module?
In theory, you cannot access a static function from another translation
unit (i.e. a different module).

Fei
Jun 7 '07 #2

P: n/a
On 2007-06-07 08:55:53 -0700, Fei Liu <fe****@aepnetworks.comsaid:
Adrian wrote:
>Can anything bad happen by using a function pointer to a static
function in a different module?

In theory, you cannot access a static function from another translation
unit (i.e. a different module).
Not by name, but there is nothing wrong with using a function pointer
to access it.

--
Clark S. Cox III
cl*******@gmail.com

Jun 7 '07 #3

P: n/a
On Jun 8, 3:30 am, Adrian <n...@bluedreamer.comwrote:
Can anything bad happen by using a function pointer to a static
function in a different module?
Your code is correct, altho it is better to
include the declration of func() in a header
file, because if you make a typo or a change
in one of the C++ files and not the other then
you will have undefined behaviour.

Also, some implementations (typically, crappy
embedded ones) fail to implement function
pointers properly so beware of that in advance.

Jun 7 '07 #4

P: n/a
Old Wolf wrote:
Also, some implementations (typically, crappy
embedded ones) fail to implement function
pointers properly so beware of that in advance.
I find that hard to believe. Function pointers have been around since
the K&R days.
Jun 7 '07 #5

P: n/a
Old Wolf wrote:
>
Also, some implementations (typically, crappy
embedded ones) fail to implement function
pointers properly so beware of that in advance.
Care to name and shame? I've never seen this in practice.

--
Ian Collins.
Jun 7 '07 #6

P: n/a
On Jun 8, 11:33 am, red floyd <no.s...@here.dudewrote:
Old Wolf wrote:
Also, some implementations (typically, crappy
embedded ones) fail to implement function
pointers properly so beware of that in advance.

I find that hard to believe. Function pointers have been around since
the K&R days.
Well, I've used several such systems, so believe
what you will. Typically what happens is that the
code for the function is on a code page that isn't
swapped in at the time (so it jumps to unrelated
code at the same location on the current page),
or that the system traps when a shared library
invokes a function pointer that points to a function
in the calling application.

Jun 8 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.