static variable

P

Peter Kirk

Hi there

I have a question about static variables. There is a function which returns
a pointer to a structure "AStructure". The function has a static local
variable of type AStructure, and it is the address of this variable which is
returned. Is this good practice?

See this pseudo code:

AStructure *my_function()
{
static AStructure result;

// "result" is filled with data...

return ( &result );
}

Thanks,
Peter
 
J

jacob navia

Peter Kirk said:
Hi there

I have a question about static variables. There is a function which returns
a pointer to a structure "AStructure". The function has a static local
variable of type AStructure, and it is the address of this variable which is
returned. Is this good practice?

See this pseudo code:

AStructure *my_function()
{
static AStructure result;

// "result" is filled with data...

return ( &result );
}

Thanks,
Peter

It is 100% correct as far as I see.

Problems *could* arise in a
multi-threaded context because of simultaneous access to a single
memory location. If one thread enters this function during the time when
another thread is updating the data structure problems would arise.

This problem can be solved with a semaphore or a similar construct that
protects the access to the static variable and allows only one thread
to work on it at a time.

In a single-thread environment there are no problems
 
J

jota

returned. Is this good practice?

Yes why not!
If you can bare the fact that the function is not reentrant and the
static duration makes sizeof(AStructure) bytes allocated even if its never
used,
or only used for a short while!

For local scope use:
void my_function(AStructure *a)
{
a->blahblah=...
}

void caller()
{
AStructure a;
my_function(&a);
}

//jota
 
E

Emmanuel Delahaye

Peter Kirk said:
I have a question about static variables. There is a function which
returns a pointer to a structure "AStructure". The function has a static
local variable of type AStructure, and it is the address of this
variable which is returned. Is this good practice?

AStructure *my_function()
{
static AStructure result;

// "result" is filled with data...

return ( &result );
}

It is technically correct, but is it a good practice or not is debatable.

The presence of a static variable in the code makes this code non re-entrant.

Personally, I'm involved into preemptive multithreading telecom projects
where this practice is simply prohibited. Now, it's up to you.
 
H

Harti Brandt

On Fri, 16 Jul 2004, jacob navia wrote:

jn>
jn>"Peter Kirk" <peter> a écrit dans le message de
jn>jn>> Hi there
jn>>
jn>> I have a question about static variables. There is a function which
jn>returns
jn>> a pointer to a structure "AStructure". The function has a static local
jn>> variable of type AStructure, and it is the address of this variable which
jn>is
jn>> returned. Is this good practice?
jn>>
jn>> See this pseudo code:
jn>>
jn>> AStructure *my_function()
jn>> {
jn>> static AStructure result;
jn>>
jn>> // "result" is filled with data...
jn>>
jn>> return ( &result );
jn>> }
jn>>
jn>> Thanks,
jn>> Peter
jn>>
jn>
jn>It is 100% correct as far as I see.
jn>
jn>Problems *could* arise in a
jn>multi-threaded context because of simultaneous access to a single
jn>memory location. If one thread enters this function during the time when
jn>another thread is updating the data structure problems would arise.
jn>
jn>This problem can be solved with a semaphore or a similar construct that
jn>protects the access to the static variable and allows only one thread
jn>to work on it at a time.
jn>
jn>In a single-thread environment there are no problems

There may be. If the "filled with data" is different for different
calls to the function, the following will not do what you think it should:

foo(my_function(), my_function());

A typical instance of this problem are formatting functions for user
defined types that use an internal buffer:

const char *
fmt_complex(complex c)
{
static char buf[100];

sprintf(buf, "%g+%gI", creal(c), cimag(c));
return (buf);
}

then call

void
foo(complex a, complex b)
{
printf("a=%s b=%s\n", fmt_complex(a), fmt_complex(b));
}

harti
 
?

=?iso-8859-1?q?Nils_O=2E_Sel=E5sdal?=

It is 100% correct as far as I see.

Problems *could* arise in a
multi-threaded context because of simultaneous access to a single
memory location. If one thread enters this function during the time when
another thread is updating the data structure problems would arise.

This problem can be solved with a semaphore or a similar construct that
protects the access to the static variable and allows only one thread
to work on it at a time.

In a single-thread environment there are no problems
Not other than common "user" bugs, such as calling the function
twice and expect the pointer from the first call to be what
one expect. Just make sure users of the function know it isn't reentrant.
Just thought it was worth mentioning.
 
A

Alan Balmer

Hi there

I have a question about static variables. There is a function which returns
a pointer to a structure "AStructure". The function has a static local
variable of type AStructure, and it is the address of this variable which is
returned. Is this good practice?

See this pseudo code:

AStructure *my_function()
{
static AStructure result;

// "result" is filled with data...

return ( &result );
}

Thanks,
Peter

This sort of thing is fairly common, and usable in single-thread
environments, so long as it's well documented. Some standard library
functions do this. I hesitate to call it "good practice", since this
sort of thing is responsible for many programming errors.
 
K

Kenneth Brody

Peter said:
Hi there

I have a question about static variables. There is a function which returns
a pointer to a structure "AStructure". The function has a static local
variable of type AStructure, and it is the address of this variable which is
returned. Is this good practice?

See this pseudo code:

AStructure *my_function()
{
static AStructure result;

// "result" is filled with data...

return ( &result );
}

With the caveat that each call to my_function() overwrites the result
of the previous call, there is nothing "wrong" with the above code.
Because the local variable is static, it still exists upon return from
the function, and "&result" is still a valid pointer.
 
P

Peter

Thanks for all the answers - about what I expected, but I just wasn't sure.

I have been trying to maintain some old code, and ran across this "pattern"
many places. It is not documented, and I thought it could be related to some
problems we have been seeing - but apparently the original programmer was
careful in his calls of these functions, as they are never called again
before the previous call is finished processing the result.

Peter
 

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
474,145
Messages
2,570,825
Members
47,371
Latest member
Brkaa

Latest Threads

Top