Re: beginner's memory leak question

Discussion in 'C++' started by Spikinsson, Jul 18, 2003.

  1. Spikinsson

    Spikinsson Guest

    "Russell Silva"
    > I have a function, let's call it foo. It takes a parameter of type
    > bar, a class I've made myself.
    >
    > void foo (bar parameter)
    >
    > Somewhere, I decide to call function foo, but with a new bar:
    >
    > foo (new bar());
    >
    > alas! new returns a pointer, not a value. that last line won't
    > compile. thus I amend:
    >
    > foo (*(new bar()));
    >
    > However, I now have a different dilemma. The new bar object is
    > created, then passed by value (copying it?) to function foo. I never
    > delete the new bar, and with the code as written, I can't, since I
    > have no reference to it. Is this a memory leak?
    >
    > Thanks in advance,
    >
    > Russell Silva


    Yes!
    Spikinsson, Jul 18, 2003
    #1
    1. Advertising

  2. "Tim Veldhuizen" <> wrote in message
    news:ejOTa.550724$...
    > I guess that something like this is also allowed:
    > foo( (class bar *myBarObj = new bar() );
    > delete myBarObj;


    Hmm, that probably couses a parse error. This one is better (forgot a
    closing bracket):
    foo( (class bar *myBarObj = new bar()) );

    Though i still don't recommend using new within a function call.. ;)

    Tim.
    Tim Veldhuizen, Jul 24, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrey Tarasevich

    Re: beginner's memory leak question

    Andrey Tarasevich, Jul 18, 2003, in forum: C++
    Replies:
    2
    Views:
    287
    Andrey Tarasevich
    Jul 18, 2003
  2. s.subbarayan

    Dynamic memory allocation and memory leak...

    s.subbarayan, Mar 18, 2005, in forum: C Programming
    Replies:
    10
    Views:
    675
    Eric Sosman
    Mar 22, 2005
  3. Richard Heathfield

    Leak or no leak ??

    Richard Heathfield, Jul 10, 2006, in forum: C Programming
    Replies:
    4
    Views:
    337
    Richard Heathfield
    Jul 10, 2006
  4. cham
    Replies:
    5
    Views:
    753
  5. Mark Probert
    Replies:
    4
    Views:
    315
    Mark Probert
    Feb 9, 2005
Loading...

Share This Page