Topic: A simple question


Author: marc@offline.be (Marc Duponcheel)
Date: Sat, 25 Jun 1994 03:21:19 GMT
Raw View
Francis Chang (francis@basie.EECS.Berkeley.EDU) wrote:
: Hi, I am a novice at C++ and I have a simple question, let's say I have
: the following two functions:

: T &foo() {
:   T *t = new T;
:   return *t;
: }

: void bar() {
:   T t2 = foo();
:   ...
: }

: would the storage allocated in foo() be deallocated when bar() exits?
: I would think so...

What makes you think so ? If you 'new' memory you have to 'delete' it
for storage allocated to be deallocated.

--





Author: jrami@cs.huji.ac.il (Rami Jaschek)
Date: 26 Jun 1994 15:37:29 +0300
Raw View
In <2uf74t$ohv@agate.berkeley.edu> francis@basie.EECS.Berkeley.EDU (Francis Chang) writes:

>T &foo() {
>  T *t = new T;
>  return *t;
>}

>void bar() {
>  T t2 = foo();
>  ...
>}

>would the storage allocated in foo() be deallocated when bar() exits?
>I would think so...

Well, I don't really think you meant what you wrote. There are two (rather than
one) allocations taking place. One is done statically using new inside the
function. The second allocation is for an automatic variable in T which is
initialized using the object constructed in foo. Changing t2 is NOT changing
t in foo. Yes, the t2 object will be destroyed when you leave bar but as it
is a different object it will have no effect. You would have lost the object
allocated in foo for good. It's there but you can't reach it.

If your had written :
T& t2 = foo();
this situation would have been avoided. As for deallocation, when references
are disposed of, they don't touch the object.
--
--------------------------------------------------------------------
Rami Jaschek         jrami@cs.huji.ac.il
Harav Kok 33 Bney-Brak Israel.
Tl : 972-3-5795315.




Author: bsb@bora.ms.sub.org (Bernd _Siggy_ Brentrup)
Date: Sun, 26 Jun 1994 20:08:12 GMT
Raw View
Rami Jaschek (jrami@cs.huji.ac.il) wrote:
: In <2uf74t$ohv@agate.berkeley.edu> francis@basie.EECS.Berkeley.EDU (Francis Chang) writes:

: >T &foo() {
: >  T *t = new T;
: >  return *t;
: >}

: >void bar() {
: >  T t2 = foo();
: >  ...
: >}

: >would the storage allocated in foo() be deallocated when bar() exits?
: >I would think so...

: Well, I don't really think you meant what you wrote. There are two (rather than
: one) allocations taking place. One is done statically using new inside the
: function. The second allocation is for an automatic variable in T which is
: initialized using the object constructed in foo. Changing t2 is NOT changing
: t in foo. Yes, the t2 object will be destroyed when you leave bar but as it
: is a different object it will have no effect. You would have lost the object
: allocated in foo for good. It's there but you can't reach it.

: If your had written :
: T& t2 = foo();
: this situation would have been avoided. As for deallocation, when references
: are disposed of, they don't touch the object.

Quoting your first sentence, I still don't see a delete matching the new.

I assume what you meant is this:

void bar() {
  T &t2 = foo();
  ... // code using t2
  delete &t2;
}

I can't imagine ANSI/ISO modifies these semantics.

 -- Siggy
--
========================================================================
bernd s. brentrup  //#   ##  ###              phone:      +49-251-864978
hensenstr. 181    //  # #  # #  #
d 48161 muenster //#  # #  # #  #             email: bsb@bora.ms.sub.org




Author: francis@basie.EECS.Berkeley.EDU (Francis Chang)
Date: 24 Jun 1994 18:03:09 GMT
Raw View
Hi, I am a novice at C++ and I have a simple question, let's say I have
the following two functions:

T &foo() {
  T *t = new T;
  return *t;
}

void bar() {
  T t2 = foo();
  ...
}

would the storage allocated in foo() be deallocated when bar() exits?
I would think so...

Thanks for any help.

Francis