Saturday, June 12, 2021

static keyword in C++

static in C++

    C memory layout

  1. Memory Layout of C Programs
  2. A typical memory representation of a C program consists of the following sections.

    1. Text segment
    2. Initialized data segment
    3. Uninitialized data segment
    4. Stack
    5. Heap

    Basics

  3. Static Variables in C
  4. Static variables have a property of preserving their value even after they are out of their scope! Hence, static variables preserve their previous value in their previous scope and are not initialized again in the new scope.

  5. Static Keyword in C++
  6. Static keyword has different meanings when used with different types. We can use static keyword with:

    1. Static Variables : Variables in a function, Variables in a class
    2. Static Members of Class : Class objects and Functions in a class

  7. static_cast in C++ | Type Casting operators
  8. A Cast operator is an unary operator which forces one data type to be converted into another data type..

  9. What are the default values of static variables in C?
  10. In C, if an object that has static storage duration is not initialized explicitly, then:

    • — if it has pointer type, it is initialized to a NULL pointer;
    • — if it has arithmetic type, it is initialized to (positive or unsigned) zero;
    • — if it is an aggregate, every member is initialized (recursively) according to these rules;
    • — if it is a union, the first named member is initialized (recursively) according to these rules.

  11. Internal static variable vs. External static variable with Examples in C
  12. The static variable may be internal or external depending on the place of declaration. Static variables are stored in initialised data segments..

  13. Initialization of global and static variables in C
  14. Both of the above programs don’t compile in C. We get the following compiler error in C.

    error: initializer element is not constant

    In C, static and global variables are initialized by the compiler itself. Therefore, they must be initialized with a constant value.

    Note that the above programs compile and run fine in C++, and produce the output as 10..

    In Depth

  15. static members
  16. Inside a class definition, the keyword static declares members that are not bound to class instances. Outside a class definition, it has a different meaning: see storage duration ..

  17. Storage class specifiers in C++
  18. The storage class specifiers are a part of the decl-specifier-seq of a name's declaration syntax. Together with the scope of the name, they control two independent properties of the name: its storage duration and its linkage..

  19. Storage-class specifiers in C
  20. Specify storage duration and linkage of objects and functions:

    • auto - automatic duration and no linkage
    • register - automatic duration and no linkage; address of this variable cannot be taken
    • static - static duration and internal linkage (unless at block scope)
    • extern - static duration and external linkage (unless already declared internal)

  21. Type vs. Incomplete type
  22. Type:Objects, references, functions including function template specializations, and expressions have a property called type, which both restricts the operations that are permitted for those entities and provides semantic meaning to the otherwise generic sequences of bits..

    Incomplete type The following types are incomplete types:

    • the type void (possibly cv-qualified);
    • incompletely-defined object types:
      • class type that has been declared (e.g. by forward declaration) but not defined;
      • array of unknown bound;
      • array of elements of incomplete type;
      • enumeration type from the point of declaration until its underlying type is determined.
    All other types are complete.

  23. How to Start the Homework or a Developer Career?
  24. Some tips on what to consider when you start learning to code.

No comments:

Post a Comment