Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > is C++ worth it ?

Reply
Thread Tools

is C++ worth it ?

 
 
James Kanze
Guest
Posts: n/a
 
      07-30-2012
On Monday, July 30, 2012 8:46:40 PM UTC+1, Bo Persson wrote:
> Stuart skrev 2012-07-30 19:32:


> The language standard cannot require a stacktrace without first
> requiring that ALL implementations must use a stack. That would be
> limiting, and exclude some of the platforms that were available when the
> first C and C++ standards were written. ISO standards strive for a
> consensus, which is hard if some manufacturers are excluded.


In the sense that you use the word here, C++ does require a
stack. It requires that you can return from a function, and
refind the environment that you left. However the
implementation does this, it can provide a backtrace. (Since
throwing an exception requires stack unwinding, an
implementation is required to have code which can do this.)

--
James
 
Reply With Quote
 
 
 
 
Stuart
Guest
Posts: n/a
 
      07-31-2012
On July 30, 2012 Stuart wrote:
>> I thought gcc did not turn Access Violations into exceptions.
>> AFAIK, this is a unique feature of the MS compilers. However,
>> I would very much like to be proven wrong.


On July 31, 2012 James Kanze wrote:
> This error is unique to MS compilers I think (and I think it's
> optional there). With g++ under Linux (and all other Unix
> compilers, I think), and access violation results in a core
> dump, which is far preferrable for most applications.


>> And to come back to backtraces in exceptions: Why not
>> standardize it "optionally", so that if the platform provides
>> this feature, it can be used in a standard way?

>
> The reason C++ doesn't have backtraces in exceptions is that
> that's not what exceptions are for. Exceptions are designed to
> be caught, so any backtrace would be ignored. If you have a
> programming error, you want to stop immediately (in most
> applications, anyway). Violently, and with a backtrace. In the
> core file, or its equivalent---your client can't do anything
> with a backtrace, but he can send you a core dump, so that you
> can do something. (And of course, if the core dump isn't in
> your test cases, you're not testing sufficiently.)



I had the following scenario: Software for quality assurance, it
controls a measurement machine with cameras and stage. Measurements take
up to 8 hours, so the user should be able to abort measurements if he
sees that the measured object is not good enough.

I implemented the aborting of measurements by means of an exception, so
that the stack is neatly unwound and the cameras stop acquisition and
the stages will be powered off. With exceptions this is rather
comfortable, since I don't have to clutter my code with

Picture* picture = 0;
if (acquirePicture (&picture) == ERROR_CODE_SHOULD_STOP)
return ERROR_CODE_SHOULD_STOP;

but simply write

Picture* picture = acquirePicture ();

Now I have the problem that I am bound to the Windows platform
(drivers), so when I do something wrong and get an Access Violation,
I'll get an BAD_ACCESS exception. This is actually a good thing because
my application does not shut down without a pip, but the stack is
unwound and the measurement machine returns to a defined state (stages
stopped, camera in idle mode). But unfortunately I have no more
information about where the Access Violation occurred since C++
exceptions are designed to be used in a different way. Well, this works
well for my StopException since I don't care which particalur action has
been performed when the user aborted the measurement, but in case
something went wrong, I'd like to see what happened.

I ended up setting up my own stack that can be examined later on, but
this is rather clumsy and requires all my actions to push themselves on
this stack (not too bad) and pop when they have finished. The later is
quite cumbersome since every code path where the action stops without an
exception has to ensure that the action pops itself from the stack, or
else my stack gets messed up.

A perfect language would generate an exception with a stacktrace upon an
Access Violation, which is much more preferable to a Core Dump. For
those who actually want to have the core dump, the language should
generate such a dump when the exception is not caught (AFAIK, Win32 does
not offer a setting that generates dumps upon Access Violations).

Thanks in advance,
Stuart

PS: @Jorgen: Come on, you may not know every little bit of the language
(who does), but certainly you know a LOT more than the average C++
programmer. I tend to read most of your posts because changes are good
that I can learn something new.
@James: The same goes for you.
 
Reply With Quote
 
 
 
 
Nick Keighley
Guest
Posts: n/a
 
      07-31-2012
On Jul 30, 8:59*am, Stuart <(E-Mail Removed)> wrote:
> On 7/27/12 Lynn McGuire wrote:
>
> > Is C++ worth it ?

>
> Sadly, the answer to this question is yes.
>
> If you are working in the industry, there is hardly a way around C++.
> First of all, most drivers are provided with a C interface, which makes
> C++ a "natural" choice. Besides this, hardware independence is mostly a
> minor issue since the hardware asset of a large company won't be changed
> overnight to a different platform, and for GUI related stuff there are
> still platform independent libraries out there.
>
> Curiously, the answer won't be to use Java or C#, as they are supposed
> to be more high-level languages. Those are good languages that can be
> easily learned by the majority of programmers. Even after 12 years as
> C++ programming, and delving into some more obscure parts of template
> programming, not even I would consider myself an expert on C++ (I think
> Victor is one, and James).
>
> Probably the flat learning curve that is what makes Java and C# so
> popular. What the software industry needs is a language that can be used
> suffiently well by the average programmer. I know that this sounds
> contemptuous, but I know a lot of small companies that employ
> Quereinsteigers (didn't find a proper English word for it, there is
> probably none).


http://www.dw.de/dw/article/0,,6616141,00.html

learn a quirky German word each week...


> Those companies have to make do with the "human
> resources" they could get, so they want their programmers to struggle
> with the problem domain and not with the programming language.
>
> Setting the "human" factor aside, when we want to talk about programming
> languages we have to consider several points:
> (1) is the actual language better,
> (2) are the available compilers/debuggers/whatever tools better, and
> (3) are the available libraries better?
>
> I think that C++ beats Java in point (1), but is inferior with regard to
> point (3). Point (2) is maybe a draw. Since the decision for either Java
> or C++ must always be based on all three points, it'll always be quite
> difficult to decide. And most often it are some very minor points that
> let the programmer tend to one or the other language.
>
> For example, I start to tend to favour Java over C++ because whenever I
> make some grave mistake and get a Memory Access Violation, this is more
> or less the end of the world for a C++ application. Java, in contrast,
> nicely captures this and gives me an exception. This not only makes me
> able to shut down gracefully, but also provides me with enough error
> information about where I went wrong (there is no standard way under C++
> to retrieve the stacktrace of an exception). I don't understand why such
> a feature is not important enough to make it into the C++ standard,
> while such gimmicks like lambdas and whatnot get huge attention.
> Happily, I can now finish with the only *answer for the whole topic:
>
> Everyone can chose the programming language that meets his needs best.
>
> Regards,
> Stuart


 
Reply With Quote
 
Stuart
Guest
Posts: n/a
 
      07-31-2012
On Jul 30, Stuart wrote:
[snip]
>> I know that this sounds
>> contemptuous, but I know a lot of small companies that employ
>> Quereinsteigers (didn't find a proper English word for it, there is
>> probably none).



On 7/31/12 Nick Keighley wrote:
> http://www.dw.de/dw/article/0,,6616141,00.html
>
> learn a quirky German word each week...


Nice page. Sits now in my bookmarks bar. Thanks.
Stuart
 
Reply With Quote
 
Nobody
Guest
Posts: n/a
 
      07-31-2012
On Mon, 30 Jul 2012 21:26:52 +0200, Bo Persson wrote:

>> That surprises me when I recall that, even without exceptions, a benign-
>> looking plus operator or even an absence of a statement can emit dozens
>> of instructions, due to operator overloading and constructors/destructors.

>
> You mean unlike other languages, where an innocent looking function f(x)
> can call other functions 10 levels deep?


In other languages, there will at least be an explicit function call.

In C++, copy constructors, destructors, and conversion operators can all
insert "invisible" function calls.

 
Reply With Quote
 
BGB
Guest
Posts: n/a
 
      07-31-2012
On 7/31/2012 9:20 AM, Scott Lurndal wrote:
> James Kanze <(E-Mail Removed)> writes:
>> On Monday, July 30, 2012 7:59:44 PM UTC+1, Stuart wrote:
>>> On 7/30/12 Jorgen Grahn wrote:

>>
>>>> Wait a minute -- you say the MS compiler works as you want;
>>>> I say gcc does on Linux ... where *do* you see the problem?

>>
>>> I thought gcc did not turn Access Violations into exceptions.
>>> AFAIK, this is a unique feature of the MS compilers. However,
>>> I would very much like to be proven wrong.

>>
>> This error is unique to MS compilers I think (and I think it's
>> optional there). With g++ under Linux (and all other Unix
>> compilers, I think), and access violation results in a core
>> dump, which is far preferrable for most applications.\\

>
> technically, an access violation results in a signal, which if
> not caught by the application will terminate the application with
> a core file (if the core file resource limit allows, and if the
> stupid Fedora ABRT package isn't running).
>


yeah, and nifty stuff can be done in signal handlers, potentially
including, among other things, triggering an exception to the thrown and
the stack to be unwound. whether or not this is a good idea is a
separate issue, normal C++ exceptions may not necessarily work (a custom
mechanism may be needed), and have not yet determined how portable or
versatile this is.

though, granted, some of this could be determined by testing it.

 
Reply With Quote
 
Dombo
Guest
Posts: n/a
 
      07-31-2012
Op 31-Jul-12 9:26, Stuart schreef:
> On July 30, 2012 Stuart wrote:
>>> I thought gcc did not turn Access Violations into exceptions.
>>> AFAIK, this is a unique feature of the MS compilers. However,
>>> I would very much like to be proven wrong.

>
> On July 31, 2012 James Kanze wrote:
>> This error is unique to MS compilers I think (and I think it's
>> optional there). With g++ under Linux (and all other Unix
>> compilers, I think), and access violation results in a core
>> dump, which is far preferrable for most applications.

>
>>> And to come back to backtraces in exceptions: Why not
>>> standardize it "optionally", so that if the platform provides
>>> this feature, it can be used in a standard way?

>>
>> The reason C++ doesn't have backtraces in exceptions is that
>> that's not what exceptions are for. Exceptions are designed to
>> be caught, so any backtrace would be ignored. If you have a
>> programming error, you want to stop immediately (in most
>> applications, anyway). Violently, and with a backtrace. In the
>> core file, or its equivalent---your client can't do anything
>> with a backtrace, but he can send you a core dump, so that you
>> can do something. (And of course, if the core dump isn't in
>> your test cases, you're not testing sufficiently.)

>
>
> I had the following scenario: Software for quality assurance, it
> controls a measurement machine with cameras and stage. Measurements take
> up to 8 hours, so the user should be able to abort measurements if he
> sees that the measured object is not good enough.
>
> I implemented the aborting of measurements by means of an exception, so
> that the stack is neatly unwound and the cameras stop acquisition and
> the stages will be powered off. With exceptions this is rather
> comfortable, since I don't have to clutter my code with
>
> Picture* picture = 0;
> if (acquirePicture (&picture) == ERROR_CODE_SHOULD_STOP)
> return ERROR_CODE_SHOULD_STOP;
>
> but simply write
>
> Picture* picture = acquirePicture ();
>
> Now I have the problem that I am bound to the Windows platform
> (drivers), so when I do something wrong and get an Access Violation,
> I'll get an BAD_ACCESS exception. This is actually a good thing because
> my application does not shut down without a pip, but the stack is
> unwound and the measurement machine returns to a defined state (stages
> stopped, camera in idle mode). But unfortunately I have no more
> information about where the Access Violation occurred since C++
> exceptions are designed to be used in a different way. Well, this works
> well for my StopException since I don't care which particalur action has
> been performed when the user aborted the measurement, but in case
> something went wrong, I'd like to see what happened.
>
> I ended up setting up my own stack that can be examined later on, but
> this is rather clumsy and requires all my actions to push themselves on
> this stack (not too bad) and pop when they have finished. The later is
> quite cumbersome since every code path where the action stops without an
> exception has to ensure that the action pops itself from the stack, or
> else my stack gets messed up.


Maintaining your own stack isn't necessary on the Windows platform. On
Windows you have the option to capture the structured exception (is not
the same as a C++ exception) when things like an access violation
occurs. At the point the handler is invoked the stack is still there to
be examined. Windows also provides functions to walk the call stack.
Many years ago I created a library for one of my clients to pinpoint
where in the code their software had crashed (which in spite of
extensive testing typically happened at some important client at the
other side of the world with very few if any clues on how to reproduce
the problem). This library installed a handler for structured exceptions
and uncaught C++ exceptions, and logs the point in the code exactly
where the exception originated from and the call stack at that point. It
also triggered flushing the trace buffers, collecting some other
information diagnostic information and put all of this in a .zip file,
pop-up a dialog instructing how to proceed and terminate the
application. The nice thing about this approach is that the existing
code (about 2+ million lines) didn't need to be changed.

Of course this library relied heavily on Windows specific calls. However
requiring this as part of the C++ standard would reduce the number of
platforms on which C++ can be used.



 
Reply With Quote
 
88888 Dihedral
Guest
Posts: n/a
 
      07-31-2012
BGB於 2012年8月1日星期三UTC+8上午3時12分11秒 道:
> On 7/31/2012 9:20 AM, Scott Lurndal wrote:
>
> > James Kanze <(E-Mail Removed)> writes:

>
> >> On Monday, July 30, 2012 7:59:44 PM UTC+1, Stuart wrote:

>
> >>> On 7/30/12 Jorgen Grahn wrote:

>
> >>

>
> >>>> Wait a minute -- you say the MS compiler works as you want;

>
> >>>> I say gcc does on Linux ... where *do* you see the problem?

>
> >>

>
> >>> I thought gcc did not turn Access Violations into exceptions.

>
> >>> AFAIK, this is a unique feature of the MS compilers. However,

>
> >>> I would very much like to be proven wrong.

>
> >>

>
> >> This error is unique to MS compilers I think (and I think it's

>
> >> optional there). With g++ under Linux (and all other Unix

>
> >> compilers, I think), and access violation results in a core

>
> >> dump, which is far preferrable for most applications.\\

>
> >

>
> > technically, an access violation results in a signal, which if

>
> > not caught by the application will terminate the application with

>
> > a core file (if the core file resource limit allows, and if the

>
> > stupid Fedora ABRT package isn't running).

>
> >

>
>
>
> yeah, and nifty stuff can be done in signal handlers, potentially
>
> including, among other things, triggering an exception to the thrown and
>
> the stack to be unwound. whether or not this is a good idea is a
>
> separate issue, normal C++ exceptions may not necessarily work (a custom
>
> mechanism may be needed), and have not yet determined how portable or
>
> versatile this is.
>
>
>
> though, granted, some of this could be determined by testing it.


Well, I have checked the size of C++ compiler from time to time.

It is always getting fatter and fatter. I think a slim JVM with the linux
in the mobile phone systems is attracting more people to work on "new"
software on mobile phones every day.

 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      07-31-2012
On Tue, 2012-07-31, Bo Persson wrote:
> Nobody wrote 2012-07-31 15:59:
>> On Mon, 30 Jul 2012 21:26:52 +0200, Bo Persson wrote:
>>
>>>> That surprises me when I recall that, even without exceptions, a benign-
>>>> looking plus operator or even an absence of a statement can emit dozens
>>>> of instructions, due to operator overloading and constructors/destructors.
>>>
>>> You mean unlike other languages, where an innocent looking function f(x)
>>> can call other functions 10 levels deep?

>>
>> In other languages, there will at least be an explicit function call.
>>
>> In C++, copy constructors, destructors, and conversion operators can all
>> insert "invisible" function calls.

>
> So you mean that when you see matrix_add(x, y) you immediately see how
> much code that produces, while x + y is a mystery?


Perhaps he's just presenting this very common complaint from people
who dislike C++, and who's own favorite language doesn't have operator
overloading.

> I don't get it!


Me neither, but as you know people who don't use C++ have been
complaining about this for ~30 years.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
BGB
Guest
Posts: n/a
 
      07-31-2012
On 7/31/2012 3:25 PM, Scott Lurndal wrote:
> BGB <(E-Mail Removed)> writes:
>> On 7/31/2012 9:20 AM, Scott Lurndal wrote:
>>> James Kanze <(E-Mail Removed)> writes:
>>>> On Monday, July 30, 2012 7:59:44 PM UTC+1, Stuart wrote:
>>>>> On 7/30/12 Jorgen Grahn wrote:
>>>>
>>>>>> Wait a minute -- you say the MS compiler works as you want;
>>>>>> I say gcc does on Linux ... where *do* you see the problem?
>>>>
>>>>> I thought gcc did not turn Access Violations into exceptions.
>>>>> AFAIK, this is a unique feature of the MS compilers. However,
>>>>> I would very much like to be proven wrong.
>>>>
>>>> This error is unique to MS compilers I think (and I think it's
>>>> optional there). With g++ under Linux (and all other Unix
>>>> compilers, I think), and access violation results in a core
>>>> dump, which is far preferrable for most applications.\\
>>>
>>> technically, an access violation results in a signal, which if
>>> not caught by the application will terminate the application with
>>> a core file (if the core file resource limit allows, and if the
>>> stupid Fedora ABRT package isn't running).
>>>

>>
>> yeah, and nifty stuff can be done in signal handlers, potentially
>> including, among other things, triggering an exception to the thrown and
>> the stack to be unwound. whether or not this is a good idea is a
>> separate issue, normal C++ exceptions may not necessarily work (a custom
>> mechanism may be needed), and have not yet determined how portable or
>> versatile this is.

>
> I tend to use siglongjmp() to unwind when I catch asynchronous signals
> such as SIGTERM/SIGINT/SIGQUIT. When catching synchronous, such as SIGSEGV, there are
> no assurances that the rest of the program is still sane, and attempting
> to generate a C++ exception may result in undefined behaviour.
>


yeah. another issue though is that given how signals and C++ exceptions
work on Linux, I would suspect (untested) that trying to throw a C++
exception in a signal handler could very well cause it to blow up.

but, yeah, siglongjmp() is probably a much better idea.


> scott
>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Worth buying modem router? =?Utf-8?B?QmFkQW5nbGVy?= Wireless Networking 2 07-05-2005 02:33 PM
How much is Bill worth right now? Me_and_you ASP .Net 18 02-12-2005 03:21 AM
Market worth? Nickolas Microsoft Certification 2 12-10-2004 12:06 AM
Is it worth it? Russ McKenna ASP .Net 5 12-06-2003 06:47 PM
MCSE Cert. Is it still worth it? Dominique Feteau Microsoft Certification 3 11-05-2003 08:43 PM



Advertisments