Logo

dev-resources.site

for different kinds of informations.

Global vs Static in C++

Published at
1/4/2025
Categories
cpp
hpc
cuda
Author
minwook
Categories
3 categories in total
cpp
open
hpc
open
cuda
open
Author
7 person written this
minwook
open
Global vs Static in C++

Key Differences

Aspect Global Variable Static Variable
Scope Accessible throughout program Limited to file, function, or class scope
Visibility Shared across files (extern) Restricted to declaring scope
Lifetime Entire program duration Entire program duration

Example Code

Global Variable

// globals.cpp
int globalVar = 42; // Global variable

// main.cpp
#include <iostream>
extern int globalVar; // Declaration for the global variable from another file

void modifyGlobal() {
    globalVar++; // Accessible across files
}

int main() {
    modifyGlobal();
    std::cout << "Global Variable: " << globalVar << std::endl; // Prints: 43
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

File-Scope Static Variable

#include <iostream>

static int staticVar = 10; // File-scope static

void modifyStatic() {
    staticVar++; // Accessible only within this file
}

int main() {
    modifyStatic();
    std::cout << "Static Variable: " << staticVar << std::endl; // Prints: 11
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Function-Scope Static Variable

#include <iostream>

void counter() {
    static int count = 0; // Retains value across calls
    count++;
    std::cout << "Count: " << count << std::endl;
}

int main() {
    counter(); // Prints: 1
    counter(); // Prints: 2
    return 0;
}

Enter fullscreen mode Exit fullscreen mode
cuda Article's
30 articles in total
Favicon
Coalesced Memory Access in CUDA for High-Performance Computing
Favicon
Accelerating Data Processing with Grid Stride Loops in CUDA
Favicon
Running Nvidia COSMOS on A100 80Gb
Favicon
Accelerating Python with Numba - Introduction to GPU Programming
Favicon
OpenMP Data-Sharing Clauses: Differences Explained
Favicon
Global vs Static in C++
Favicon
"Learn HPC with me"ย kickoff
Favicon
Snooping on your GPU: Using eBPF to Build Zero-instrumentation CUDA Monitoring
Favicon
Qt error when opening ncu-ui
Favicon
Cuda help
Favicon
Using Polars/Tensorflow with NVIDIA GPU (CUDA), on Windows using WSL2
Favicon
Lattice Generation using GPU computing in realtime
Favicon
Tensorman: TensorFlow with CUDA made easy
Favicon
Simplifying PyTorch Installation: Introducing Install.PyTorch
Favicon
Setup Nx lib and EXLA to run NX/AXON with CUDA
Favicon
NVIDIA GPU & CUDA
Favicon
Deep Learning with โ€œAWS Graviton2 + NVIDIA Tensor T4Gโ€ for as low as free* with CUDA 12.2
Favicon
My Experience Running HeadJobs: Generative AI at Home
Favicon
Why Your AWS Deep Learning AMI is Holding You Back and How toย Fix
Favicon
NVIDIA's $200B Overnight Gain: Trending CUDA Repos Revealed! โšก๏ธ
Favicon
Trending CUDA repos of the week ๐Ÿ“ˆ
Favicon
Cheapest CUDA-Compatible Cloud GPU Options in 2023
Favicon
Dockerize CUDA-Accelerated Applications
Favicon
nVidia 525 + Cuda 11.8 + Python 3.10 + pyTorch GPU Docker image
Favicon
How to use GPU for Deep Learning
Favicon
Install NVIDIA CUDA on Linux
Favicon
Import error: cannot open shared object file:no such file or directory
Favicon
[BTY] Day 1: Install NVIDA Driver, CUDA, and CUDNN on Ubuntu 20.04
Favicon
Training ESRGAN: Seemingly impossible
Favicon
GPGPU-sim Day 1

Featured ones: