static in structure 
Author Message
 static in structure

Why can we not  declare a static varible inside struct?
Is it due to the requirement of the contiguity for a struct variable?
paiyi


Sat, 10 Jan 2004 00:01:38 GMT  
 static in structure

Quote:

> Why can we not declare a static varible inside struct?

    What exactly would you want a static member of a struct to be? (i.e.
what would you want to be different from a regular member of a struct?)

--
Clark S. Cox III

http://www.whereismyhead.com/clark/



Sat, 10 Jan 2004 00:06:44 GMT  
 static in structure

Quote:

> Why can we not  declare a static varible inside struct?
> Is it due to the requirement of the contiguity for a struct variable?

The semantics of such a thing would have to be defined.  There is
no tradition for static variables inside a struct.  I can think
of several possible semantics, but no good reason for any of
them.
--
Available for short-term C and Linux contracts through September 2001.


Sat, 10 Jan 2004 00:06:05 GMT  
 static in structure

Quote:

> Why can we not  declare a static varible inside struct?
> Is it due to the requirement of the contiguity for a struct variable?

Consider: what would you do with an auto struct with a static member? An
entire static struct, yes, but how can one possibly have a disappearing
structure with one remaining member?

Richard



Sat, 10 Jan 2004 00:10:36 GMT  
 static in structure

Quote:


> > Why can we not  declare a static varible inside struct?
> > Is it due to the requirement of the contiguity for a struct variable?

> Consider: what would you do with an auto struct with a static member? An
> entire static struct, yes, but how can one possibly have a disappearing
> structure with one remaining member?

I saw some C++ code [off_topic] in which a static int is declared
inside a class (struct) in order to record how many objects are still
alive during executing.

paiyi



Sat, 10 Jan 2004 00:32:51 GMT  
 static in structure

Quote:


> > Why can we not  declare a static varible inside struct?
> > Is it due to the requirement of the contiguity for a struct variable?

> The semantics of such a thing would have to be defined.  There is
> no tradition for static variables inside a struct.  I can think
> of several possible semantics, but no good reason for any of
> them.

Not exactly a struct, but a C++ Class does share some things in common
with a structure and there are many possible uses for such things.

Essentially, if a class has static items declared within it, a single,
global, instance of that class is created and the same static items are
accessable and shared through every instance of the class.

--
== Eric Gorr ===== http://www.*-*-*.com/ :9293199 ===
"Therefore the considerations of the intelligent always include both
benefit and harm." - Sun Tzu
== Insults, like {*filter*}, are the last refuge of the incompetent... ===



Sat, 10 Jan 2004 00:35:19 GMT  
 static in structure

Quote:
> Why can we not  declare a static varible inside struct?

because C != C++


Sat, 10 Jan 2004 08:45:18 GMT  
 static in structure

Quote:



> > > Why can we not  declare a static varible inside struct?
> > > Is it due to the requirement of the contiguity for a struct variable?

> > Consider: what would you do with an auto struct with a static member? An
> > entire static struct, yes, but how can one possibly have a disappearing
> > structure with one remaining member?

> I saw some C++ code [off_topic] in which a static int is declared
> inside a class (struct) in order to record how many objects are still
> alive during executing.

As Daniel says, classes are not structs. You can get away with this in a
class because you can have con- and destructors. Consider what you would
do with a recursive function that defined both a static and an auto
struct, both of the same type containing a static int. Which "new"
struct would count as new when? The auto, one each for each function
call, and minus one at each return. The static: one at program start?
One at the outermost function call? It just doesn't work neatly.

Then consider a program in which _all_ such structs are auto. What do
you do when the last auto struct has ended its lifetime - remove the
static member as well, or leave it? If so, where?

Richard



Sat, 10 Jan 2004 22:44:53 GMT  
 static in structure

Quote:



> > > > Why can we not  declare a static varible inside struct?
> > > > Is it due to the requirement of the contiguity for a struct variable?
> As Daniel says, classes are not structs. You can get away with this in a
> class because you can have con- and destructors. Consider what you would
> do with a recursive function that defined both a static and an auto
> struct, both of the same type containing a static int. Which "new"
> struct would count as new when? The auto, one each for each function
> call, and minus one at each return. The static: one at program start?
> One at the outermost function call? It just doesn't work neatly.

I think the static struct should be counted one time at the first call to
the function.

The reason why a static variable isn't allowed in struct is due to C
requires the whole stucture elements are placed "together".
To withdraw a element value from a structure is counted by the offset of
the element from the beginning address of the structure.

struct STRUC{static int i;
             char A;
             char B;
             char D;        }; /*illegal*/

C is not designed to be able to take value of i from america and the
values of A,B,D from europe.

Quote:
> Then consider a program in which _all_ such structs are auto. What do
> you do when the last auto struct has ended its lifetime - remove the
> static member as well, or leave it? If so, where?

Leaving it untill the last moment just like a static variable in a
function.

paiyi



Sat, 10 Jan 2004 23:41:36 GMT  
 static in structure
In article

Quote:



>> > Why can we not  declare a static varible inside struct?
>> > Is it due to the requirement of the contiguity for a struct variable?

>> Consider: what would you do with an auto struct with a static member? An
>> entire static struct, yes, but how can one possibly have a disappearing
>> structure with one remaining member?

>I saw some C++ code [off_topic] in which a static int is declared
>inside a class (struct) in order to record how many objects are still
>alive during executing.

A C++ static member is not an actual component of the object instance;
it's a static variable with class scope.

The C language has no such thing as ``class scope''. In principle, a
static member could be accessed with the existing notation object->member
or object.member. But the problem is that there would be no way to
*define* storage for the object, without adding some truly new language
feature.

In C++ this is handled by defining the object using its fully qualified
name.

        class X {
                static int Y; // There exists a static member called Y
        };

        // .. elsewhere ..

        int X::Y; // Aha, the static member Y of class X lives here!

C has no class scope, or qualified identifiers, that would disambiguate
two static members having the same name, from different structs.



Sun, 11 Jan 2004 14:14:33 GMT  
 static in structure
Quote:
> Why can we not  declare a static varible inside struct?
It's not defined.
> Is it due to the requirement of the contiguity for a struct variable?

no.


Mon, 12 Jan 2004 01:30:46 GMT  
 static in structure

Quote:

> The C language has no such thing as ``class scope''.

How I wish it had! It could be extremely useful for things like enums.

--
Peter Pichler (Increment my "From:" address if your email bounces.)
This message is not solicitation for contact via email or phone.



Mon, 12 Jan 2004 06:21:35 GMT  
 static in structure


Quote:


>> > Why can we not  declare a static varible inside struct?
>> > Is it due to the requirement of the contiguity for a struct variable?

>> Consider: what would you do with an auto struct with a static member? An
>> entire static struct, yes, but how can one possibly have a disappearing
>> structure with one remaining member?

>I saw some C++ code [off_topic] in which a static int is declared
>inside a class (struct) in order to record how many objects are still
>alive during executing.

>paiyi

You can't have a static member within a struct, as many others have pointed
out.  However, to accomplish the purpose you cite here, wouldn't it suffice
to place a pointer to a static/global int within your struct?

--ben



Sat, 24 Jan 2004 06:09:42 GMT  
 
 [ 13 post ] 

 Relevant Pages 

1. C Beginners: Storage class static in structures.

2. Debugger shows incorrect global static variable structure

3. Providing limited access to static data structures?

4. static pointer to structure == NULL?

5. Structure offsets in static array ?

6. Static Structure

7. Can a structure element be static?

8. Initializing static structures inside of classes

9. F.Y.I. (static tunctions and structures)

10. Static and non-Static member - Signature

11. Why there is no static destructor corresponding to static constructor

12. Creating a static class from non static Framework classes

 

 
Powered by phpBB® Forum Software