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