J
Jack
I have a chunk of code that loads a few dozen function pointers into
global variables. I'm concerned with unused memory consumption. What
if the client only needs to use one or two functions? Then there's
quite a few function pointers consuming memory and going to waste.
Here's little example:
// mycode.cpp or mycode.c
typedef int (*PFN) ();
PFN g_pfn;
// Assuming initialization of g_pfn has occurred.
int foo()
{
if (g_pfn)
{
return g_pfn();
}
return 0;
}
I've thought about localizing the function pointers and making them
static as such:
int foo()
{
static g_pfn = NULL;
if ( !g_pfn )
{
g_pfn = (PFN)GetFunctionPointer();
}
if ( g_pfn )
{
return g_pfn();
}
return 0;
}
but I've read that static variables even though declared locally are
initialized at program startup, and therefore are still possibly
wasting memory.
Is there a way I could delay declaring and loading these function
pointers until the client needs them, and have that initialization
occur only once? In other words, is there a way to keep g_pfn from
taking up memory until it is needed?
How about this: What if I define the foo() function in a header file,
make it a static function, and then declare the function pointers as
local static variables? Will putting it in a header file make any
difference regarding my problem? Will the compiler omit the unused
portions of the code and not compile them? (Assuming a decent, run of
the mill compiler). Are static variables in unused static functions in
header files initialized a program startup?
Any help on this topic would be greatly appreciated.
Thanks,
Jack
global variables. I'm concerned with unused memory consumption. What
if the client only needs to use one or two functions? Then there's
quite a few function pointers consuming memory and going to waste.
Here's little example:
// mycode.cpp or mycode.c
typedef int (*PFN) ();
PFN g_pfn;
// Assuming initialization of g_pfn has occurred.
int foo()
{
if (g_pfn)
{
return g_pfn();
}
return 0;
}
I've thought about localizing the function pointers and making them
static as such:
int foo()
{
static g_pfn = NULL;
if ( !g_pfn )
{
g_pfn = (PFN)GetFunctionPointer();
}
if ( g_pfn )
{
return g_pfn();
}
return 0;
}
but I've read that static variables even though declared locally are
initialized at program startup, and therefore are still possibly
wasting memory.
Is there a way I could delay declaring and loading these function
pointers until the client needs them, and have that initialization
occur only once? In other words, is there a way to keep g_pfn from
taking up memory until it is needed?
How about this: What if I define the foo() function in a header file,
make it a static function, and then declare the function pointers as
local static variables? Will putting it in a header file make any
difference regarding my problem? Will the compiler omit the unused
portions of the code and not compile them? (Assuming a decent, run of
the mill compiler). Are static variables in unused static functions in
header files initialized a program startup?
Any help on this topic would be greatly appreciated.
Thanks,
Jack