Module 11: How memory works
This commit is contained in:
parent
d6262f2151
commit
8269a15a56
19
how_memory_works/addresses_&_pointers.c
Normal file
19
how_memory_works/addresses_&_pointers.c
Normal file
@ -0,0 +1,19 @@
|
||||
#include <iostream>
|
||||
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
|
||||
}
|
BIN
how_memory_works/b
Executable file
BIN
how_memory_works/b
Executable file
Binary file not shown.
BIN
how_memory_works/c
Executable file
BIN
how_memory_works/c
Executable file
Binary file not shown.
BIN
how_memory_works/d
Executable file
BIN
how_memory_works/d
Executable file
Binary file not shown.
BIN
how_memory_works/e
Executable file
BIN
how_memory_works/e
Executable file
Binary file not shown.
22
how_memory_works/heap.c
Normal file
22
how_memory_works/heap.c
Normal file
@ -0,0 +1,22 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
using namespace std;
|
||||
|
||||
// motivation for using the heap is it is dynamic
|
||||
int main () {
|
||||
vector<int> 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<int> q (new int(8)); // smart pointers that deletes themselves when destroyed
|
||||
cout << *q << "\n";
|
||||
int z = *q;
|
||||
cout << z << "\n";
|
||||
}
|
48
how_memory_works/passing_values.c
Normal file
48
how_memory_works/passing_values.c
Normal file
@ -0,0 +1,48 @@
|
||||
#include <iostream>
|
||||
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";
|
||||
}
|
19
how_memory_works/to_upper.c
Normal file
19
how_memory_works/to_upper.c
Normal file
@ -0,0 +1,19 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
void ToUpper(string& s) {
|
||||
for (int i=0; i<s.length(); i++) {
|
||||
s[i] = toupper(s[i]);
|
||||
}
|
||||
// for (char& c : s) {
|
||||
// c = toupper(c);
|
||||
// }
|
||||
}
|
||||
|
||||
int main() {
|
||||
string name("dog");
|
||||
cout << name << "\n";
|
||||
ToUpper(name);
|
||||
cout << name << "\n";
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user