472,807 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Optimizing function pointer usage

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
Mar 7 '06 #1
2 2481
Jack wrote:
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.
Unless you're working in an environment where memory usage is very
tight, I'd say you're prematurely optimizing. On a typical system, a
pointer is 4 bytes, and say 3 dozen pointers would consume a whopping
144 bytes. You could probably use some sort of dynamically allocated
array of function pointers (or functors in C++), but I'm guessing that
the code/data overhead required to do so would itself exceed 144 bytes.

If memory really is your tightest constraint, then keep working on this
problem (now or later). Otherwise, worry about correctness and
maintainability and come back to memory efficiency later. It's much
easier to make a correct program more efficient than to make an
"efficient" program correct.
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.
The space for such objects is allocated at program startup for the
duration of the program, but the objects are not initialized until the
function runs.
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?
There are two options: use automatic variables rather than
static/globals or use dynamic memory allocation (but see the problem
with the latter approach above).
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?


The compiler doesn't distinguish between .c* files and header files.
The preprocessor literally inserts the text from your header files into
the .c* files to make a "translation unit" that is passed to the
compiler.

Putting a static variable/function in a header may mean that it is
duplicated in each translation unit in which it is used, thus taking up
more space. BTW, how much memory space does the code for one occurrence
of foo() take up? Again, I suspect you're likely worry about
optimizations prematurely.

A compiler will compile everything in the translation unit except C++
templates that are not instantiated. <OT>If you are building a
statically linked executable, a linker will strip out the unused
variables and code.</OT>

Cheers! --M

Mar 7 '06 #2
On 7 Mar 2006 07:00:32 -0800, "mlimber" <ml*****@gmail.com> wrote:
Unless you're working in an environment where memory usage is very
tight, I'd say you're prematurely optimizing. On a typical system, a
pointer is 4 bytes, and say 3 dozen pointers would consume a whopping
144 bytes.
I said a few dozen. I should have said several dozen. I just counted
and it's 261. Still, I see your point...1044 bytes, not a big deal.
But it's the principle, I don't want to waste any memory at all. I
guess it's not that big of a deal. Just thought I'd ask.
A compiler will compile everything in the translation unit except C++
templates that are not instantiated. <OT>If you are building a
statically linked executable, a linker will strip out the unused
variables and code.</OT>

Cheers! --M


Thanks for clarifying that for me. I should have know that. It's the
linker, not the compiler, that strips out unused code for my
executable. I'm gonna keep the function pointers global and have them
all initialized in one function. This way, I have the added feature of
the code being thread safe.

Thanks,
Jack
Mar 8 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: Thomas Matthews | last post by:
Hi, My son is writing a program to move a character. He is using the numbers on the keypad to indicate the direction of movement: 7 8 9 4 5 6 1 2 3 Each number has a direction except...
5
by: amit kumar | last post by:
I am calling a function which returns pointer to a map. The declaration of the map is map<int,vectxyz*>. vectxyz is a vector containing pointer to a class xyz. For map<int,vectxyz*>* p1 In the...
15
by: Albert | last post by:
Hi, I need to pass a pointer-to-member-function as a parameter to a function which takes pointer-to-function as an argument. Is there any way to do it besides overloading the function? Here...
9
by: Pushker Pradhan | last post by:
I've a function which accepts void * as the arg because the actual datatype can be char or float or anything. The fuction: void convolve(void *x, float *convx, uint32 numrowsx, uint32 numcolsx,...
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...
18
by: ramu | last post by:
Can anyone tell me the advantages of using function pointers? Thanks in advance.
9
by: CryptiqueGuy | last post by:
Consider the variadic function with the following prototype: int foo(int num,...); Here 'num' specifies the number of arguments, and assume that all the arguments that should be passed to this...
7
by: WaterWalk | last post by:
Hello. I thought I understood member function pointers, but in fact I don't. Consider the following example: class Base { public: virtual ~Base() {} }; class Derived : public Base {
2
by: Andrea Taverna | last post by:
Hello everyone, I wrote a bunch of recursive functions to operate on multi-dimensional matrices. The matrices are allocated dynamically in a non-contiguous way, i.e. as an array of pointers...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.