Skip to content

Instantly share code, notes, and snippets.

@paulocoutinhox
Last active December 21, 2018 00:53
Show Gist options
  • Save paulocoutinhox/af0782115310a8de48df0de38fe26dea to your computer and use it in GitHub Desktop.
Save paulocoutinhox/af0782115310a8de48df0de38fe26dea to your computer and use it in GitHub Desktop.
C++ memory samples
#include <iostream>
#include <string>
#include <vector>
#include <memory>
void show_1(int value) {
std::cout << "show_1: " << &value << std::endl;
}
void show_2(int & value) {
std::cout << "show_2: " << &value << std::endl;
}
void show_3(int * value) {
std::cout << "show_3: " << value << std::endl;
}
void show_4(std::vector<int> value) {
std::cout << "show_4: " << &value << std::endl;
}
void show_5(std::vector<int> & value) {
std::cout << "show_5: " << &value << std::endl;
}
void show_6(const std::vector<int> value) {
std::cout << "show_6: " << &value << std::endl;
}
void show_7(std::shared_ptr<int> value) {
std::cout << "show_7: " << &value << std::endl;
}
void show_8(std::shared_ptr<int> & value) {
std::cout << "show_8: " << value << std::endl;
}
void show_9(std::shared_ptr<int> * value) {
std::cout << "show_9: " << value << std::endl;
}
const int get_1() {
int value = 1;
std::cout << "get_1: " << &value << std::endl;
return value;
}
constexpr int get_2() {
return 1;
}
std::vector<int> get_3() {
auto value = std::vector<int>{1, 2, 3};
std::cout << "get_3: " << &value << std::endl;
return value;
}
std::shared_ptr<int> get_4() {
auto value = std::make_shared<int>(1);
std::cout << "get_4: " << &value << std::endl;
return value;
}
int main()
{
int a = 1;
int b = a;
int *c = &a;
std::cout << "a: " << &a << std::endl;
std::cout << "b: " << &b << std::endl;
std::cout << "c: " << c << std::endl;
show_1(a);
show_2(a);
show_3(&a);
int get_1_result = get_1();
std::cout << "value from get 1: " << &get_1_result << std::endl;
int get_2_result = get_1();
std::cout << "value from get 2: " << &get_2_result << std::endl;
auto get_3_result = get_3();
std::cout << "value from get 3: " << &get_3_result << std::endl;
show_4(get_3_result);
show_5(get_3_result);
show_6(get_3_result);
auto get_4_result = get_4();
std::cout << "value from get 4: " << &get_4_result << std::endl;
show_7(get_4_result);
show_8(get_4_result);
show_9(&get_4_result);
return EXIT_SUCCESS;
}
/*
Start
prog.cc:42:1: warning: type qualifiers ignored on function return type [-Wignored-qualifiers]
42 | const int get_1() {
| ^~~~~
a: 0x7fff548a6a6c
b: 0x7fff548a6a68
c: 0x7fff548a6a6c
show_1: 0x7fff548a6a1c
show_2: 0x7fff548a6a6c
show_3: 0x7fff548a6a6c
get_1: 0x7fff548a6a1c
value from get 1: 0x7fff548a6a64
get_1: 0x7fff548a6a1c
value from get 2: 0x7fff548a6a60
get_3: 0x7fff548a6a40
value from get 3: 0x7fff548a6a40
show_4: 0x7fff548a6a70
show_5: 0x7fff548a6a40
show_6: 0x7fff548a6a90
get_4: 0x7fff548a6a30
value from get 4: 0x7fff548a6a30
show_7: 0x7fff548a6ab0
show_8: 0xb0cc60
show_9: 0x7fff548a6a30
0
Finish
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment