C++: Improve the example for cpp/return-stack-allocated-memory.

This commit is contained in:
Geoffrey White
2024-07-08 10:59:09 +01:00
parent 3c70583aa2
commit d52210d565

View File

@@ -1,7 +1,5 @@
Record* fixRecord(Record* r) {
Record myRecord = *r;
delete r;
Record *mkRecord(int value) {
Record myRecord(value);
myRecord.fix();
return &myRecord; //returns reference to myRecord, which is a stack-allocated object
}
return &myRecord; // BAD: return a pointer to `myRecord`, which is a stack-allocated object
}