Skip to content
Snippets Groups Projects
Commit eda343db authored by Sandipan Mohanty's avatar Sandipan Mohanty
Browse files

Add missing memory error section

parent 91846a32
No related branches found
No related tags found
No related merge requests found
No preview for this file type
#include <iostream>
#include <string>
auto main(int argc, char* argv[]) -> int
{
unsigned long* p = nullptr;
if (argc > 1) {
auto i = std::stoul(argv[1]);
p = &i;
// Since p outlives i, storing address
// of i in p is a bad idea
std::cout << "p is pointing at " << p << " storing a value " << *p << "\n";
} else {
std::cout << "Needs one integer command line argument!\n";
}
// At this point, if argc > 1, p is pointing to
// a location where there was a variable called i
// which has run out of scope. Accessing p here is
// undefined behaviour.
if (p)
std::cout << "p is pointing at " << p << " storing a value " << *p << "\n";
}
#include <vector>
#include <iostream>
auto main() -> int
{
std::vector v{1, 2, 3};
const auto& vstart = v.front();
v.push_back(4);
v.push_back(5);
v.push_back(6);
v.push_back(7);
v.push_back(8);
std::cout << "Start element of v = " << vstart << "\n";
}
#include <iostream>
#include <string>
auto f(double x) -> double&
{
auto y = x * x;
return y;
}
auto main() -> int
{
auto a = 4.0;
auto&& z = f(a);
std::cout << z << "\n";
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment