3.1 C++ Review and Best Practices
3.1.1 Brief Review of Object-Oriented Programming
Classes and Objects
Encapsulation : means that an object presents anly a limited interface to the outside world; the object's internal state and implementation details are kept hidden.
Inheritance : "is-a" relationship
Multiple Inheritance :
Polymorphism : is a language feature that allows a collection of objects of diffirent types to be manipulated through a single common interface.
Design Patterns: Singleton, Iterator, Abstract factory.
3.1.2 Coding Standards: why and how much?
Two primary reasons:
1 some standards make the code more readable , understandable, and maintainable.
2.Other conventions help to prevent programmers from shooting themselves in the foot.
Need to achieve:
Interfaces are king; Good names encourage understanding and avoid confusion; Don't clutter the global namespace; Follow C++ best practices; Be consistent.
3.2 Data, Code , and Memory in C/C++
3.2.1 Numeric Representations
Numeric Bases
0b1101 = (1*23)+(1*22)+(0*21)+(1*20) = 13
0xB052 = (11*163)+(0*162)+(5*161)+(2*160) =45,138 .
Signed and Unsigned Integers
Fixed-Point Notation
Floating-Point Notation
Atomic Data Types
Multi-Byte Values and Endianness
3.2.2. Declarations, Defi nitions, and Linkage
3.2.3. C/C++ Memory Layout
3.2.3.1. Executable Image
1. Text segment. Sometimes called the code segment, this block contains executable
machine code for all functions defi ned by the program.
2. Data segment. This segment contains all initialized global and static variables.
3. BSS segment. “BSS” is an outdated name which stands for “block started
by symbol.” This segment contains all of the uninitialized global and static
variables defi ned by the program.
4. Read-only data segment. Sometimes called the rodata segment, this segment
contains any read-only (constant) global data defi ned by the program.
3.2.3.2. Program Stack
1. It stores the return address of the calling function, so that execution may
continue in the calling function when the called function returns.
2. The contents of all relevant CPU registers are saved in the stack frame.
3. The stack frame also contains all local variables declared by the function;
these are also known as automatic variables.
Example:
void c()
{
U32 localC1;
// ...
}
F32 b()
{
F32 localB1;
I32 localB2;
// ...
c(); // call function c()
// ...
return localB1;
}
void a()
{
U32 aLocalsA1[5];
// ...
F32 localA2 = b(); // call function b()
// ...
}
3.2.3.3. Dynamic Allocation Heap
To allow for dynamic allocation, the operating system maintains a block of memory that can be allocated by a running program by calling malloc() and later returned to the pool for use by other programs by calling free(). This memory block is known as heap memory, or the free store.
In C++, the global new and delete operators are used to allocate and free memory to and from the heap.
3.2.4. Member Variables
3.2.4.1. Class-Static Members
As we’ve seen, the static keyword has many diff erent meanings depending on context:
When used at fi le scope, static means “restrict the visibility of this variable or function so it can only be seen inside this .cpp fi le.”
When used at function scope, static means “this variable is a global, not an automatic, but it can only be seen inside this function.”
When used inside a struct or class declaration, static means “this variable is not a regular member variable, but instead acts just like a
global.”
3.2.5. Object Layout in Memory
struct Foo
{
U32 mUnsignedValue;
F32 mFloatValue;
I32 mSignedValue;
};
3.2.5.1. Alignment and Packing
You might imagine that the compiler simply packs the data members into memory as tightly as it can.
struct InefficientPacking
{
U32 mU1; // 32 bits
F32 mF2; // 32 bits
U8 mB3; // 8 bits
I32 mI4; // 32 bits
bool mB5; // 8 bits
char* mP6; // 32 bits
};
3.2.5.2. Memory Layout of C++ Classes