Re: memory leak

Discussion in 'C++' started by ephlodur, Mar 3, 2013.

  1. ephlodur

    ephlodur Guest

    On Fri, 01 Mar 2013 12:34:19 +0000, Jorgen Grahn wrote:
    I'm not sure I understand your comment below
    > (They will also tell you you shouldn't uselessly call operator new, and
    > rely on constructors instead of manual initialization some time after
    > the object was created.)
    ephlodur, Mar 3, 2013
    #1
    1. Advertising

  2. ephlodur

    Jorgen Grahn Guest

    On Sun, 2013-03-03, ephlodur wrote:
    > On Fri, 01 Mar 2013 12:34:19 +0000, Jorgen Grahn wrote:
    > I'm not sure I understand your comment below


    Your code upthread (in comp.unix.programmer; I hope you noticed the
    followup-to) was

    > struct MyData
    > {
    > std::string msg;
    > int level;
    > };
    >
    > int main( void )
    > {
    > MyData * _m = new MyData;
    >
    > _m->msg = "hello";
    > _m->level = 0;
    >
    > delete _m;
    > }


    >> (They will also tell you you shouldn't uselessly call operator new,


    The 'new' above wasn't needed. You could have just said

    MyData m;
    m.msg = "hello";
    m.level = 0;

    Perhaps it was needed because of your question about memory leaks, but
    it's a common mistake for Java programmers to abuse C++ new exactly
    like this, so it was worth mentioning.

    >> and
    >> rely on constructors instead of manual initialization some time after
    >> the object was created.)


    Perhaps it's better if you explain /what/ you don't understand first.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
    Jorgen Grahn, Mar 3, 2013
    #2
    1. Advertising

  3. ephlodur

    Guest

    On Thursday, February 28, 2013 5:59:35 PM UTC-7, ephlodur wrote:
    > hello all,
    >
    >
    >
    > I'm wondering is there memory leak is the below situation
    >
    >
    >
    > struct MyData
    >
    >
    >
    > {
    >
    >
    >
    > std::string msg;
    >
    > int level;
    >
    >
    >
    > };
    >
    >
    >
    > int main( void )
    >
    > {
    >
    > MyData * _m = new MyData;
    >
    >
    >
    > _m->msg = "hello";
    >
    > _m->level = 0;
    >
    >
    >
    > delete _m;
    >
    > }


    If std::string's assignment operator throws above you will leak memory as you
    will never reach delete _m. In this case, if std::string::eek:perator= throws
    your program will terminate and the operating system will clean up your mess
    (note that it probably won't here due to short string optimizations – but
    that's implementation specific). Follow Jorgen's suggestion if possible oruse
    std::unique pointer if you really wanted to dynamically allocate the object:

    MyData _m {“hello”, 0};
    MyData _m = {“hello”, 0};
    std::unique_ptr <MyData> _m {new MyData {“hello”, 0}};
    , Mar 3, 2013
    #3
    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. =?Utf-8?B?Y3liZXJzdHJpa2U=?=

    datagrid memory leak?

    =?Utf-8?B?Y3liZXJzdHJpa2U=?=, Jan 3, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    465
    =?Utf-8?B?Y3liZXJzdHJpa2U=?=
    Jan 3, 2005
  2. s.subbarayan

    Dynamic memory allocation and memory leak...

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

    Leak or no leak ??

    Richard Heathfield, Jul 10, 2006, in forum: C Programming
    Replies:
    4
    Views:
    352
    Richard Heathfield
    Jul 10, 2006
  4. cham
    Replies:
    5
    Views:
    770
  5. Mark Probert
    Replies:
    4
    Views:
    328
    Mark Probert
    Feb 9, 2005
Loading...

Share This Page