Re: minor confusion

From: Robert W Hand (rwhand_at_NOSPAMoperamail.com)
Date: 11/01/03


Date: Sat, 01 Nov 2003 05:03:30 -0500

On 31 Oct 2003 19:10:24 -0800, forums_mp@hotmail.com (forums_mp)
wrote:

>and again my confusion. "gh" is a 'copy' (member wise) of t. hence
>when gh goes out of scope why does "t" care?

The compiler generated copy constructor makes gh from t. The pointer
to int, data, will have the same value in both objects. That value
points to the anonymous array that was formed in the default
constructor when t was formed. So t and gh are not independent
objects. They share that array. When the destructor of gh is called,
that array is destroyed. It is again destroyed when t goes out of
existence. That is an example of undefined behavior.

Reread Mark's notes. He gave good advice about writing your own copy
constructor.

>// consider
>int idx = 5;
>int jdx = test (idx);
>int test (int a)
>{
> return a++;
>}
>the fact that a has block scope means nothing to idx. so now "a" got a
>copy of idx. "a" post incremented the copy of idx. "a" is no longer
>in existence, however idx is preserved. the difference here is the
>parameter is of type int but pass by value is pass by value, hence my
>confusion.

When the object passed by value is a pointer, doing something to the
objected pointed to, may change the apparent semantics. In your OP
example, the array, that data pointed to, was deleted twice.

Best wishes,

Bob



Relevant Pages

  • Re: Sorting records using sort()
    ... > We have an array of n*m bytes. ... > proxy objects store a pointer to some master descriptor that stores the ... > need a default constructor, the value type needs to be assignable only ...
    (comp.lang.cpp)
  • Re: Warning on assigning a function-returning-a-pointer-to-arrays
    ... This declares pfunc as a function taking no arguments and returning ... int x, y; ... Presumably pfuncwill return a pointer to a single int, ... or the first of a sequence of "array 5 of int"s. ...
    (comp.lang.c)
  • Re: Newbie
    ... to talk about the int value 3 and the int value 4, ... It also lets you talk about pointer ... C has a special rule for array objects. ... to printf() is: ...
    (comp.lang.c)
  • Re: union {unsigned char u[10]; ...}
    ... But character type is not a union. ... u.a is of type int. ... has to do so to make pointer equality work consistently). ... were a single-element array. ...
    (comp.lang.c)
  • Re: union {unsigned char u[10]; ...}
    ... But character type is not a union. ... u.a is of type int. ... has to do so to make pointer equality work consistently). ... were a single-element array. ...
    (comp.lang.c)