Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #41195 > unrolled thread

asserts considered harmful

Started byMr Flibble <flibbleREMOVETHISBIT@i42.co.uk>
First post2016-02-23 22:19 +0000
Last post2016-02-24 06:22 -0800
Articles 18 on this page of 38 — 11 participants

Back to article view | Back to comp.lang.c++


Contents

  asserts considered harmful Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2016-02-23 22:19 +0000
    Re: asserts considered harmful Ian Collins <ian-news@hotmail.com> - 2016-02-24 11:27 +1300
      Re: asserts considered harmful Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2016-02-23 22:32 +0000
        Re: asserts considered harmful Ian Collins <ian-news@hotmail.com> - 2016-02-24 11:44 +1300
      Re: asserts considered harmful Cholo Lennon <chololennon@hotmail.com> - 2016-02-24 16:49 -0300
    Re: asserts considered harmful "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2016-02-24 00:00 +0100
      Re: asserts considered harmful Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2016-02-23 23:20 +0000
        Re: asserts considered harmful "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2016-02-24 01:02 +0100
          Re: asserts considered harmful Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2016-02-24 00:17 +0000
            Re: asserts considered harmful Ian Collins <ian-news@hotmail.com> - 2016-02-24 13:25 +1300
        Re: asserts considered harmful Vir Campestris <vir.campestris@invalid.invalid> - 2016-02-24 21:27 +0000
          Re: asserts considered harmful Ian Collins <ian-news@hotmail.com> - 2016-02-25 10:59 +1300
          Re: asserts considered harmful Öö Tiib <ootiib@hot.ee> - 2016-02-24 14:13 -0800
            Re: asserts considered harmful scott@slp53.sl.home (Scott Lurndal) - 2016-02-25 00:12 +0000
              Re: asserts considered harmful David Brown <david.brown@hesbynett.no> - 2016-02-25 01:23 +0100
                Re: asserts considered harmful Ian Collins <ian-news@hotmail.com> - 2016-02-25 15:29 +1300
                  Re: asserts considered harmful Vir Campestris <vir.campestris@invalid.invalid> - 2016-02-25 23:05 +0000
                    Re: asserts considered harmful Ian Collins <ian-news@hotmail.com> - 2016-02-26 19:41 +1300
                    Re: asserts considered harmful David Brown <david.brown@hesbynett.no> - 2016-02-26 17:11 +0100
              Re: asserts considered harmful Öö Tiib <ootiib@hot.ee> - 2016-02-24 17:32 -0800
      Re: asserts considered harmful Ian Collins <ian-news@hotmail.com> - 2016-02-24 13:00 +1300
        Re: asserts considered harmful Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2016-02-24 00:24 +0000
          Re: asserts considered harmful Ian Collins <ian-news@hotmail.com> - 2016-02-24 13:34 +1300
            Re: asserts considered harmful Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2016-02-24 01:21 +0000
              Re: asserts considered harmful Ian Collins <ian-news@hotmail.com> - 2016-02-24 14:55 +1300
              Re: asserts considered harmful Öö Tiib <ootiib@hot.ee> - 2016-02-23 18:47 -0800
                Re: asserts considered harmful Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2016-02-24 17:52 +0000
                  Re: asserts considered harmful Öö Tiib <ootiib@hot.ee> - 2016-02-24 11:14 -0800
                    Re: asserts considered harmful Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2016-02-24 19:42 +0000
                      Re: asserts considered harmful Öö Tiib <ootiib@hot.ee> - 2016-02-24 12:58 -0800
                        Re: asserts considered harmful Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2016-02-24 21:49 +0000
                          Re: asserts considered harmful Öö Tiib <ootiib@hot.ee> - 2016-02-24 14:03 -0800
            Re: asserts considered harmful Paavo Helde <myfirstname@osa.pri.ee> - 2016-02-24 11:48 +0200
              Re: asserts considered harmful Ian Collins <ian-news@hotmail.com> - 2016-02-24 23:14 +1300
              Re: asserts considered harmful Öö Tiib <ootiib@hot.ee> - 2016-02-24 05:52 -0800
            Re: asserts considered harmful woodbrian77@gmail.com - 2016-03-09 11:32 -0800
        Re: asserts considered harmful mark <mark@invalid.invalid> - 2016-02-24 14:43 +0100
          Re: asserts considered harmful Öö Tiib <ootiib@hot.ee> - 2016-02-24 06:22 -0800

Page 2 of 2 — ← Prev page 1 [2]


#41203

FromIan Collins <ian-news@hotmail.com>
Date2016-02-24 13:00 +1300
Message-ID<dj4a4tFj7j3U4@mid.individual.net>
In reply to#41201
Alf P. Steinbach wrote:
> On 23.02.2016 23:19, Mr Flibble wrote:
>> Code containing asserts will behave differently depending on whether or
>> not the macro NDEBUG is defined.  This is dangerous as it may result in
>> undetected bugs in a release build manifesting as undefined behaviour
>> that doesn't cause a straight crash but does something more serious.
>
> Well, you can define a wrapper that throws an exception in NDEBUG
> (a.k.a. “release”) mode, then preferably a “hard exception” that has
> maximum chances of propagating all the way up to `main`.
>
> The main problem with that, as I see it, is that cleanup via stack
> unwinding can be dangerous when an assertion has failed.

Not only that, but unless you can capture enough context, it offers 
little help in diagnosing the cause.  I much prefer to have a decent 
core dump to analyse than an exception report.  I guess things are 
different on platforms that lack support for cores, but even in embedded 
systems, you are often better off rebooting than trying to handle an 
unexpected condition.

The discussion should be around where to use assert and where to throw 
an exception, forget about NDEBUG.  I'm guessing the former is less 
frequent in most code, but when it is used, the options for recovery are 
sparse and the code to handle it nonexistent.  So swapping an assert for 
an exception in production builds may be even more dangerous.

My advice is to use asserts sparingly and keep them in!

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#41206

FromMr Flibble <flibbleREMOVETHISBIT@i42.co.uk>
Date2016-02-24 00:24 +0000
Message-ID<C7adnQL08_S_ZVHLnZ2dnUU7-WmdnZ2d@giganews.com>
In reply to#41203
On 24/02/2016 00:00, Ian Collins wrote:
> Alf P. Steinbach wrote:
>> On 23.02.2016 23:19, Mr Flibble wrote:
>>> Code containing asserts will behave differently depending on whether or
>>> not the macro NDEBUG is defined.  This is dangerous as it may result in
>>> undetected bugs in a release build manifesting as undefined behaviour
>>> that doesn't cause a straight crash but does something more serious.
>>
>> Well, you can define a wrapper that throws an exception in NDEBUG
>> (a.k.a. “release”) mode, then preferably a “hard exception” that has
>> maximum chances of propagating all the way up to `main`.
>>
>> The main problem with that, as I see it, is that cleanup via stack
>> unwinding can be dangerous when an assertion has failed.
>
> Not only that, but unless you can capture enough context, it offers
> little help in diagnosing the cause.  I much prefer to have a decent
> core dump to analyse than an exception report.  I guess things are
> different on platforms that lack support for cores, but even in embedded
> systems, you are often better off rebooting than trying to handle an
> unexpected condition.

If stack unwinding can be dangerous in the face of a detected logic 
error (and I agree it is technically possible) then why can 
std::vector::at() throw an exception in the first place? In fact why 
does std::logic_error exist at all?

Following your logic to its ultimate conclusion there should exist a 
proposal to deprecate/remove std::logic_error and exceptions derived 
from it from C++.

/Flibble


[toc] | [prev] | [next] | [standalone]


#41208

FromIan Collins <ian-news@hotmail.com>
Date2016-02-24 13:34 +1300
Message-ID<dj4c3sFj7j3U6@mid.individual.net>
In reply to#41206
Mr Flibble wrote:
> On 24/02/2016 00:00, Ian Collins wrote:
>> Alf P. Steinbach wrote:
>>> On 23.02.2016 23:19, Mr Flibble wrote:
>>>> Code containing asserts will behave differently depending on whether or
>>>> not the macro NDEBUG is defined.  This is dangerous as it may result in
>>>> undetected bugs in a release build manifesting as undefined behaviour
>>>> that doesn't cause a straight crash but does something more serious.
>>>
>>> Well, you can define a wrapper that throws an exception in NDEBUG
>>> (a.k.a. “release”) mode, then preferably a “hard exception” that has
>>> maximum chances of propagating all the way up to `main`.
>>>
>>> The main problem with that, as I see it, is that cleanup via stack
>>> unwinding can be dangerous when an assertion has failed.
>>
>> Not only that, but unless you can capture enough context, it offers
>> little help in diagnosing the cause.  I much prefer to have a decent
>> core dump to analyse than an exception report.  I guess things are
>> different on platforms that lack support for cores, but even in embedded
>> systems, you are often better off rebooting than trying to handle an
>> unexpected condition.
>
> If stack unwinding can be dangerous in the face of a detected logic
> error (and I agree it is technically possible) then why can
> std::vector::at() throw an exception in the first place? In fact why
> does std::logic_error exist at all?

Because std::vector is designed to do so?

If for an example an invalid pointer triggers an assert, I would want to 
know how this condition occurred, an assert and a core would give me the 
best chance of doing so.  Even if the alternative exception captured the 
stack, it wouldn't help if the bad data came from another thread.

Use exceptions where recovery is an option, asserts where it isn't.

> Following your logic to its ultimate conclusion there should exist a
> proposal to deprecate/remove std::logic_error and exceptions derived
> from it from C++.

I think you lost the way somewhere.  std::logic_error has its place, but 
that place isn't as a substitute for assert.

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#41209

FromMr Flibble <flibbleREMOVETHISBIT@i42.co.uk>
Date2016-02-24 01:21 +0000
Message-ID<Xc2dnYdaP8A-mFDLnZ2dnUU7-QudnZ2d@giganews.com>
In reply to#41208
On 24/02/2016 00:34, Ian Collins wrote:
> Mr Flibble wrote:
>> On 24/02/2016 00:00, Ian Collins wrote:
>>> Alf P. Steinbach wrote:
>>>> On 23.02.2016 23:19, Mr Flibble wrote:
>>>>> Code containing asserts will behave differently depending on
>>>>> whether or
>>>>> not the macro NDEBUG is defined.  This is dangerous as it may
>>>>> result in
>>>>> undetected bugs in a release build manifesting as undefined behaviour
>>>>> that doesn't cause a straight crash but does something more serious.
>>>>
>>>> Well, you can define a wrapper that throws an exception in NDEBUG
>>>> (a.k.a. “release”) mode, then preferably a “hard exception” that has
>>>> maximum chances of propagating all the way up to `main`.
>>>>
>>>> The main problem with that, as I see it, is that cleanup via stack
>>>> unwinding can be dangerous when an assertion has failed.
>>>
>>> Not only that, but unless you can capture enough context, it offers
>>> little help in diagnosing the cause.  I much prefer to have a decent
>>> core dump to analyse than an exception report.  I guess things are
>>> different on platforms that lack support for cores, but even in embedded
>>> systems, you are often better off rebooting than trying to handle an
>>> unexpected condition.
>>
>> If stack unwinding can be dangerous in the face of a detected logic
>> error (and I agree it is technically possible) then why can
>> std::vector::at() throw an exception in the first place? In fact why
>> does std::logic_error exist at all?
>
> Because std::vector is designed to do so?
>
> If for an example an invalid pointer triggers an assert, I would want to
> know how this condition occurred, an assert and a core would give me the
> best chance of doing so.  Even if the alternative exception captured the
> stack, it wouldn't help if the bad data came from another thread.
>
> Use exceptions where recovery is an option, asserts where it isn't.

You are missing the point.  If an incorrect index is being passed to 
at() then it is likely that something is seriously amiss.

>
>> Following your logic to its ultimate conclusion there should exist a
>> proposal to deprecate/remove std::logic_error and exceptions derived
>> from it from C++.
>
> I think you lost the way somewhere.  std::logic_error has its place, but
> that place isn't as a substitute for assert.

Yes it is.

/Flibble

[toc] | [prev] | [next] | [standalone]


#41212

FromIan Collins <ian-news@hotmail.com>
Date2016-02-24 14:55 +1300
Message-ID<dj4gsaFj7j3U8@mid.individual.net>
In reply to#41209
Mr Flibble wrote:
> On 24/02/2016 00:34, Ian Collins wrote:
>> Mr Flibble wrote:

>> If for an example an invalid pointer triggers an assert, I would want to
>> know how this condition occurred, an assert and a core would give me the
>> best chance of doing so.  Even if the alternative exception captured the
>> stack, it wouldn't help if the bad data came from another thread.
>>
>> Use exceptions where recovery is an option, asserts where it isn't.
>
> You are missing the point.  If an incorrect index is being passed to
> at() then it is likely that something is seriously amiss.

But it shouldn't be a "this can never happen and if it does, we're 
screwed" condition where an assert is appropriate.

>>> Following your logic to its ultimate conclusion there should exist a
>>> proposal to deprecate/remove std::logic_error and exceptions derived
>>> from it from C++.
>>
>> I think you lost the way somewhere.  std::logic_error has its place, but
>> that place isn't as a substitute for assert.
>
> Yes it is.

Only if you control 100% of the code base and aren't too interested in 
fault finding the case of the throw.

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#41213

FromÖö Tiib <ootiib@hot.ee>
Date2016-02-23 18:47 -0800
Message-ID<cb8008f9-18bb-4433-8830-7926edaa8334@googlegroups.com>
In reply to#41209
On Wednesday, 24 February 2016 03:21:56 UTC+2, Mr Flibble  wrote:
> On 24/02/2016 00:34, Ian Collins wrote:
> > Mr Flibble wrote:
> >> On 24/02/2016 00:00, Ian Collins wrote:
> >>> Alf P. Steinbach wrote:
> >>>> On 23.02.2016 23:19, Mr Flibble wrote:
> >>>>> Code containing asserts will behave differently depending on
> >>>>> whether or
> >>>>> not the macro NDEBUG is defined.  This is dangerous as it may
> >>>>> result in
> >>>>> undetected bugs in a release build manifesting as undefined behaviour
> >>>>> that doesn't cause a straight crash but does something more serious.
> >>>>
> >>>> Well, you can define a wrapper that throws an exception in NDEBUG
> >>>> (a.k.a. "release") mode, then preferably a "hard exception" that has
> >>>> maximum chances of propagating all the way up to `main`.
> >>>>
> >>>> The main problem with that, as I see it, is that cleanup via stack
> >>>> unwinding can be dangerous when an assertion has failed.
> >>>
> >>> Not only that, but unless you can capture enough context, it offers
> >>> little help in diagnosing the cause.  I much prefer to have a decent
> >>> core dump to analyse than an exception report.  I guess things are
> >>> different on platforms that lack support for cores, but even in embedded
> >>> systems, you are often better off rebooting than trying to handle an
> >>> unexpected condition.
> >>
> >> If stack unwinding can be dangerous in the face of a detected logic
> >> error (and I agree it is technically possible) then why can
> >> std::vector::at() throw an exception in the first place? In fact why
> >> does std::logic_error exist at all?
> >
> > Because std::vector is designed to do so?
> >
> > If for an example an invalid pointer triggers an assert, I would want to
> > know how this condition occurred, an assert and a core would give me the
> > best chance of doing so.  Even if the alternative exception captured the
> > stack, it wouldn't help if the bad data came from another thread.
> >
> > Use exceptions where recovery is an option, asserts where it isn't.
> 
> You are missing the point.  If an incorrect index is being passed to 
> at() then it is likely that something is seriously amiss.

That 'vector::at()' is likely for cases when out of range index can be
result of valid processing for example it was read from some potentially
dirty stream. Otherwise it is better to 'assert' and to use [].

> 
> >
> >> Following your logic to its ultimate conclusion there should exist a
> >> proposal to deprecate/remove std::logic_error and exceptions derived
> >> from it from C++.
> >
> > I think you lost the way somewhere.  std::logic_error has its place, but
> > that place isn't as a substitute for assert.
> 
> Yes it is.

It is matter of contract of function interface. In general if caller is
supposed to avoid error situation then abort, if caller is not supposed to
avoid it (and especially when it can not avoid it) then throw.  

Bugs must be fixed, but thrown error may lose context. It is cheaper to
analyse defect by post-mortem debugging of crash dump and throwing on 
bugs just risks losing valuable context.

[toc] | [prev] | [next] | [standalone]


#41224

FromMr Flibble <flibbleREMOVETHISBIT@i42.co.uk>
Date2016-02-24 17:52 +0000
Message-ID<w9qdnZ1gCctbcFDLnZ2dnUU7-UWdnZ2d@giganews.com>
In reply to#41213
On 24/02/2016 02:47, Öö Tiib wrote:
> On Wednesday, 24 February 2016 03:21:56 UTC+2, Mr Flibble  wrote:
>> On 24/02/2016 00:34, Ian Collins wrote:
>>> Mr Flibble wrote:
>>>> On 24/02/2016 00:00, Ian Collins wrote:
>>>>> Alf P. Steinbach wrote:
>>>>>> On 23.02.2016 23:19, Mr Flibble wrote:
>>>>>>> Code containing asserts will behave differently depending on
>>>>>>> whether or
>>>>>>> not the macro NDEBUG is defined.  This is dangerous as it may
>>>>>>> result in
>>>>>>> undetected bugs in a release build manifesting as undefined behaviour
>>>>>>> that doesn't cause a straight crash but does something more serious.
>>>>>>
>>>>>> Well, you can define a wrapper that throws an exception in NDEBUG
>>>>>> (a.k.a. "release") mode, then preferably a "hard exception" that has
>>>>>> maximum chances of propagating all the way up to `main`.
>>>>>>
>>>>>> The main problem with that, as I see it, is that cleanup via stack
>>>>>> unwinding can be dangerous when an assertion has failed.
>>>>>
>>>>> Not only that, but unless you can capture enough context, it offers
>>>>> little help in diagnosing the cause.  I much prefer to have a decent
>>>>> core dump to analyse than an exception report.  I guess things are
>>>>> different on platforms that lack support for cores, but even in embedded
>>>>> systems, you are often better off rebooting than trying to handle an
>>>>> unexpected condition.
>>>>
>>>> If stack unwinding can be dangerous in the face of a detected logic
>>>> error (and I agree it is technically possible) then why can
>>>> std::vector::at() throw an exception in the first place? In fact why
>>>> does std::logic_error exist at all?
>>>
>>> Because std::vector is designed to do so?
>>>
>>> If for an example an invalid pointer triggers an assert, I would want to
>>> know how this condition occurred, an assert and a core would give me the
>>> best chance of doing so.  Even if the alternative exception captured the
>>> stack, it wouldn't help if the bad data came from another thread.
>>>
>>> Use exceptions where recovery is an option, asserts where it isn't.
>>
>> You are missing the point.  If an incorrect index is being passed to
>> at() then it is likely that something is seriously amiss.
>
> That 'vector::at()' is likely for cases when out of range index can be
> result of valid processing for example it was read from some potentially
> dirty stream. Otherwise it is better to 'assert' and to use [].

That would be an inappropriate use of vector::at().  Invalid user input 
is a runtime error not a logic error and passing an invalid index to 
vector::at() is a logic error evidenced by the fact that 
std::out_of_range is derived from std::logic_error.  The correct thing 
to do is validate your input first and then you can use 
vector::operator[]() instead of vector::at() which in my opinion is 
redundantly defensive as we should design our code in such away that 
logic errors are mitigated against.

/Flibble

[toc] | [prev] | [next] | [standalone]


#41226

FromÖö Tiib <ootiib@hot.ee>
Date2016-02-24 11:14 -0800
Message-ID<157eed2b-4d3e-498d-853e-f38a6213ecb6@googlegroups.com>
In reply to#41224
On Wednesday, 24 February 2016 19:52:18 UTC+2, Mr Flibble  wrote:
> On 24/02/2016 02:47, Öö Tiib wrote:
> > On Wednesday, 24 February 2016 03:21:56 UTC+2, Mr Flibble  wrote:
> >> On 24/02/2016 00:34, Ian Collins wrote:
> >>> Mr Flibble wrote:
> >>>> On 24/02/2016 00:00, Ian Collins wrote:
> >>>>> Alf P. Steinbach wrote:
> >>>>>> On 23.02.2016 23:19, Mr Flibble wrote:
> >>>>>>> Code containing asserts will behave differently depending on
> >>>>>>> whether or
> >>>>>>> not the macro NDEBUG is defined.  This is dangerous as it may
> >>>>>>> result in
> >>>>>>> undetected bugs in a release build manifesting as undefined behaviour
> >>>>>>> that doesn't cause a straight crash but does something more serious.
> >>>>>>
> >>>>>> Well, you can define a wrapper that throws an exception in NDEBUG
> >>>>>> (a.k.a. "release") mode, then preferably a "hard exception" that has
> >>>>>> maximum chances of propagating all the way up to `main`.
> >>>>>>
> >>>>>> The main problem with that, as I see it, is that cleanup via stack
> >>>>>> unwinding can be dangerous when an assertion has failed.
> >>>>>
> >>>>> Not only that, but unless you can capture enough context, it offers
> >>>>> little help in diagnosing the cause.  I much prefer to have a decent
> >>>>> core dump to analyse than an exception report.  I guess things are
> >>>>> different on platforms that lack support for cores, but even in embedded
> >>>>> systems, you are often better off rebooting than trying to handle an
> >>>>> unexpected condition.
> >>>>
> >>>> If stack unwinding can be dangerous in the face of a detected logic
> >>>> error (and I agree it is technically possible) then why can
> >>>> std::vector::at() throw an exception in the first place? In fact why
> >>>> does std::logic_error exist at all?
> >>>
> >>> Because std::vector is designed to do so?
> >>>
> >>> If for an example an invalid pointer triggers an assert, I would want to
> >>> know how this condition occurred, an assert and a core would give me the
> >>> best chance of doing so.  Even if the alternative exception captured the
> >>> stack, it wouldn't help if the bad data came from another thread.
> >>>
> >>> Use exceptions where recovery is an option, asserts where it isn't.
> >>
> >> You are missing the point.  If an incorrect index is being passed to
> >> at() then it is likely that something is seriously amiss.
> >
> > That 'vector::at()' is likely for cases when out of range index can be
> > result of valid processing for example it was read from some potentially
> > dirty stream. Otherwise it is better to 'assert' and to use [].
> 
> That would be an inappropriate use of vector::at().  Invalid user input 
> is a runtime error not a logic error and passing an invalid index to 
> vector::at() is a logic error evidenced by the fact that 
> std::out_of_range is derived from std::logic_error.  The correct thing 
> to do is validate your input first and then you can use 
> vector::operator[]() instead of vector::at() which in my opinion is 
> redundantly defensive as we should design our code in such away that 
> logic errors are mitigated against.

Who said that potentially dirty stream comes from human? World is full
of automated dirty stream sources. That 'at()' *is* validating input and
exception is perfectly fine since likely a whole transaction containing
invalid index has to be canceled and rejected; there's no need to
validate it any further. I agree that 'at()' of most containers is
rarely useful, instead assert and [] are common but it was you who 
brought it up.
 

[toc] | [prev] | [next] | [standalone]


#41227

FromMr Flibble <flibbleREMOVETHISBIT@i42.co.uk>
Date2016-02-24 19:42 +0000
Message-ID<pOydnVvDlfUUmlPLnZ2dnUU7-YHOydjZ@giganews.com>
In reply to#41226
On 24/02/2016 19:14, Öö Tiib wrote:
> On Wednesday, 24 February 2016 19:52:18 UTC+2, Mr Flibble  wrote:
>> On 24/02/2016 02:47, Öö Tiib wrote:
>>> On Wednesday, 24 February 2016 03:21:56 UTC+2, Mr Flibble  wrote:
>>>> On 24/02/2016 00:34, Ian Collins wrote:
>>>>> Mr Flibble wrote:
>>>>>> On 24/02/2016 00:00, Ian Collins wrote:
>>>>>>> Alf P. Steinbach wrote:
>>>>>>>> On 23.02.2016 23:19, Mr Flibble wrote:
>>>>>>>>> Code containing asserts will behave differently depending on
>>>>>>>>> whether or
>>>>>>>>> not the macro NDEBUG is defined.  This is dangerous as it may
>>>>>>>>> result in
>>>>>>>>> undetected bugs in a release build manifesting as undefined behaviour
>>>>>>>>> that doesn't cause a straight crash but does something more serious.
>>>>>>>>
>>>>>>>> Well, you can define a wrapper that throws an exception in NDEBUG
>>>>>>>> (a.k.a. "release") mode, then preferably a "hard exception" that has
>>>>>>>> maximum chances of propagating all the way up to `main`.
>>>>>>>>
>>>>>>>> The main problem with that, as I see it, is that cleanup via stack
>>>>>>>> unwinding can be dangerous when an assertion has failed.
>>>>>>>
>>>>>>> Not only that, but unless you can capture enough context, it offers
>>>>>>> little help in diagnosing the cause.  I much prefer to have a decent
>>>>>>> core dump to analyse than an exception report.  I guess things are
>>>>>>> different on platforms that lack support for cores, but even in embedded
>>>>>>> systems, you are often better off rebooting than trying to handle an
>>>>>>> unexpected condition.
>>>>>>
>>>>>> If stack unwinding can be dangerous in the face of a detected logic
>>>>>> error (and I agree it is technically possible) then why can
>>>>>> std::vector::at() throw an exception in the first place? In fact why
>>>>>> does std::logic_error exist at all?
>>>>>
>>>>> Because std::vector is designed to do so?
>>>>>
>>>>> If for an example an invalid pointer triggers an assert, I would want to
>>>>> know how this condition occurred, an assert and a core would give me the
>>>>> best chance of doing so.  Even if the alternative exception captured the
>>>>> stack, it wouldn't help if the bad data came from another thread.
>>>>>
>>>>> Use exceptions where recovery is an option, asserts where it isn't.
>>>>
>>>> You are missing the point.  If an incorrect index is being passed to
>>>> at() then it is likely that something is seriously amiss.
>>>
>>> That 'vector::at()' is likely for cases when out of range index can be
>>> result of valid processing for example it was read from some potentially
>>> dirty stream. Otherwise it is better to 'assert' and to use [].
>>
>> That would be an inappropriate use of vector::at().  Invalid user input
>> is a runtime error not a logic error and passing an invalid index to
>> vector::at() is a logic error evidenced by the fact that
>> std::out_of_range is derived from std::logic_error.  The correct thing
>> to do is validate your input first and then you can use
>> vector::operator[]() instead of vector::at() which in my opinion is
>> redundantly defensive as we should design our code in such away that
>> logic errors are mitigated against.
>
> Who said that potentially dirty stream comes from human? World is full
> of automated dirty stream sources. That 'at()' *is* validating input and
> exception is perfectly fine since likely a whole transaction containing
> invalid index has to be canceled and rejected; there's no need to
> validate it any further. I agree that 'at()' of most containers is
> rarely useful, instead assert and [] are common but it was you who
> brought it up.

Who said "user" was a human? It still makes no difference: you should 
still validate your input whatever its source and at() is not the place 
to do that validation as propagating a logic error exception is 
inappropriate for invalid runtime data.

/Flibble

[toc] | [prev] | [next] | [standalone]


#41229

FromÖö Tiib <ootiib@hot.ee>
Date2016-02-24 12:58 -0800
Message-ID<a2d8edcb-d8c9-4c4e-8c81-bdf6816fad0c@googlegroups.com>
In reply to#41227
On Wednesday, 24 February 2016 21:42:20 UTC+2, Mr Flibble  wrote:
> On 24/02/2016 19:14, Öö Tiib wrote:
> > On Wednesday, 24 February 2016 19:52:18 UTC+2, Mr Flibble  wrote:
> >> On 24/02/2016 02:47, Öö Tiib wrote:
> >>> On Wednesday, 24 February 2016 03:21:56 UTC+2, Mr Flibble  wrote:
> >>>> On 24/02/2016 00:34, Ian Collins wrote:
> >>>>> Mr Flibble wrote:
> >>>>>> On 24/02/2016 00:00, Ian Collins wrote:
> >>>>>>> Alf P. Steinbach wrote:
> >>>>>>>> On 23.02.2016 23:19, Mr Flibble wrote:
> >>>>>>>>> Code containing asserts will behave differently depending on
> >>>>>>>>> whether or
> >>>>>>>>> not the macro NDEBUG is defined.  This is dangerous as it may
> >>>>>>>>> result in
> >>>>>>>>> undetected bugs in a release build manifesting as undefined behaviour
> >>>>>>>>> that doesn't cause a straight crash but does something more serious.
> >>>>>>>>
> >>>>>>>> Well, you can define a wrapper that throws an exception in NDEBUG
> >>>>>>>> (a.k.a. "release") mode, then preferably a "hard exception" that has
> >>>>>>>> maximum chances of propagating all the way up to `main`.
> >>>>>>>>
> >>>>>>>> The main problem with that, as I see it, is that cleanup via stack
> >>>>>>>> unwinding can be dangerous when an assertion has failed.
> >>>>>>>
> >>>>>>> Not only that, but unless you can capture enough context, it offers
> >>>>>>> little help in diagnosing the cause.  I much prefer to have a decent
> >>>>>>> core dump to analyse than an exception report.  I guess things are
> >>>>>>> different on platforms that lack support for cores, but even in embedded
> >>>>>>> systems, you are often better off rebooting than trying to handle an
> >>>>>>> unexpected condition.
> >>>>>>
> >>>>>> If stack unwinding can be dangerous in the face of a detected logic
> >>>>>> error (and I agree it is technically possible) then why can
> >>>>>> std::vector::at() throw an exception in the first place? In fact why
> >>>>>> does std::logic_error exist at all?
> >>>>>
> >>>>> Because std::vector is designed to do so?
> >>>>>
> >>>>> If for an example an invalid pointer triggers an assert, I would want to
> >>>>> know how this condition occurred, an assert and a core would give me the
> >>>>> best chance of doing so.  Even if the alternative exception captured the
> >>>>> stack, it wouldn't help if the bad data came from another thread.
> >>>>>
> >>>>> Use exceptions where recovery is an option, asserts where it isn't.
> >>>>
> >>>> You are missing the point.  If an incorrect index is being passed to
> >>>> at() then it is likely that something is seriously amiss.
> >>>
> >>> That 'vector::at()' is likely for cases when out of range index can be
> >>> result of valid processing for example it was read from some potentially
> >>> dirty stream. Otherwise it is better to 'assert' and to use [].
> >>
> >> That would be an inappropriate use of vector::at().  Invalid user input
> >> is a runtime error not a logic error and passing an invalid index to
> >> vector::at() is a logic error evidenced by the fact that
> >> std::out_of_range is derived from std::logic_error.  The correct thing
> >> to do is validate your input first and then you can use
> >> vector::operator[]() instead of vector::at() which in my opinion is
> >> redundantly defensive as we should design our code in such away that
> >> logic errors are mitigated against.
> >
> > Who said that potentially dirty stream comes from human? World is full
> > of automated dirty stream sources. That 'at()' *is* validating input and
> > exception is perfectly fine since likely a whole transaction containing
> > invalid index has to be canceled and rejected; there's no need to
> > validate it any further. I agree that 'at()' of most containers is
> > rarely useful, instead assert and [] are common but it was you who
> > brought it up.
> 
> Who said "user" was a human? It still makes no difference: you should 
> still validate your input whatever its source and at() is not the place 
> to do that validation as propagating a logic error exception is 
> inappropriate for invalid runtime data.

Where it is written that I should use "logic error" for reporting 
defects in my code? I use unit tests, 'static_assert' and 'assert'
to check for defects in my code. Every of those is stop the show
and back to drawing board, it is not "exceptional situation" that
I made a defect. I make one every hour or so, it is part of work.

I do not know if the index was produced run time by that "user" or 
was carved into rock like that since stone age. It is index that does
not make sense, so it is violating logical preconditions and data
invariants and so is logic error. "Out of range" is clearest 
explanation of the error. 

As contrast writing code that checks and throws something else like
"range error" on my own instead of using 'at()' would be wrong. Wrong
exception, pointless duplication of already existing standard feature
and needless bloat.

[toc] | [prev] | [next] | [standalone]


#41231

FromMr Flibble <flibbleREMOVETHISBIT@i42.co.uk>
Date2016-02-24 21:49 +0000
Message-ID<k7WdnfXQTeXHuFPLnZ2dnUU7-YnOydjZ@giganews.com>
In reply to#41229
On 24/02/2016 20:58, Öö Tiib wrote:
> On Wednesday, 24 February 2016 21:42:20 UTC+2, Mr Flibble  wrote:
>> On 24/02/2016 19:14, Öö Tiib wrote:
>>> On Wednesday, 24 February 2016 19:52:18 UTC+2, Mr Flibble  wrote:
>>>> On 24/02/2016 02:47, Öö Tiib wrote:
>>>>> On Wednesday, 24 February 2016 03:21:56 UTC+2, Mr Flibble  wrote:
>>>>>> On 24/02/2016 00:34, Ian Collins wrote:
>>>>>>> Mr Flibble wrote:
>>>>>>>> On 24/02/2016 00:00, Ian Collins wrote:
>>>>>>>>> Alf P. Steinbach wrote:
>>>>>>>>>> On 23.02.2016 23:19, Mr Flibble wrote:
>>>>>>>>>>> Code containing asserts will behave differently depending on
>>>>>>>>>>> whether or
>>>>>>>>>>> not the macro NDEBUG is defined.  This is dangerous as it may
>>>>>>>>>>> result in
>>>>>>>>>>> undetected bugs in a release build manifesting as undefined behaviour
>>>>>>>>>>> that doesn't cause a straight crash but does something more serious.
>>>>>>>>>>
>>>>>>>>>> Well, you can define a wrapper that throws an exception in NDEBUG
>>>>>>>>>> (a.k.a. "release") mode, then preferably a "hard exception" that has
>>>>>>>>>> maximum chances of propagating all the way up to `main`.
>>>>>>>>>>
>>>>>>>>>> The main problem with that, as I see it, is that cleanup via stack
>>>>>>>>>> unwinding can be dangerous when an assertion has failed.
>>>>>>>>>
>>>>>>>>> Not only that, but unless you can capture enough context, it offers
>>>>>>>>> little help in diagnosing the cause.  I much prefer to have a decent
>>>>>>>>> core dump to analyse than an exception report.  I guess things are
>>>>>>>>> different on platforms that lack support for cores, but even in embedded
>>>>>>>>> systems, you are often better off rebooting than trying to handle an
>>>>>>>>> unexpected condition.
>>>>>>>>
>>>>>>>> If stack unwinding can be dangerous in the face of a detected logic
>>>>>>>> error (and I agree it is technically possible) then why can
>>>>>>>> std::vector::at() throw an exception in the first place? In fact why
>>>>>>>> does std::logic_error exist at all?
>>>>>>>
>>>>>>> Because std::vector is designed to do so?
>>>>>>>
>>>>>>> If for an example an invalid pointer triggers an assert, I would want to
>>>>>>> know how this condition occurred, an assert and a core would give me the
>>>>>>> best chance of doing so.  Even if the alternative exception captured the
>>>>>>> stack, it wouldn't help if the bad data came from another thread.
>>>>>>>
>>>>>>> Use exceptions where recovery is an option, asserts where it isn't.
>>>>>>
>>>>>> You are missing the point.  If an incorrect index is being passed to
>>>>>> at() then it is likely that something is seriously amiss.
>>>>>
>>>>> That 'vector::at()' is likely for cases when out of range index can be
>>>>> result of valid processing for example it was read from some potentially
>>>>> dirty stream. Otherwise it is better to 'assert' and to use [].
>>>>
>>>> That would be an inappropriate use of vector::at().  Invalid user input
>>>> is a runtime error not a logic error and passing an invalid index to
>>>> vector::at() is a logic error evidenced by the fact that
>>>> std::out_of_range is derived from std::logic_error.  The correct thing
>>>> to do is validate your input first and then you can use
>>>> vector::operator[]() instead of vector::at() which in my opinion is
>>>> redundantly defensive as we should design our code in such away that
>>>> logic errors are mitigated against.
>>>
>>> Who said that potentially dirty stream comes from human? World is full
>>> of automated dirty stream sources. That 'at()' *is* validating input and
>>> exception is perfectly fine since likely a whole transaction containing
>>> invalid index has to be canceled and rejected; there's no need to
>>> validate it any further. I agree that 'at()' of most containers is
>>> rarely useful, instead assert and [] are common but it was you who
>>> brought it up.
>>
>> Who said "user" was a human? It still makes no difference: you should
>> still validate your input whatever its source and at() is not the place
>> to do that validation as propagating a logic error exception is
>> inappropriate for invalid runtime data.
>
> Where it is written that I should use "logic error" for reporting
> defects in my code? I use unit tests, 'static_assert' and 'assert'
> to check for defects in my code. Every of those is stop the show
> and back to drawing board, it is not "exceptional situation" that
> I made a defect. I make one every hour or so, it is part of work.
>
> I do not know if the index was produced run time by that "user" or
> was carved into rock like that since stone age. It is index that does
> not make sense, so it is violating logical preconditions and data
> invariants and so is logic error. "Out of range" is clearest
> explanation of the error.

Why are you being so obtuse? If you let at() throw then you ARE using a 
logic error for something that is not a logic error. YOU ARE DOING IT WRONG.

>
> As contrast writing code that checks and throws something else like
> "range error" on my own instead of using 'at()' would be wrong. Wrong
> exception, pointless duplication of already existing standard feature
> and needless bloat.

Nobody is suggesting that .. STOP BEING SO OBTUSE.

/Flibble

[toc] | [prev] | [next] | [standalone]


#41233

FromÖö Tiib <ootiib@hot.ee>
Date2016-02-24 14:03 -0800
Message-ID<9caf5458-26a4-4e7b-914f-73a8ae5e10fa@googlegroups.com>
In reply to#41231
On Wednesday, 24 February 2016 23:49:28 UTC+2, Mr Flibble  wrote:
> On 24/02/2016 20:58, Öö Tiib wrote:
> > On Wednesday, 24 February 2016 21:42:20 UTC+2, Mr Flibble  wrote:
> >> On 24/02/2016 19:14, Öö Tiib wrote:
> >>> On Wednesday, 24 February 2016 19:52:18 UTC+2, Mr Flibble  wrote:
> >>>> On 24/02/2016 02:47, Öö Tiib wrote:
> >>>>> On Wednesday, 24 February 2016 03:21:56 UTC+2, Mr Flibble  wrote:
> >>>>>> On 24/02/2016 00:34, Ian Collins wrote:
> >>>>>>> Mr Flibble wrote:
> >>>>>>>> On 24/02/2016 00:00, Ian Collins wrote:
> >>>>>>>>> Alf P. Steinbach wrote:
> >>>>>>>>>> On 23.02.2016 23:19, Mr Flibble wrote:
> >>>>>>>>>>> Code containing asserts will behave differently depending on
> >>>>>>>>>>> whether or
> >>>>>>>>>>> not the macro NDEBUG is defined.  This is dangerous as it may
> >>>>>>>>>>> result in
> >>>>>>>>>>> undetected bugs in a release build manifesting as undefined behaviour
> >>>>>>>>>>> that doesn't cause a straight crash but does something more serious.
> >>>>>>>>>>
> >>>>>>>>>> Well, you can define a wrapper that throws an exception in NDEBUG
> >>>>>>>>>> (a.k.a. "release") mode, then preferably a "hard exception" that has
> >>>>>>>>>> maximum chances of propagating all the way up to `main`.
> >>>>>>>>>>
> >>>>>>>>>> The main problem with that, as I see it, is that cleanup via stack
> >>>>>>>>>> unwinding can be dangerous when an assertion has failed.
> >>>>>>>>>
> >>>>>>>>> Not only that, but unless you can capture enough context, it offers
> >>>>>>>>> little help in diagnosing the cause.  I much prefer to have a decent
> >>>>>>>>> core dump to analyse than an exception report.  I guess things are
> >>>>>>>>> different on platforms that lack support for cores, but even in embedded
> >>>>>>>>> systems, you are often better off rebooting than trying to handle an
> >>>>>>>>> unexpected condition.
> >>>>>>>>
> >>>>>>>> If stack unwinding can be dangerous in the face of a detected logic
> >>>>>>>> error (and I agree it is technically possible) then why can
> >>>>>>>> std::vector::at() throw an exception in the first place? In fact why
> >>>>>>>> does std::logic_error exist at all?
> >>>>>>>
> >>>>>>> Because std::vector is designed to do so?
> >>>>>>>
> >>>>>>> If for an example an invalid pointer triggers an assert, I would want to
> >>>>>>> know how this condition occurred, an assert and a core would give me the
> >>>>>>> best chance of doing so.  Even if the alternative exception captured the
> >>>>>>> stack, it wouldn't help if the bad data came from another thread.
> >>>>>>>
> >>>>>>> Use exceptions where recovery is an option, asserts where it isn't.
> >>>>>>
> >>>>>> You are missing the point.  If an incorrect index is being passed to
> >>>>>> at() then it is likely that something is seriously amiss.
> >>>>>
> >>>>> That 'vector::at()' is likely for cases when out of range index can be
> >>>>> result of valid processing for example it was read from some potentially
> >>>>> dirty stream. Otherwise it is better to 'assert' and to use [].
> >>>>
> >>>> That would be an inappropriate use of vector::at().  Invalid user input
> >>>> is a runtime error not a logic error and passing an invalid index to
> >>>> vector::at() is a logic error evidenced by the fact that
> >>>> std::out_of_range is derived from std::logic_error.  The correct thing
> >>>> to do is validate your input first and then you can use
> >>>> vector::operator[]() instead of vector::at() which in my opinion is
> >>>> redundantly defensive as we should design our code in such away that
> >>>> logic errors are mitigated against.
> >>>
> >>> Who said that potentially dirty stream comes from human? World is full
> >>> of automated dirty stream sources. That 'at()' *is* validating input and
> >>> exception is perfectly fine since likely a whole transaction containing
> >>> invalid index has to be canceled and rejected; there's no need to
> >>> validate it any further. I agree that 'at()' of most containers is
> >>> rarely useful, instead assert and [] are common but it was you who
> >>> brought it up.
> >>
> >> Who said "user" was a human? It still makes no difference: you should
> >> still validate your input whatever its source and at() is not the place
> >> to do that validation as propagating a logic error exception is
> >> inappropriate for invalid runtime data.
> >
> > Where it is written that I should use "logic error" for reporting
> > defects in my code? I use unit tests, 'static_assert' and 'assert'
> > to check for defects in my code. Every of those is stop the show
> > and back to drawing board, it is not "exceptional situation" that
> > I made a defect. I make one every hour or so, it is part of work.
> >
> > I do not know if the index was produced run time by that "user" or
> > was carved into rock like that since stone age. It is index that does
> > not make sense, so it is violating logical preconditions and data
> > invariants and so is logic error. "Out of range" is clearest
> > explanation of the error.
> 
> Why are you being so obtuse? If you let at() throw then you ARE using a 
> logic error for something that is not a logic error. YOU ARE DOING IT WRONG.

You are doing it wrong. You are asserting something baldly and then
discussing mine mental capabilities instead of discussing the points
that I made and the detail of it you consider incorrect.

> 
> >
> > As contrast writing code that checks and throws something else like
> > "range error" on my own instead of using 'at()' would be wrong. Wrong
> > exception, pointless duplication of already existing standard feature
> > and needless bloat.
> 
> Nobody is suggesting that .. STOP BEING SO OBTUSE.

You are mirroring. Everybody tells you that stop asserting that "sky is 
blue" crap baldly and obtusely. Instead elaborate what you are suggesting
and be constructive.

[toc] | [prev] | [next] | [standalone]


#41218

FromPaavo Helde <myfirstname@osa.pri.ee>
Date2016-02-24 11:48 +0200
Message-ID<c4SdndUwVpHs4VDLnZ2dnUU78bWdnZ2d@giganews.com>
In reply to#41208
On 24.02.2016 2:34, Ian Collins wrote:
>
> I think you lost the way somewhere.  std::logic_error has its place, but
> that place isn't as a substitute for assert.

I think it depends very much on the modularity of the component which 
has failed and needs to be restarted.

If the OS fails with a blue screen or kernel panic, then the execution 
flow goes back to the hardware and the computer is restarted either 
automatically or by the user.

If an app gets terminated via an assert, then the execution flow 
basically goes back to OS and user or another program can restart the app.

If a component in an app fails with an assert-style exception, the 
execution flow goes back to the main driver component of the app and the 
user or another routine can restart the component.

If the component state is corrupted and restarting it does not work, the 
next level (app) restart is needed.

If the computer state is corrupted and the app does not work after 
restart, the next level (computer) restart is needed (not a rare 
scenario in Windows).

The most tricky part is to find the correct level of restart needed at 
any given situation. Assert is not always the best choice, and neither 
is std::logic_error.

Just my 2 cents.




[toc] | [prev] | [next] | [standalone]


#41219

FromIan Collins <ian-news@hotmail.com>
Date2016-02-24 23:14 +1300
Message-ID<dj5e3dFj7j3U9@mid.individual.net>
In reply to#41218
Paavo Helde wrote:
> On 24.02.2016 2:34, Ian Collins wrote:
>>
>> I think you lost the way somewhere.  std::logic_error has its place, but
>> that place isn't as a substitute for assert.
>
> I think it depends very much on the modularity of the component which
> has failed and needs to be restarted.
>
> If the OS fails with a blue screen or kernel panic, then the execution
> flow goes back to the hardware and the computer is restarted either
> automatically or by the user.
>
> If an app gets terminated via an assert, then the execution flow
> basically goes back to OS and user or another program can restart the app.
>
> If a component in an app fails with an assert-style exception, the
> execution flow goes back to the main driver component of the app and the
> user or another routine can restart the component.
>
> If the component state is corrupted and restarting it does not work, the
> next level (app) restart is needed.
>
> If the computer state is corrupted and the app does not work after
> restart, the next level (computer) restart is needed (not a rare
> scenario in Windows).
>
> The most tricky part is to find the correct level of restart needed at
> any given situation. Assert is not always the best choice, and neither
> is std::logic_error.
>
> Just my 2 cents.

A fair summary.

I agree with the theory, but I've seldom seen component restarts 
implemented well in practice!  Most applications are monolithic, so 
there is nothing to restart.  Long running threaded applications are a 
good candidate for trapping errors with exceptions and restarting a 
thread while multi-process applications are a good candidate for 
asserts.  Most long running applications I've seen (such as system 
services) are happy to abort and rely on what ever started them to do 
the restart.  It is often safer and no doubt easier to code them this way.

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#41221

FromÖö Tiib <ootiib@hot.ee>
Date2016-02-24 05:52 -0800
Message-ID<f78733ee-08c3-42eb-b252-565ff9cbd3e1@googlegroups.com>
In reply to#41218
On Wednesday, 24 February 2016 11:48:43 UTC+2, Paavo Helde  wrote:
>
> If an app gets terminated via an assert, then the execution flow 
> basically goes back to OS and user or another program can restart the app.
> 
> If a component in an app fails with an assert-style exception, the 
> execution flow goes back to the main driver component of the app and the 
> user or another routine can restart the component.

There is a component (that throws exceptions about being buggy). The
memory of whole process is apparently shared (or how else it did throw 
exceptions) and so whatever corrupted component's state possibly 
corrupted something else alike. That makes restart of whole application
likely safer on case of "I'm insane" exception. 

If the component is implemented as separate process then there is
memory access barrier and so it can't throw exceptions anyway.

[toc] | [prev] | [next] | [standalone]


#41478

Fromwoodbrian77@gmail.com
Date2016-03-09 11:32 -0800
Message-ID<663088e9-9b30-48a6-b075-2c5802dc5b4e@googlegroups.com>
In reply to#41208
On Tuesday, February 23, 2016 at 6:34:16 PM UTC-6, Ian Collins wrote:
> Mr Flibble wrote:
> > On 24/02/2016 00:00, Ian Collins wrote:
> >> Alf P. Steinbach wrote:
> >>> On 23.02.2016 23:19, Mr Flibble wrote:
> >>>> Code containing asserts will behave differently depending on whether or
> >>>> not the macro NDEBUG is defined.  This is dangerous as it may result in
> >>>> undetected bugs in a release build manifesting as undefined behaviour
> >>>> that doesn't cause a straight crash but does something more serious.
> >>>
> >>> Well, you can define a wrapper that throws an exception in NDEBUG
> >>> (a.k.a. "release") mode, then preferably a "hard exception" that has
> >>> maximum chances of propagating all the way up to `main`.
> >>>
> >>> The main problem with that, as I see it, is that cleanup via stack
> >>> unwinding can be dangerous when an assertion has failed.
> >>
> >> Not only that, but unless you can capture enough context, it offers
> >> little help in diagnosing the cause.  I much prefer to have a decent
> >> core dump to analyse than an exception report.  I guess things are
> >> different on platforms that lack support for cores, but even in embedded
> >> systems, you are often better off rebooting than trying to handle an
> >> unexpected condition.
> >
> > If stack unwinding can be dangerous in the face of a detected logic
> > error (and I agree it is technically possible) then why can
> > std::vector::at() throw an exception in the first place? In fact why
> > does std::logic_error exist at all?
> 
> Because std::vector is designed to do so?
> 
> If for an example an invalid pointer triggers an assert, I would want to 
> know how this condition occurred, an assert and a core would give me the 
> best chance of doing so.  Even if the alternative exception captured the 
> stack, it wouldn't help if the bad data came from another thread.
> 
> Use exceptions where recovery is an option, asserts where it isn't.
> 
> > Following your logic to its ultimate conclusion there should exist a
> > proposal to deprecate/remove std::logic_error and exceptions derived
> > from it from C++.
> 
> I think you lost the way somewhere.  std::logic_error has its place, but 
> that place isn't as a substitute for assert.
> 

What place is left for std::logic_error?  I've never 
used it and only know of one guy that has:

http://stackoverflow.com/questions/2924058/confused-about-stdruntime-error-vs-stdlogic-error/6476858

He (Vladimir) said he was thinking about stopping using it
a few years ago.

Brian
Ebenezer Enterprises - In G-d we trust.
http://webEbenezer.net

[toc] | [prev] | [next] | [standalone]


#41220

Frommark <mark@invalid.invalid>
Date2016-02-24 14:43 +0100
Message-ID<nakbs4$8g4$1@dont-email.me>
In reply to#41203
On 2016-02-24 01:00, Ian Collins wrote:

> My advice is to use asserts sparingly and keep them in!

IMO, multiple levels of asserts should be used; release asserts that are 
always active and multiple debug assert versions that perform checks too 
expensive for release builds.

Debug asserts should be used extensively.

[toc] | [prev] | [next] | [standalone]


#41222

FromÖö Tiib <ootiib@hot.ee>
Date2016-02-24 06:22 -0800
Message-ID<f02948a4-b5bf-48fb-88c1-cb193d544e60@googlegroups.com>
In reply to#41220
On Wednesday, 24 February 2016 15:43:46 UTC+2, mark  wrote:
> On 2016-02-24 01:00, Ian Collins wrote:
> 
> > My advice is to use asserts sparingly and keep them in!
> 
> IMO, multiple levels of asserts should be used; release asserts that are 
> always active and multiple debug assert versions that perform checks too 
> expensive for release builds.

Assert typically just checks integrals or pointers or flags. That can be
done billions of times during second. When assert is seriously affecting
performance of optimized release build then it likely runs complicated
checks (that may be also defective and so should contain asserts) and 
the debug build is likely inhumanly torturous to test thanks to such.
Better reconsider such asserts.

> 
> Debug asserts should be used extensively.

About 5-10% of whole product code base is executed during 98% of profile. 
So it is likely better idea to use real asserts extensively and replace
those with debug asserts only in that 5-10% of code and only when 
profiling of product suggests to.

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

Back to top | Article view | comp.lang.c++


csiph-web