Optimizing function pointer usage

Discussion in 'C++' started by Jack, Mar 7, 2006.

  1. Jack

    Jack Guest

    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
    Jack, Mar 7, 2006
    #1
    1. Advertising

  2. Jack

    mlimber Guest

    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
    mlimber, Mar 7, 2006
    #2
    1. Advertising

  3. Jack

    Jack Guest

    On 7 Mar 2006 07:00:32 -0800, "mlimber" <> 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
    Jack, Mar 8, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. glen stark
    Replies:
    2
    Views:
    684
    Ron Natalie
    Oct 10, 2003
  2. Fraser Ross
    Replies:
    4
    Views:
    1,012
    Fraser Ross
    Aug 14, 2004
  3. murgan
    Replies:
    6
    Views:
    4,826
    Thad Smith
    Dec 21, 2005
  4. Vijai Kalyan
    Replies:
    4
    Views:
    683
    Vijai Kalyan
    Nov 8, 2005
  5. Replies:
    6
    Views:
    223
Loading...

Share This Page