octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: rc/ov.h:166: warning: 'octave_value::rep' should be initialized in t


From: Jordi Gutiérrez Hermoso
Subject: Re: rc/ov.h:166: warning: 'octave_value::rep' should be initialized in the member initialization list
Date: Fri, 14 Jan 2011 13:04:50 -0600

On 14 January 2011 12:25, CdeMills <address@hidden> wrote:
>
> Could someone please enlighten my weak comprehension of C++ with regard to
> ov.h ?
>
> Line 166:
> octave_value (void)
>    {
>      static octave_base_value nil_rep;
>      rep = &nil_rep;
>      rep->count++;
>    }

"static" is one of the most overused keywords in C++...

In this case, you are declaring a static variable inside a function.
It's approximately equivalent to "persistent" in m-scripts. The
warning you are seeing is that gcc wants something like this:

     octave_value (void)
        : rep(&nil_rep)
        {
          rep->count++;
        }

but then that nil_rep itself should be initialised somewhere.

I think what makes most sense is to forego nil_rep and replace rep
with a static member variable in octave_value class. Then you have to
be careful abou static initialisation, you have to make sure the
hypothetical

     static octave_base_value rep;

has static initialisation and only in one translation unit (e.g. don't
put the static initialisation inside any header).

This is reference counting, but I don't see anything directly about
smart pointers, at least not in the snippets you posted.

>      ~octave_value (void)
>      {
>        if (--rep->count == 0)
>          delete rep;
>      }

This here is calling the dtor on the static function-scope variable,
through the rep pointer... but this shouldn't work, because every
delete should correspond to a new, and there was no new in the ctor.
If this works and I'm correct that there was no corresponding new,
it's sheer coincidence this hasn't given us UB everywhere.

Does this explain it?

- Jordi G.H.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]