static (what? not electricity)

Sgqvur

FullOfUltimateTruthsAndEt ernalPrinciples, i.e shi
Reaction score
62

----I was "thinking" about the word static and it's usage and meaning in
the programming languages. I am not sure if the C language was the one
to introduce it but it's usage is strange for me at least. I know a
little C but I think I know what static does. Prepended to a global var-
iable or function make it only visible and usable to the file/compilation
unit it is declared/defined. Prepending it to a local varaible makes the
variable sort of anonymous global so to speak making it only visible and
usable to the function. So it seams static is a visibilty modifier for
the variable/function that follows it. So how logical is static for you?
For me more logical names for a visibilty modifier are hidden (I think
you can agree that hidden is describles presisly what is meant by static.
In the global case it hides the variable/function from the rest of the
compilation units. In the local case it hides the variable from the rest
of the code in the unit and of course it's value is saved not lost like
the rest of the local variables.), my (as in you can't have my global int
=) and this is my var/function there are many like it but this one's mine)
visibleonlyhere is rather verbose and private of course makes sense as
well. I was "thinking" about something starting with un... but only came up
with unavailable =), I would pick static too if only unavailable was
available. But anyway... enough about it's meaning and usage in C. The
reassion it was picked up by the creators of the language is I guess
borrowed from the Science of Physics in which static has the meaning of
"unable to change", "fixed", "Something that is not part of any perceived
universe phenomena; having no motion; no particle; no wavelength". Hm
I think I am starting "to getting it", so static var/function means
unchangeable for the rest of the code but not here? So change[able]hereonly
is another logical name?
----Moving to the OOP langguages from the C family C++, Java, C#, D etc.
But it seams in general that in Computer Science that the word static
has a lot of meanings (not just double, triple, quadriple meaning but
also sextuple and more... In those languages the meaning of static is
even more foggy. Prepended to a variable it makes only one copy of it
for every instance of the class to access (sound like shared to me) and
prepended to a method it stands for litteraly a function which name
when used outside of the class begings with the class's name follow by a
dot and the name which the user picked (I think there are some additinal
rules like only able to access other static data and what not). So the
meaning of static (something unchangeable, fixed) that was hard to find
in the usage of C is lost entirely here. So why did the smart people
behind those languages rather carelessly copied it over? So in my opinion
shared (intead of static) and simple function (instead of static method)
is simpler and easier to grasp.
 

The Helper

Necromancy Power over 9000
Staff member
Reaction score
1,697
Static and Global are about scope. It is true that visibility is a factor there but they were created more inline with memory allocation. It takes less memory to declare a variable static than global. Also, if you have something declared global and it conflicts with another variable name it can cause memory leaks because the space cannot be de-allocated. This is is especially troublesome in loops where you try and create and destroy a variable that is local (static) that conflicts with a global. This is not a problem with all languages though.

It is all about memory allocation. When you have 4 gigs of memory on a windows box, not a big deal and sloppy coding is not noticeable but on mobile apps or limited memory situations you want to be able to control your memory usage down to the smallest level. That is why that is there.
 

s3rius

Linux is only free if your time is worthless.
Reaction score
130
Different languages often use the same terminology for different things.
For example, a class in Haskell is very different from the C++ class, but instead is more like an interface from Java. While the Java interface is implemented using abstract classes in C++.

From MSDN:
When modifying a variable, the static keyword specifies that the variable has static duration (it is allocated when the program begins and deallocated when the program ends) and initializes it to 0 unless another value is specified. When modifying a variable or function at file scope, the static keyword specifies that the variable or function has internal linkage (its name is not visible from outside the file in which it is declared).

A variable declared static in a function retains its state between calls to that function.

When modifying a data member in a class declaration, the static keyword specifies that one copy of the member is shared by all instances of the class. When modifying a member function in a class declaration, the static keyword specifies that the function accesses only static members.
 

Slapshot136

Divide et impera
Reaction score
471
Any chance you can explain why this is, or have a link that does?

I think it something like for things like string, where they don't always use the full amount of memory allocated, it can be optimized to use only how much is needed for statics, but not for dynamic variables, as they might end up using all of their allocated space later, while a static wouldn't - im not sure about this, as I think this would cause problems when you tried to copy a static into a non-static/etc. - I would also like a better explanation
 

s3rius

Linux is only free if your time is worthless.
Reaction score
130
I think it something like for things like string, where they don't always use the full amount of memory allocated, it can be optimized to use only how much is needed for statics, but not for dynamic variables, as they might end up using all of their allocated space later, while a static wouldn't - im not sure about this, as I think this would cause problems when you tried to copy a static into a non-static/etc. - I would also like a better explanation

There are three types of memory storage:
Code:
static int s = 5; //Static
int i = 5; //Stack
int *c = new int[i]; //Dynamic - we're making an i-sized array here! i is not constant!

Variables that are stack or static have a known size during compilation. You know exactly that an int is X bytes on your system:

Code:
<allocate 4 bytes of memory for i>
set i = 5
<later deallocate the 4 bytes of memory that i uses>


Dynamic memory, however, can be an arbitrary size. (In the above example we're declaring an array with i elements. Since i is a number unknown to the compiler it doesn't know how large that array actually is). Since the compiler doesn't know how large the memory chunk is it can't automatically clean it up:

Code:
<allocate some user-defined number of bytes of memory for c>
set c[3] = 5
<later deallocate the ??? bytes of memory that c uses>

So, when dynamically allocating, the system will always store a number in the memory to remember how large the particular allocated chunk is.

Look at my (awesomely-drawn) picture.
The first one is how the memory looks like for stack/static variables. The system stores a pointer to the memory-portion, and because it knows exactly what type the data is of, it knows that the next 4 bytes (starting from that memory pointer) are the variable we want.
No extra memory is used.

The second one is how dynamic memory looks like. The compiler can't be sure how large the allocated memory block is. That's why it uses a small piece of memory in-front of the pointer to remember how large it was.
Thus we need an extra few bytes. Usually it's 4 bytes, I think.

Soo... dynamic memory requires some management-overhead.
However, static memory is static! That means it gets allocated when the program starts and deallocated when it ends. It basically stays in your memory as long as your program is active. While dynamic memory can be deallocated when you don't need it anymore.

The difference is memory usage is also minimal. Those few bytes really aren't important. Even on mobile devices it's not that important. (On mobile devices you try not to allocate so much because it takes relatively much time to allocate/deallocate.)

Unbenannt.png
 

camelCase

The Case of the Mysterious Camel.
Reaction score
362
I don't get it =/
It takes less memory to declare a variable static than global.

Don't both static and global variables have the same life-span?
The only difference being their scope.

And, unless your global variable uses dynamic storage, I don't see how a static uses less memory =/
 

Slapshot136

Divide et impera
Reaction score
471
Don't both static and global variables have the same life-span?
The only difference being their scope.

I think you can delete static variables once they are (or will be) out of scope, while globals you can't (since they will never be out of scope)
 

camelCase

The Case of the Mysterious Camel.
Reaction score
362
statics last till the end of the program ._.
Their scope is within w/e class or function they are declared in.
 

GetTriggerUnit-

DogEntrepreneur
Reaction score
129
I think what they're trying to say is that when a scope finishes its execution, the garbage collection will remove all the memory it used. static variables will be cleaned too.
 

s3rius

Linux is only free if your time is worthless.
Reaction score
130
I think what they're trying to say is that when a scope finishes its execution, the garbage collection will remove all the memory it used. static variables will be cleaned too.

No, that's exactly what does not happen. Static variables stay, even if the program leaves it's scope.
 

s3rius

Linux is only free if your time is worthless.
Reaction score
130
I don't know of any reason why a static variable would require less juice than a global.
I've explained why dynamically allocated variables require more above, but the difference between a normal global and a static, in my eyes, is only scope visibility.
 

Jedi

New Member
Reaction score
63
I don't know of any reason why a static variable would require less juice than a global.
I've explained why dynamically allocated variables require more above, but the difference between a normal global and a static, in my eyes, is only scope visibility.

I don't even understand the part you explained.

Code:
char p* =(char *) malloc(5); //allocated 5 bytes
here
code
comes
...
free(p) //deallocated 5 bytes
^Isn't there 5 bytes allocated and deallocated?
 

camelCase

The Case of the Mysterious Camel.
Reaction score
362
I reckon he means that for dynamically allocated memory, an extra byte or a few is needed to 'remember' how much memory was allocated.
 

Lyerae

I keep popping up on this site from time to time.
Reaction score
105
<snip'd>
^Isn't there 5 bytes allocated and deallocated?

If I understand correctly, yes (though I believe C++ just uses new and delete). I'm not too familiar with the finer details of memory allocation yet (but I do this that should technically be something like "char p = (char *) malloc(sizeof(char));").
 

s3rius

Linux is only free if your time is worthless.
Reaction score
130
I don't even understand the part you explained.


Code:
void blah(){

    int i = GetUserInput();

   char* array =malloc( i*( sizeof(char) ) );

   //blah blah


   free(array);
}

So, how many bytes does the system have to deallocate when it reaches free()?
You can't know beforehand, so the system needs to store a copy of the size you passed to malloc() to remember how large the allocation was.
And it stores this number next to the actual allocated memory block.

When I'm at it - malloc isn't guaranteed to give you X bytes of memory, even if you requested X bytes. It will give you at least X bytes, instead.
Some operating systems like to give away memory in 4-byte or 8-byte chunks. So if you request 14 bytes you might very well get 16.
 

Dan

The New Helper.Net gives me great Anxiety... o.O;;
Reaction score
159
It's the opposite of dynamic. Not reading your post as it's too hipster.

lol. I'm surprised it took this long for someone to mention this. I'm actually half sure that this post is a troll post. (the good kind)

"Static" means "unchanging" , "fixed", or "lacking movement." Static functions are never put on the stack, thus are immobile. static variables exist in one instance, and thus do not change.
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • Varine Varine:
    How can you tell the difference between real traffic and indexing or AI generation bots?
  • The Helper The Helper:
    The bots will show up as users online in the forum software but they do not show up in my stats tracking. I am sure there are bots in the stats but the way alot of the bots treat the site do not show up on the stats
  • Varine Varine:
    I want to build a filtration system for my 3d printer, and that shit is so much more complicated than I thought it would be
  • Varine Varine:
    Apparently ABS emits styrene particulates which can be like .2 micrometers, which idk if the VOC detectors I have can even catch that
  • Varine Varine:
    Anyway I need to get some of those sensors and two air pressure sensors installed before an after the filters, which I need to figure out how to calculate the necessary pressure for and I have yet to find anything that tells me how to actually do that, just the cfm ratings
  • Varine Varine:
    And then I have to set up an arduino board to read those sensors, which I also don't know very much about but I have a whole bunch of crash course things for that
  • Varine Varine:
    These sensors are also a lot more than I thought they would be. Like 5 to 10 each, idk why but I assumed they would be like 2 dollars
  • Varine Varine:
    Another issue I'm learning is that a lot of the air quality sensors don't work at very high ambient temperatures. I'm planning on heating this enclosure to like 60C or so, and that's the upper limit of their functionality
  • Varine Varine:
    Although I don't know if I need to actually actively heat it or just let the plate and hotend bring the ambient temp to whatever it will, but even then I need to figure out an exfiltration for hot air. I think I kind of know what to do but it's still fucking confusing
  • The Helper The Helper:
    Maybe you could find some of that information from AC tech - like how they detect freon and such
  • Varine Varine:
    That's mostly what I've been looking at
  • Varine Varine:
    I don't think I'm dealing with quite the same pressures though, at the very least its a significantly smaller system. For the time being I'm just going to put together a quick scrubby box though and hope it works good enough to not make my house toxic
  • Varine Varine:
    I mean I don't use this enough to pose any significant danger I don't think, but I would still rather not be throwing styrene all over the air
  • The Helper The Helper:
    New dessert added to recipes Southern Pecan Praline Cake https://www.thehelper.net/threads/recipe-southern-pecan-praline-cake.193555/
  • The Helper The Helper:
    Another bot invasion 493 members online most of them bots that do not show up on stats
  • Varine Varine:
    I'm looking at a solid 378 guests, but 3 members. Of which two are me and VSNES. The third is unlisted, which makes me think its a ghost.
    +1
  • The Helper The Helper:
    Some members choose invisibility mode
    +1
  • The Helper The Helper:
    I bitch about Xenforo sometimes but it really is full featured you just have to really know what you are doing to get the most out of it.
  • The Helper The Helper:
    It is just not easy to fix styles and customize but it definitely can be done
  • The Helper The Helper:
    I do know this - xenforo dropped the ball by not keeping the vbulletin reputation comments as a feature. The loss of the Reputation comments data when we switched to Xenforo really was the death knell for the site when it came to all the users that left. I know I missed it so much and I got way less interested in the site when that feature was gone and I run the site.
  • Blackveiled Blackveiled:
    People love rep, lol
    +1
  • The Helper The Helper:
    The recipe today is Sloppy Joe Casserole - one of my faves LOL https://www.thehelper.net/threads/sloppy-joe-casserole-with-manwich.193585/
  • The Helper The Helper:
    Decided to put up a healthier type recipe to mix it up - Honey Garlic Shrimp Stir-Fry https://www.thehelper.net/threads/recipe-honey-garlic-shrimp-stir-fry.193595/

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top