Year End Sale: Get Upto 40% OFF on Live Training! Offer Ending in
D
H
M
S
Get Now

Storage Classes in C++

Level : Intermediate
Mentor: Shailendra Chauhan
Duration : 00:02:00

What is the Storage Class in the C++ programming language?

Storage classes are keywords that specify the scope (visibility) and lifetime (duration) of variables, such as auto, register, and static. They specify how memory is allocated and released for variables in a C++ program.

Why are storage classes used in C++?

  • Memory Management: Effectively allocates memory for variables based on their usage and scope.
  • Lifetime Control: Controls the lifespan of variables, ensuring proper allocation and deallocation.
  • Performance optimization: Uses CPU registers for faster access, increasing code execution speed.
  • Sharing Data: Allows variables to be shared between files, encouraging communication within the code base.
  • Code Organization: Chooses storage classes to represent variable properties, resulting in structured, manageable code.

Types of Storage Classes in C++

There are 6 different types of storage classes in C++:

  1. Automatic Storage Class in C++
  2. Register Storage Class in C++
  3. Static Storage Class in C++
  4. Extern Storage Class in C++
  5. Mutable Storage Class in C++
  6. thread_local Storage Class in C++

Automatic Storage Class in C++

The Automatic Storage Class in C++ allows variables to be declared without specifying their type, allowing for greater flexibility at runtime. This class allows the definition of variables such as int, double, and char, which improves program clarity and speed by optimizing memory access and organization. 

Static Storage Class in C++

A static variable, specified using the static keyword, can be declared several times but only given a value once. Its default value is 0 or null, and it lasts from the function call until the program terminates, continuing throughout. Static local variables are only visible in the function in which they are defined, but global static variables are visible throughout the program.

Register Storage Class in C++

The ' register' keyword instructs the compiler to store a variable in high-speed registers, which improves access speed. It cannot be referenced via the '&' operator, has a default value of zero, and is not required for modern compiler optimizations. It was removed from C++11 and should no longer be used.

External Storage Class in C++

External or global variables, denoted with 'extern', can be declared several times but only given a value once. They start at 0 or null, have external linkage, and are not allocated memory until expressly initialized. Initialization must be global; otherwise, the compiler will throw an error.

Mutable Storage Class in C++

A mutable storage class in C++ programming is used to change one or more data elements of a structure/class using a constant function. This task can be readily advanced by changeable keywords, which are especially useful for allowing a specific data member of an object to be a modifier.

thread_local Storage Class in C++

Thread_local Storage Class is a new storage class added to C++11. To make the object thread-local, use the thread_local storage class specifier. The thread_local object's characteristics change when the thread_local variable is combined with other storage specifiers like static or extern.

Self-paced Membership
  • 24+ Video Courses
  • 825+ Hands-On Labs
  • 400+ Quick Notes
  • 125+ Skill Tests
  • 10+ Interview Q&A Courses
  • 10+ Real-world Projects
  • Career Coaching Sessions
  • Email Support
Upto 60% OFF
Know More
Still have some questions? Let's discuss.
CONTACT US
Accept cookies & close this