#include <vector>
int main(int argc, char *argv[])
{
std::vector<int> fib;
fib.push_back(1);
fib.push_back(1);
for (int i = 0; i < 10; i++) {
fib.push_back(fib[fib.size() - 1)] + fib[fib.size() - 2)];
}
}
int a;
int func(int b, int c)
{
int num;
return num * b * c * a;
}
The stack here has space for 5 floats!
int *a;
int b = 5;
a = &b;
*a = 10; // Now b is 10
int *a;
int b = 5;
a = &b;
float *c;
c = (float *) a; // Valid but weird results!
char st[] = "hello"; // The size of this array is determined from the content
st[4] = '!';
char *c = st;
for (int i = 0; i < 5; i++)
{
printf("%i - %c\n", c++, st[i]);
}
int *list = (int *) malloc(4 *sizeof(int));
list[0] = 1;
list[1] = 2;
list[2] = 3;
list[3] = 4;
list[4] = 5; // Invalid memory access!!
free(list); // You must free memory yourself
ClassType *variable = new ClassType;
delete variable; // You must free memory yourself
The constructor is called on "new"
The destructor is called on "delete"
unique_ptr : a self-destroying pointer
http://en.cppreference.com/w/cpp/memory/unique_ptr/unique_ptr
In other dynamic memory scope doesn't affect the lifetime of the pointer memory, which can lead to memory leaks.
https://en.wikipedia.org/wiki/Dining_philosophers_problem#/media/File:Dining_philosophers.png
From: https://en.wikipedia.org/wiki/Dining_philosophers_problem