Re: Memory leaks


Subject: Re: Memory leaks
From: Mike Nordell (tamlin@algonet.se)
Date: Thu Jun 21 2001 - 18:21:08 CDT


[Joaquín Cuenca Abela explained scoping]

Martin Sevior wrote:

> Actually because of the scoping you've beautifully explained
> what we have are lots of instances of
>
> void foo(void)
> {
> UT_String me;
> if(x)
> {
> me = "bar:";
> }
> else
> {
> me = "foo:";
> }
> me += "fred; ";

which can changed to initializers:

    UT_String me(x ? "bar" : "foo");

or in more complex cases:

    ut_String me(get_char_ptr_depending_on_x(x));

or inlined:

    const char* psz;

    if (...)
    ...
    // lots of lines setting psz to something depending on another thing or
two

    UT_String me(psz);

Please note that this is the way (in fact the only way) to initialize
references, e.g. members like (module style and some keywords):

class bar_t;
extern bar_t& obj1;
extern bar_t& obj2;

class foo {
    foo(bool b) : m_bar(b ? obj1 : obj2) {}
    bar_t& m_bar;
}

It's always better to use references than pointers when possible.

/Mike



This archive was generated by hypermail 2b25 : Fri Jun 22 2001 - 10:08:16 CDT