diff --git a/how_memory_works/addresses_&_pointers.c b/how_memory_works/addresses_&_pointers.c new file mode 100644 index 0000000..53c1398 --- /dev/null +++ b/how_memory_works/addresses_&_pointers.c @@ -0,0 +1,19 @@ +#include +using namespace std; + +int main() { + char c = 'A'; + cout << c << "\n"; + // cout << &c << "\n"; // address of c. The compiler will show again the value and not the address + void* p = &c; // let's define a pointer to get the address + cout << p << "\n"; + cout << sizeof(c) << "\n"; // it'll give 1 byte (for chars) + int i = 10; + void* q = &i; + cout << q << "\n"; + cout << sizeof(i) << "\n"; // it'll give 4 bytes (for ints) + char* r = &c; // the pointer "r" will hold the address of c + cout << c << "\n"; // will print "A" + *r = 'B'; // * is the "dereference" op + cout << c << "\n"; // will print "B" because the pointer was modified right before +} \ No newline at end of file diff --git a/how_memory_works/b b/how_memory_works/b new file mode 100755 index 0000000..d73b1e9 Binary files /dev/null and b/how_memory_works/b differ diff --git a/how_memory_works/c b/how_memory_works/c new file mode 100755 index 0000000..a652611 Binary files /dev/null and b/how_memory_works/c differ diff --git a/how_memory_works/d b/how_memory_works/d new file mode 100755 index 0000000..41988ac Binary files /dev/null and b/how_memory_works/d differ diff --git a/how_memory_works/e b/how_memory_works/e new file mode 100755 index 0000000..9753d79 Binary files /dev/null and b/how_memory_works/e differ diff --git a/how_memory_works/heap.c b/how_memory_works/heap.c new file mode 100644 index 0000000..67d9fc5 --- /dev/null +++ b/how_memory_works/heap.c @@ -0,0 +1,22 @@ +#include +#include +#include +#include +using namespace std; + +// motivation for using the heap is it is dynamic +int main () { + vector x(1000); + cout << "size of x = " << sizeof(x) << "\n"; //only 24 bytes instead of 4*1000!!! + int* p = new int(8); // allocate a new byte from the heap (naked pointer) + cout << *p << "\n"; + int y = *p; + cout << y << "\n"; + + delete p; // be careful though; the heap has to be cleared explicitly! memory leaks!!! + + unique_ptr q (new int(8)); // smart pointers that deletes themselves when destroyed + cout << *q << "\n"; + int z = *q; + cout << z << "\n"; +} \ No newline at end of file diff --git a/how_memory_works/passing_values.c b/how_memory_works/passing_values.c new file mode 100644 index 0000000..8d011b3 --- /dev/null +++ b/how_memory_works/passing_values.c @@ -0,0 +1,48 @@ +#include +using namespace std; + +// passing by value +// will not modify the value +void Foo(int x) { + cout << "FOO x = " << x << "\n"; + x += 1; + cout << "BAR x = " << x << "\n"; +} + +// passing by reference +// will modify the value if a reference is used +void Foo(int x, int& y) { + cout << "FOO x = " << x << "\n"; + cout << "FOO y = " << y << "\n"; + x += 1; + y += 2; + cout << "BAR x = " << x << "\n"; + cout << "BAR j = " << y << "\n"; +} + +// passing by pointer +// will modify the value if a pointer is used +void Foo(int x, int& y, int* z) { + cout << "FOO x = " << x << "\n"; + cout << "FOO y = " << y << "\n"; + cout << "FOO z = " << *z << "\n"; + x += 1; + y += 2; + *z += 3; // use the * operator when using pointers + cout << "BAR x = " << x << "\n"; + cout << "BAR j = " << y << "\n"; + cout << "BAR z = " << *z << "\n"; +} + +int main() { + int i = 10; + int j = 20; + int k = 30; + cout << "ORIG i = " << i << "\n"; + cout << "ORIG j = " << j << "\n"; + cout << "ORIG k = " << k << "\n"; + Foo(i, j, &k); // when passing by pointer, used & + cout << "AFTER i = " << i << "\n"; + cout << "AFTER j = " << j << "\n"; + cout << "AFTER k = " << k << "\n"; +} \ No newline at end of file diff --git a/how_memory_works/to_upper.c b/how_memory_works/to_upper.c new file mode 100644 index 0000000..4960104 --- /dev/null +++ b/how_memory_works/to_upper.c @@ -0,0 +1,19 @@ +#include +#include +using namespace std; + +void ToUpper(string& s) { + for (int i=0; i