Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > local static data vs. global data

Reply
Thread Tools

local static data vs. global data

 
 
Leslaw Bieniasz
Guest
Posts: n/a
 
      10-23-2012


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
 
Reply With Quote
 
 
 
 
Alain Ketterlin
Guest
Posts: n/a
 
      10-23-2012
Leslaw Bieniasz <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Marcel Müller
Guest
Posts: n/a
 
      10-23-2012
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
 
Reply With Quote
 
Werner
Guest
Posts: n/a
 
      10-24-2012
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Local vs Global Static Suraj C++ 7 04-08-2009 04:52 PM
does value-initialization happen for global and local static object ? subramanian100in@yahoo.com, India C++ 1 04-21-2008 03:17 AM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 1 10-25-2006 06:50 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 0 10-25-2006 01:04 PM
a static local variable in a static method is thread local storage? Patrick Hoffmann C++ 3 08-08-2003 02:37 PM



Advertisments