Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using array of function pointers

0 views
Skip to first unread message

krishna

unread,
Nov 12, 2009, 3:36:52 AM11/12/09
to
int main(int argc, char **argv)
{
std::string (* fp[]) (int) = {binary};
std::cout << fp[0] << std::endl << fp[1];
}

This compiles (in visual studio 2008 express) but fails with run-time
error. Why?

Thanks,
Krishna.

P.S. This is not a homework question.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Chris Morley

unread,
Nov 12, 2009, 1:36:10 PM11/12/09
to
> int main(int argc, char **argv)
> {
> std::string (* fp[]) (int) = {binary};
> std::cout << fp[0] << std::endl << fp[1];
> }
>
> This compiles (in visual studio 2008 express) but fails with run-time
> error. Why?

std::cout << fp[0] << std::endl << fp[1];

The compiler doesn't do compile time bounds checks for you when you access
an array. fp[1] is out of bounds, fp[10000] would be too. It may have
various run time checks though depending on the compiler and
switches/options used for debug builds.

Note, you are displaying the pointers here, not trying to call the
function...

Complete (slightly) less trivial example...

#include <iostream>
#include <string>

std::string binary(int i)
{
char t[256];
sprintf(t,"b1 i=%d",i);
return std::string(t);
}

std::string binary2(int i)
{
char t[256];
sprintf(t,"b2 i=%d",i);
return std::string(t);
}

int main(int argc, char **argv)
{

std::string (* fp[]) (int) = {binary,binary2};
std::cout << (void*)fp[0] << std::endl << (void*)fp[1] << std::endl;
// printf("%p, %p\n", fp[0], fp[1] ); // IMO less messy than cout

for (int i=0;i<2;i++) std::cout << fp[i](100*i) << std::endl;
for (int i=0;i<2;i++) std::cout << (*fp[i])(100*i) << std::endl; // or...
with more typing
}

Outputs:
0x401397
0x4012fc
b1 i=0
b2 i=100
b1 i=0
b2 i=100

Chris

P.S. Someone who knows, why does the "cout << fp[0]" print 1 (bool?) and not
the function pointer? (GCC 4.3.2 if it matters...)

Johannes Schaub (litb)

unread,
Nov 13, 2009, 1:57:45 AM11/13/09
to
Chris Morley wrote:
>
> P.S. Someone who knows, why does the "cout << fp[0]" print 1 (bool?) and
> not the function pointer? (GCC 4.3.2 if it matters...)

Function pointers don't convert to void const*, but they convert to bool. So
the bool version of "operator<<" is called. Try to put "<< boolalpha <<" and
see true/false as output.

CornedBee

unread,
Nov 13, 2009, 3:40:22 PM11/13/09
to
On Nov 12, 7:36 pm, "Chris Morley" <chris.mor...@lineone.net> wrote:
> std::cout << (void*)fp[0] << std::endl << (void*)fp[1] << std::endl;

This is not strictly conforming. You can't cast between pointers to
objects and pointers to functions in C++03. You should cast to a
pointer-sized integer and display it in hex mode.

In C++0x, this will be conditionally supported, meaning that compilers
must either support it with the semantics we expect or reject it.

0 new messages