473,395 Members | 1,629 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Function pointer question

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
6 1591
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
3
by: Bryan Parkoff | last post by:
Do C/C++ Compiler allow function to contain more than 8 parameters? I checked MS Visual C++ 6.0 that it can only limit 8 parameters, but most C/C++ Compiler can limit maximum 256 parameters. Can...
89
by: Sweety | last post by:
hi, Is main function address is 657. its show in all compiler. try it & say why? bye,
4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
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...
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'...
54
by: John | last post by:
Is the following program print the address of the function? void hello() { printf("hello\n"); } void main() { printf("hello function=%d\n", hello); }
12
by: Googy | last post by:
Hi!! Can any one explain me the meaning of following notations clearly : 1. typedef char(*(*frpapfrc()))(); frpapfrc f; 2. typedef int (*(arr2d_ptr)()); arr2d_ptr p; 3. typedef int...
5
by: pauldepstein | last post by:
Hi all, I saw some code like this: unsigned short SomeFunc(unsigned short SomeNum, bool SomeBool, const SomeClass& SomeMem, bool(SomeClass::*AmemberFunctionOfSomeClass)(const unsigned...
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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
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
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...

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.