Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Windows Tipps & SecurityNighthawk M7 Pro im Test: Flexibler, aber teurer 5G-Router(21.09.2026 um 10:30 Uhr)
Sichere ProgrammierungNeue Gmail-Funktion: So sparst du jetzt Zeit bei Einmalcodes(21.09.2026 um 10:00 Uhr)
Sichere ProgrammierungYour GIF exporter is fine — the container is the problem(21.09.2026 um 10:01 Uhr)
Sichere ProgrammierungCSS, Motion, or GSAP? I Choose by Who Owns the Animation(21.09.2026 um 10:12 Uhr)
Windows Tipps & SecurityNighthawk M7 Pro im Test: Flexibler, aber teurer 5G-Router(21.09.2026 um 10:30 Uhr)
Sichere ProgrammierungNeue Gmail-Funktion: So sparst du jetzt Zeit bei Einmalcodes(21.09.2026 um 10:00 Uhr)
Sichere ProgrammierungYour GIF exporter is fine — the container is the problem(21.09.2026 um 10:01 Uhr)
Sichere ProgrammierungCSS, Motion, or GSAP? I Choose by Who Owns the Animation(21.09.2026 um 10:12 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

Static in the house - C++ tales #1

What is static In C++, static is a keyword that primarily affects a variable's storage duration. A variable declared as static exists for the entire lifetime of the program. However, there is a little more to it. Depending on where it…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!




What is static



In C++, static is a keyword that primarily affects a variable's storage duration. A variable declared as static exists for the entire lifetime of the program.



However, there is a little more to it. Depending on where it is used, static can also affect a symbol's visibility, which is known as linkage.






Static: The Local Hero



When a variable is declared inside a function, it can only be accessed within that function. This is true for both automatic and static local variables. The difference lies in their lifetime. Automatic variables are created every time the function is called and destroyed when the function returns. A local static variable, on the other hand, is initialized only once and remains alive until the program terminates.



Although the variable continues to exist after the function returns, it is still only accessible from within the function where it was declared. Because its lifetime extends beyond the function call, a local static variable is not typically stored on the stack. It also preserves its value between function calls.






Example time



Here is a little showcase code:




#include <cstdio>

void test_static() {
static int count{};
int other_count{};

count++;
other_count++;

printf("The test_static func is called %d times!!!\n", count);
printf("This is other_count >> %d\n", other_count);
}

int main() {
test_static();
test_static();
test_static();

return 0;
}






...and the output:




The test_static func is called 1 times!!!
This is other_count >> 1
-------------------------------------------------------
The test_static func is called 2 times!!!
This is other_count >> 1
-------------------------------------------------------
The test_static func is called 3 times!!!
This is other_count >> 1
-------------------------------------------------------






Here, we have a test_static function with a static and automatic variable: count and other_count. In the output, it can be easily what happens with this two little guy. The count - what is a static variable of the test_static funtion - keeps the value and in every function call it is incremented and keeps and increase the value while the other_count is incremented, too although it cannot keep the own value between the function calls.






Static, the member



The behavior of the static is pretty similar in classes but have some differencies, too.



Let's check this:




class PlanetOfTheSolarSystem {
public:
static std::string our_star;
std::string planet_name;

PlanetOfTheSolarSystem(std::string n) {
this->planet_name = n;
}

std::string get_planet_name() {
return this->planet_name;
}
};

std::string PlanetOfTheSolarSystem::our_star{ "Sun (other name is Sol)" };






The PlanetOfTheSolarSystem class has two variable our_star and the planet_name. While the planet_name is declared in the constructor, the static varialbe - our_star - is declared outside of the class, and a different syntax is used to it. The reason is the static member belongs to class and not to the object.




void print_planets_star(std::string planet_name, std::string star_of_the_planet) {
printf("Star of the %s is %s\n", planet_name.c_str(), star_of_the_planet.c_str());
}

int main() {
PlanetOfTheSolarSystem venus{ "Venus" };
PlanetOfTheSolarSystem earth{ "Earth" };

print_planets_star(venus.get_planet_name(), venus.our_star);
print_planets_star(earth.get_planet_name(), earth.our_star);

return 0;
}









Star of the Venus is Sun (other name is Sol)
Star of the Earth is Sun (other name is Sol)






This example code and the output show us the essence of the static member. The venus and the earth objects point to same variable.






Conclusion



In a nutshell, static is an essential C++ feature for creating data that must persist throughout the program's execution. When used inside functions, it allows values to survive between function calls. When used as a class member, it creates data shared by all instances of the class. In other contexts, it can also affect symbol visibility and linkage.



Understanding static is important because it appears frequently in real-world C++ code and forms the basis of several common programming techniques and design patterns.



Thanks for reading, and keep learning!

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Static in the house - C++ tales #1

Thematisch verwandte Begriffe: Static, house, tales · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-94030 | A security vulnerability has been detected in SerenityOS up to 3d83e4509…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick