Introduction to CPP Storage Classes

Understanding Storage Classes in C++ Programming

Storage classes in C++ play a crucial role in defining the visibility and lifetime of variables and functions within a C++ program.

These classes modify the behavior of the types they are associated with. Here, we’ll delve into the various storage classes available in C++ and their specific use cases.

The Auto Storage Class in C++

The auto storage class stands as the default choice for all local variables in C++.

int mountain;
auto int month;

In the above example, both variables are defined with the auto storage class. Note that auto is limited to function scope, meaning it’s suitable for local variables.

The Register Storage Class in C++

Utilized for local variables, the register storage class designates that variables should be stored in a CPU register instead of the RAM. This implies that the variable size can’t exceed the register size and the unary ‘&’ operator cannot be applied to it.

register int miles;

The register class is best suited for variables requiring swift access, like counters. However, it’s important to understand that declaring ‘register’ doesn’t guarantee the variable will reside in a register; it’s dependent on hardware and implementation constraints.

The Static Storage Class in C++

By using the static storage class, a local variable persists throughout the program’s lifespan, avoiding the overhead of repeated creation and destruction during scope changes. This is particularly beneficial for retaining variable values between function calls. In the C++ static storage class is used to define variables that retain their values between function calls, making them persistent throughout the program’s execution.

The static modifier can also be applied to global variables, confining their scope to the declaring file. In the context of a class data member, a static declaration results in a single copy shared among all instances of the class.

Example: Static Variables in C++

#include <iostream>

// Function declaration
void func(void);

static int count = 10; /* Global variable */

int main() {
   while(count--) {
      func();
   }

   return 0;
}

// Function definition
void func(void) {
   static int i = 5; // local static variable
   i++;
   std::cout << "i is " << i;
   std::cout << " and count is " << count << std::endl;
}

The Extern Storage Class

The extern storage class enables global variables to be accessed across all program files. It creates a reference to a pre-defined storage location and doesn’t involve variable initialization.

When working with multiple files sharing a global variable or function, extern is crucial. It is used to declare global variables or functions in separate files.

Example: Using Extern for Global Variables

First File: main.cpp

#include <iostream>
int count;
extern void write_extern();

int main() {
   count = 5;
   write_extern();
}

Second File: support.cpp

#include <iostream>

extern int count;

void write_extern(void) {
   std::cout << "Count is " << count << std::endl;
}

The Mutable Storage Class

The mutable specifier, exclusive to class objects, allows a member to override a const member function. In essence, a mutable member can be modified even within a const member function. This concept becomes more relevant in the context of class objects, as we’ll explore in later tutorials.

Understanding these storage classes in C++ is pivotal for writing efficient and effective code. Each class serves a specific purpose, contributing to the overall performance and organization of your programs.


Discover more from PiEmbSysTech

Subscribe to get the latest posts sent to your email.

Leave a Reply

Scroll to Top

Discover more from PiEmbSysTech

Subscribe now to keep reading and get access to the full archive.

Continue reading