local static data vs. global data

Discussion in 'C++' started by Leslaw Bieniasz, Oct 23, 2012.

  1. Hi,

    In my program I need to have several (global) functions that share some
    data, but I want to avoid introducing global data. I am therefore trying
    to use the following construct:


    inline const long double *const get_data_ptr(void)
    {
    static const long double data[10]
    {
    1.0L, 2.0L, 3.0L, etc.
    };

    return data;
    }



    void a(void)
    {
    static const long double *const data = get_data_ptr();

    // use the data, for example
    long double x = data[0] + data[1];

    return;
    }



    void b(void)
    {
    static const long double *const data = get_data_ptr();

    // use the data, for example
    long double y = data[0]*data[1];

    return;
    }


    This compiles without errors, and seems to work
    without errors. However, is this construct correct?

    If this plays any role, I am using
    Borland C++ Builder 6.0.

    Leslaw
    Leslaw Bieniasz, Oct 23, 2012
    #1
    1. Advertising

  2. Leslaw Bieniasz <> writes:

    > In my program I need to have several (global) functions that share
    > some data, but I want to avoid introducing global data. I am therefore
    > trying to use the following construct:
    >
    > inline const long double *const get_data_ptr(void)
    > {
    > static const long double data[10]
    > {
    > 1.0L, 2.0L, 3.0L, etc.
    > };
    > return data;
    > }
    >
    > void a(void)
    > {
    > static const long double *const data = get_data_ptr();
    > // use the data, for example
    > long double x = data[0] + data[1];
    > return;
    > }

    [...]

    Why not wrap all that stuff in a class?

    -- Alain.
    Alain Ketterlin, Oct 23, 2012
    #2
    1. Advertising

  3. On 23.10.2012 10:36, Leslaw Bieniasz wrote:
    > In my program I need to have several (global) functions that share some
    > data, but I want to avoid introducing global data.


    Either your design with global shared data is OK, then there is nothing
    wrong with global data, or the design is bad. Then you should change
    your design.

    > I am therefore trying
    > to use the following construct:
    >
    > inline const long double *const get_data_ptr(void)
    > {
    > static const long double data[10]
    > {
    > 1.0L, 2.0L, 3.0L, etc.
    > };
    >
    > return data;
    > }


    What are you trying to accomplish here?
    Except for the overhead, I mean?

    By the way, on many platforms this code has a race condition in
    multi-threaded context because the initialization of local statics is
    not implicitly thread-safe.


    > This compiles without errors, and seems to work
    > without errors. However, is this construct correct?


    It depends on what you mean with correct. Yes, it is valid C++ code.
    No, I would not do so.

    If you are really talking only about constants, then feel free to define
    a global constant.

    If you do not want to expose this symbol in the global name space, then
    place it in a separate namespace. Alternatively you can use a class,
    that contains all the functions, that depend on your constants.


    Marcel
    Marcel Müller, Oct 23, 2012
    #3
  4. Leslaw Bieniasz

    Werner Guest

    On Tuesday, October 23, 2012 10:36:18 AM UTC+2, Leslaw Bieniasz wrote:
    > Hi,
    >
    >
    >
    > In my program I need to have several (global) functions that share some
    >
    > data, but I want to avoid introducing global data. I am therefore trying
    >
    > to use the following construct:
    >
    >
    >
    >
    >
    > inline const long double *const get_data_ptr(void)
    >
    > {
    >
    > static const long double data[10]
    >
    > {
    >
    > 1.0L, 2.0L, 3.0L, etc.
    >
    > };
    >
    >
    >
    > return data;
    >
    > }
    >
    >
    >
    >
    >
    >
    >
    > void a(void)
    >
    > {
    >
    > static const long double *const data = get_data_ptr();
    >
    >
    >
    > // use the data, for example
    >
    > long double x = data[0] + data[1];
    >
    >
    >
    > return;
    >
    > }
    >
    >
    >
    >
    >
    >
    >
    > void b(void)
    >
    > {
    >
    > static const long double *const data = get_data_ptr();
    >
    >
    >
    > // use the data, for example
    >
    > long double y = data[0]*data[1];
    >
    >
    >
    > return;
    >
    > }
    >
    >
    >
    >
    >
    > This compiles without errors, and seems to work
    >
    > without errors. However, is this construct correct?


    Valid code, but there is hardly any gain (if any at all -
    the initializer being inline) in having the two additional
    static members. IMO you can call the initializing function
    directly.

    Regards,

    Werner
    Werner, Oct 24, 2012
    #4
    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. Patrick Hoffmann
    Replies:
    3
    Views:
    2,812
    Christian Janßen
    Aug 8, 2003
  2. Martin Wells

    Static inline functions with static local variables

    Martin Wells, Oct 6, 2007, in forum: C Programming
    Replies:
    10
    Views:
    692
    Army1987
    Oct 8, 2007
  3. Mark
    Replies:
    2
    Views:
    407
    Jim Langston
    Oct 16, 2007
  4. , India
    Replies:
    1
    Views:
    314
    , India
    Apr 21, 2008
  5. Suraj

    Local vs Global Static

    Suraj, Apr 4, 2009, in forum: C++
    Replies:
    7
    Views:
    435
    Jerry Coffin
    Apr 8, 2009
Loading...

Share This Page