POV-Ray : Newsgroups : povray.off-topic : Smart little programming tricks, where to find ? : Re: Smart little programming tricks, where to find ? Server Time
11 Oct 2024 01:24:19 EDT (-0400)
  Re: Smart little programming tricks, where to find ?  
From: Warp
Date: 19 Mar 2008 12:34:11
Message: <47e14e92@news.povray.org>
stbenge <stb### [at] hotmailcom> wrote:
> I don't find it too difficult to delete every "new".

  You would be surprised.

  There's a potential memory leak here:

void foo()
{
    char* s = new char[100];
    bar();
    delete[] s;
}

  There's a potential memory leak here:

class A
{
    char* s;

 public:
    A(): s(new char[100]) {}
    ~A() { delete[] s; }
};

  Naturally both of these leaks can be fixed by using cleaner code:

void foo()
{
    std::string s(100, ' ');
    bar();
}

class A
{
    std::string s;

 public:
    A(): s(100, ' ') {}
};

  No 'new' -> no leaks.

-- 
                                                          - Warp


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.