labs-edaf30/lab1/buggy_programs/dangling.cc
Sven Gestegard Robertz e4df45f4a9 imported lab skeletons
2021-10-27 15:39:22 +02:00

37 lines
650 B
C++

#include <iostream>
#include <numeric>
using std::cout;
struct Foo{
Foo(int s) :p(new int[s]), sz(s) {}
~Foo() {delete[] p;}
int* begin() {return p;}
int* end() {return begin()+sz;}
const int* begin() const {return p;}
const int* end() const {return begin()+sz;}
int* p;
int sz;
};
void print(Foo f)
{
for(const auto& x : f) cout << x << " ";
endl(cout);
}
void example1()
{
Foo f(10);
std::iota(std::begin(f), std::end(f), 0);
print(f);
cout << "after first print\n";
*f.begin()=123;
cout << "printing again\n";
print(f);
cout << "done\n";
}
int main()
{
example1();
}