local static data vs. global data

L

Leslaw Bieniasz

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
 
A

Alain Ketterlin

Leslaw Bieniasz said:
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.
 
M

Marcel Müller

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
 
W

Werner

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top