C++ Note
new/delete
vsmalloc/free
new/delete
( keyword ) call the Constructor/Destructor.
malloc/free
( std. function ) simply allocate memory from the heap.OOP features
Encapsulation:Human { public: void eat(); private: void openMouth(); void putFoodIn(); void closeMouth(); } void Human::eat() { openMouth(); putFoodIn(); closeMouth(); }
Abstraction: Essential characteristics of model.
Inheritance & Polymorphism:
Women
andMan
, since they are part ofHuman
, so both canspeak()
, but theirvoice
is different.union
The union is only as big as necessary to hold its largest data member.
A union can have member functions (including constructors and destructors), but not virtual functions.
A union cannot have base classes and cannot be used as a base class.
A union cannot have data members of reference types.stack vs heap
Stack:- Stored in computer RAM just like the heap.
- Variables created on the stack will go out of scope and automatically deallocate.
- Much faster to allocate in comparison to variables on the heap.
- Implemented with an actual stack data structure.
- Stores local data, return addresses, used for parameter passing
- Can have a stack overflow when too much of the stack is used. (mostly from infinite (or too much) recursion, very large allocations)
- Data created on the stack can be used without pointers.
- You would use the stack if you know exactly how much data you need to allocate before compile time and it is not too big.
- Usually has a maximum size already determined when your program starts.
Heap:
- Stored in computer RAM just like the stack.
- In C, variables on the heap must be destroyed manually and never fall out of scope. The data is freed with delete, delete[], or free
- Slower to allocate in comparison to variables on the stack.
- Used on demand to allocate a block of data for use by the program.
- Can have fragmentation when there are a lot of allocations and deallocations
- In C++ data created on the heap will be pointed to by pointers and allocated with new or malloc
- Can have allocation failures if too big of a buffer is requested to be allocated.
- You would use the heap if you don’t know exactly how much data you will need at runtime or if you need to allocate a lot of data.
- Responsible for memory leaks.
reference vs pointer
- reference have to be initialized.
- reference can not be modified(not value).
- local reference should not be return value
array vs pointer
char amessage[] = "now is the time";
char *pmessage = "now is the time";
amessage[0] = 'N' //OK
pmessage[0] = 'N' //Compile OK,But run NG
amessage
is an array who live in stack.
pmessage
is a pointer who live in stack, but its value is commonly stored in a string table.
sizeof(pmessage)
is size of pointer but not string length.
before
main()
, global object while be constructedvirtual destructor
class A{
public:
~A(){
qDebug()<<"A Dead";
}
};
class B: public A{
public:
~B(){
qDebug()<<"B Dead";
}
};
A* ab = new B();
delete ab; // A Dead
//But
class A{
public:
virtual ~A(){
qDebug()<<"A Dead";
}
};
// A Dead
// B Dead
Stack Overflow
Eg: infinite recursion.Heap Overflow
garbage collection.Inline function
If a function is inline, the compiler places a copy of the code of that function at each point where the function is called at compile time.
So, it decreases the frequency of function call but it increases the size of function which contains the call of inline function .
To be continue…