Side effects when passing objects to function in C++
Asked Answered
P

3

5

I have read in C++ : The Complete Reference book the following

Even though objects are passed to functions by means of the normal call-by-value parameter passing mechanism, which, in theory, protects and insulates the calling argument, it is still possible for a side effect to occur that may affect, or even damage, the object used as an argument. For example, if an object used as an argument allocates memory and frees that memory when it is destroyed, then its local copy inside the function will free the same memory when its destructor is called. This will leave the original object damaged and effectively useless.

I do not really understand how the side effect occurs. Could anybody help me understand this with an example ?

Pontic answered 14/10, 2011 at 4:20 Comment(4)
It's not a side effect, it's a badly written type.Mcabee
That particular book is written by one of the worst C++ authors ever. You are better off disposing of it now. Put it in a trash can and light it up. You'll thank me later.Decoder
@FredOverflow, which book do u recommend for a person who knows C programming, have a little knowledge in C++ and want to quickly learn C++ and start the codingPontic
The Definitive C++ Book Guide and ListDecoder
B
1

That passage is probably talking about this situation:

class A {
  int *p;
public:
  A () : p(new int[100]) {}
  // default copy constructor and assignment
 ~A() { delete[] p; }
};

Now A object is used as pass by value:

void bar(A copy)
{
  // do something
  // copy.~A() called which deallocates copy.p
}
void foo ()
{
  A a;  // a.p is allocated
  bar(a);  // a.p was shallow copied and deallocated at the end of  'bar()'
  // again a.~A() is called and a.p is deallocated ... undefined behavior
}
Broadleaf answered 14/10, 2011 at 4:24 Comment(0)
C
5

Here is an example:

class bad_design
{
public:
    bad_design( std::size_t size )
      : _buffer( new char[ size ] )
    {}

    ~bad_design()
    {
        delete[] _buffer;
    }

private:
    char* _buffer;
};

Note that the class has a constructor and a destructor to handle the _buffer resource. It would also need a proper copy-constructor and assignment operator, but is such a bad design that it wasn't added. The compiler will fill those with the default implementation, that just copies the pointer _buffer.

When calling a function:

void f( bad_design havoc ){ ... }

the copy constructor of bad_design is invoked, which will create a new object pointing to the same buffer than the one passed as an argument. As the function returns, the local copy destructor will be invoked which will delete the resources pointed by the variable used as an argument. Note that the same thing happens when doing any copy construction:

bad_design first( 512 );
bad_design error( first );
bad_design error_as_well = first;
Chronicle answered 14/10, 2011 at 4:25 Comment(0)
B
1

That passage is probably talking about this situation:

class A {
  int *p;
public:
  A () : p(new int[100]) {}
  // default copy constructor and assignment
 ~A() { delete[] p; }
};

Now A object is used as pass by value:

void bar(A copy)
{
  // do something
  // copy.~A() called which deallocates copy.p
}
void foo ()
{
  A a;  // a.p is allocated
  bar(a);  // a.p was shallow copied and deallocated at the end of  'bar()'
  // again a.~A() is called and a.p is deallocated ... undefined behavior
}
Broadleaf answered 14/10, 2011 at 4:24 Comment(0)
C
0

Here is another example. The point is that when the callee (SomeFunc) parameter destructor is invoked it will free the same object (ptr) pointed to by the caller argument (obj1). Consequently, any use of the caller argument (obj1) after the invocation will produce a segfault.

#include <iostream>
using namespace std;

class Foo {
public:
    int *ptr;

    Foo (int i) {
        ptr = new int(i);
    }

    ~Foo() {
        cout << "Destructor called.\n" << endl;
        //delete ptr;
    }

    int PrintVal() {
        cout << *ptr;
        return *ptr;
    }
};

void SomeFunc(Foo obj2) {
    int x = obj2.PrintVal();
} // here obj2 destructor will be invoked and it will free the "ptr" pointer.

int main() {
    Foo obj1 = 15;

    SomeFunc(obj1);

    // at this point the "ptr" pointer is already gone.
    obj1.PrintVal();
}
Curie answered 11/12, 2014 at 16:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.