Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Clang warning

44 views
Skip to first unread message

woodb...@gmail.com

unread,
May 24, 2015, 11:28:52 AM5/24/15
to

I started using a version of PC-BSD that uses clang 3.6
and am getting a warning about this code:

template <unsigned long N = udp_packet_max>
class SendBufferStack : public SendBuffer
{
::std::array<char, N> ar;

public:
SendBufferStack () : SendBuffer(&ar[0], N) {}
};

The warning is that field ar is uninitialized when used.
I think that taking the address of the field should be
OK. Gcc 4.9.2 and other compilers don't warn about the
code. Is this a problem or is Clang off track? Thanks
in advance.

Brian
Ebenezer Enterprises - "Free at last; free at last; thank G-d
Almighty we are free at last." Martin Luther King Jr.

http://webEbenezer.net

Mr Flibble

unread,
May 24, 2015, 12:32:32 PM5/24/15
to
On 24/05/2015 16:28, woodb...@gmail.com wrote:
>
> I started using a version of PC-BSD that uses clang 3.6
> and am getting a warning about this code:
>
> template <unsigned long N = udp_packet_max>
> class SendBufferStack : public SendBuffer
> {
> ::std::array<char, N> ar;
>
> public:
> SendBufferStack () : SendBuffer(&ar[0], N) {}
> };
>
> The warning is that field ar is uninitialized when used.
> I think that taking the address of the field should be
> OK. Gcc 4.9.2 and other compilers don't warn about the
> code. Is this a problem or is Clang off track? Thanks
> in advance.

The warning is quite valid: you are indeed using an uninitialised
variable; formally it might even be undefined behaviour as you are
calling a non-static member function of an object that is yet to be created.

Why do you insist on writing ::std::foo Brian? The correct form is std::foo.

/Flibble

woodb...@gmail.com

unread,
May 24, 2015, 1:36:51 PM5/24/15
to
On Sunday, May 24, 2015 at 11:32:32 AM UTC-5, Mr Flibble wrote:
> On 24/05/2015 16:28, woodb...@gmail.com wrote:
> >
> > I started using a version of PC-BSD that uses clang 3.6
> > and am getting a warning about this code:
> >
> > template <unsigned long N = udp_packet_max>
> > class SendBufferStack : public SendBuffer
> > {
> > ::std::array<char, N> ar;
> >
> > public:
> > SendBufferStack () : SendBuffer(&ar[0], N) {}
> > };
> >
> > The warning is that field ar is uninitialized when used.
> > I think that taking the address of the field should be
> > OK. Gcc 4.9.2 and other compilers don't warn about the
> > code. Is this a problem or is Clang off track? Thanks
> > in advance.
>
> The warning is quite valid: you are indeed using an uninitialised

Is taking/storing the address of an uninitialized variable wrong?


> variable; formally it might even be undefined behaviour as you are
> calling a non-static member function of an object that is yet to be created.
>
> Why do you insist on writing ::std::foo Brian? The correct form is std::foo.

Why do you ask?

Brian
Ebenezer Enterprises - Heavenly code.
http://webEbenezer.net

Mr Flibble

unread,
May 24, 2015, 3:42:15 PM5/24/15
to
On 24/05/2015 18:36, woodb...@gmail.com wrote:
> On Sunday, May 24, 2015 at 11:32:32 AM UTC-5, Mr Flibble wrote:
>> On 24/05/2015 16:28, woodb...@gmail.com wrote:
>>>
>>> I started using a version of PC-BSD that uses clang 3.6
>>> and am getting a warning about this code:
>>>
>>> template <unsigned long N = udp_packet_max>
>>> class SendBufferStack : public SendBuffer
>>> {
>>> ::std::array<char, N> ar;
>>>
>>> public:
>>> SendBufferStack () : SendBuffer(&ar[0], N) {}
>>> };
>>>
>>> The warning is that field ar is uninitialized when used.
>>> I think that taking the address of the field should be
>>> OK. Gcc 4.9.2 and other compilers don't warn about the
>>> code. Is this a problem or is Clang off track? Thanks
>>> in advance.
>>
>> The warning is quite valid: you are indeed using an uninitialised
>
> Is taking/storing the address of an uninitialized variable wrong?

No it isn't but calling a non-static member function, namely
operator[](), on an object that doesn't yet exist is wrong.

/Flibble

Chris Vine

unread,
May 25, 2015, 1:31:08 PM5/25/15
to
On Sun, 24 May 2015 10:36:41 -0700 (PDT)
woodb...@gmail.com wrote:
> On Sunday, May 24, 2015 at 11:32:32 AM UTC-5, Mr Flibble wrote:
> > On 24/05/2015 16:28, woodb...@gmail.com wrote:
> > >
> > > I started using a version of PC-BSD that uses clang 3.6
> > > and am getting a warning about this code:
> > >
> > > template <unsigned long N = udp_packet_max>
> > > class SendBufferStack : public SendBuffer
> > > {
> > > ::std::array<char, N> ar;
> > >
> > > public:
> > > SendBufferStack () : SendBuffer(&ar[0], N) {}
> > > };
> > >
> > > The warning is that field ar is uninitialized when used.
> > > I think that taking the address of the field should be
> > > OK. Gcc 4.9.2 and other compilers don't warn about the
> > > code. Is this a problem or is Clang off track? Thanks
> > > in advance.
> >
> > The warning is quite valid: you are indeed using an uninitialised
>
> Is taking/storing the address of an uninitialized variable wrong?

It is not just that the array is uninitialized, but that it doesn't
exist at all, and you cannot take the address of a non-existent
object. It doesn't exist because the constructor of SendBuffer is
called before any of the members of its sub-class (SendBufferStack) are
constructed. You could lawfully do an assignment (not initialization)
in the body of SendBufferStack's constructor if SendBuffer's member
being assigned to is protected or has a protected accessor, but more
likely you want to consider templating the base class or using a
virtual function.

Of course, as Flibble has pointed out, you are not just taking the
array's address, but you are applying operator[] to the non-existent
array in order to take the address of its first (but also non-existent)
member. So you have doubly undefined behaviour.

Possibly your unnecessary use of the initial scoping operator in
declaring 'ar' is adding to the cognitive load when your read your code.
If you write your code more conventionally without such superfluous
nonsense you would find it easier to see errors like this.

Chris


woodb...@gmail.com

unread,
May 25, 2015, 2:20:19 PM5/25/15
to
If I change it like this:

template <unsigned long N = udp_packet_max>
class SendBufferStack : public SendBuffer
{
char ar[N];

public:
SendBufferStack () : SendBuffer(ar, N) {}
};

Clang and Gcc are fine with it.


Brian
Ebenezer Enterprises
http://webEbenezer.net

Chris Vine

unread,
May 25, 2015, 2:37:00 PM5/25/15
to
On Mon, 25 May 2015 11:20:10 -0700 (PDT)
woodb...@gmail.com wrote:
[snip]
> If I change it like this:
>
> template <unsigned long N = udp_packet_max>
> class SendBufferStack : public SendBuffer
> {
> char ar[N];
>
> public:
> SendBufferStack () : SendBuffer(ar, N) {}
> };
>
> Clang and Gcc are fine with it.

If they still refuse to listen, tell it to the church; and if they
refuse to listen even to the church, treat them as you would a pagan or
a tax collector: [Matthew 18:17]

Chris

Chris Vine

unread,
May 25, 2015, 2:48:10 PM5/25/15
to
It now occurs to me that you may not just be being deliberately
awkward for the fun of it, but that you don't understand the basics.
Referring to 'ar' takes its address by pointer decay. So whether you
are to be treated as a pagan or a tax collector I leave to you.


0 new messages