c++delete() using a reference variable !

Q1: if you have nothing but a reference to a heap object, how can you delete it?
AA: yes. q(delete & theReference) works

Q1b: how about valgrind q(g++ -g d.cpp; valgrind ./a.out)?
AA: no leak.

See https://stackoverflow.com/questions/3233987/deleting-a-reference. q(new vector<int>) works but is code smell. Code smells tend to be useful tricks.

https://bintanvictor.wordpress.com/?p=10514&preview=true has details on vector memory allocation.

 // --- d.cpp ---
#include <vector>
#include <iostream>
using namespace std;

typedef vector<int> intArray;

intArray & createArray() {
    intArray *arr = new intArray(3, 0);
    return(*arr);
}

int main(int argc, char *argv[]) {
    intArray& array = createArray();
    for (int i=0; i< array.size(); ++i){
      cout<<array[i]<<' ';
    }
    intArray * ptr2ref = & array;
    delete ptr2ref;
}

c++return by ref, briefly

Background: This is an old topic I studied multiple times, but still unfamiliar. I think most people don’t work at such a low level?

Q1: Can return a malloc object by reference? I doubt it. I think the address is lost and there’s no way to free it.

Q1b: if you only have a reference to a heap object, how can you delete it? See https://bintanvictor.wordpress.com/?p=10505&preview=true

Q: Can return a static local by reference? I think so but not popular

Q: Can return a by-ref argument by reference? No need (but legal) since the caller already has that reference. You could return it in an if/else

Q: can return another object in scope by reference? Yes

Return-by-non-const-ref is often used in operator overloading to support counter[3].increment()

The assignment operator returns a const ref.

 

returning const ptr: useless#with test program

I agree with an online post. Returning a const ptr is same as returning a non-const ptr. Caller would clone that const ptr just as it clones a mutable ptr.

int i=444;

int * const pi = &i;

int * p2 = pi;

int main(){

int i2=222;

p2 = &i2;

cout << *p2 <<endl;

}

It does make a difference if you return a ptr-to-const. The caller would make a clone of the ptr-to-const and can’t subsequently write to the pointee.

pointer as field –#1 pattern in c++

This mega-pattern is present in 90% of java and c# classes!

Three related choices for a member variable:

  • Raw pointer
  • Smart ptr
  • char pointer, usually a c-str

In each case, we worry about

  • construction of this field
  • freeing heap memory
  • RAII
  • what if the pointee is not on heap?
  • copy control of this field
  • ownership of the pointer
  • when to return raw ptr and when to return a smart ptr

 

illegal to assign rvalues into non-const Lval-ref

This is a tough one for the average cpp guy

int& myint = getInt(); // illegal, since getInt() returns a nonref.

(What if getInt returns a reference??? . I think it’s legal.)

However, adding a const makes it legal:

int const & var1 = 33;

In fact, this is same thing as the (extremely) common idiom of using const ref as function param. With that, you can pass in either an lvalue (like a literal) or rvalue.

I found the above exlanation on
http://eli.thegreenplace.net/2011/12/15/understanding-lvalues-and-rvalues-in-c-and-c . The author (Eli) points out:

* subtle difference between const lval-ref vs non-const lval-ref (above) * lvalue means “locator value”
* a concise example of mv-semantics
* An everyday example of a function returning an lvalue is operator[] in

myMap[10] =5.6