Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > To go with Go or C/C++?

Reply
Thread Tools

To go with Go or C/C++?

 
 
James Kanze
Guest
Posts: n/a
 
      05-03-2013
On Thursday, 2 May 2013 20:42:03 UTC+1, Ian Collins wrote:
> Stefan Ram wrote:
> > James Kanze <(E-Mail Removed)> writes:
> >> maintainable programs in it. And it's a lot easier to write
> >> robust, maintainable programs in C++ than in C.


> > I am not sure about this. For example, exceptions introduce
> > so many hidden paths into a seemingly simple piece of code
> > in C++.


> > http://www.gotw.ca/gotw/020.htm


> > In C, one can write


> > if( f = fopen( "file", "r" )){ use( f ); close( f ); }


> > , i.e., apply the well-established style of structured
> > programming. Without exception, we do not need no RAII,
> > we can assert that close will be executed iff the file
> > was opened successfully.


> We can (and probably would) do the same in C++. A missing file isn't
> necessarily an exceptional situation. Now if it were an exceptional
> situation and the open was part of a group of resource allocations, C
> code would have to include had written clean up code and quite likely a
> "goto error". In C++, we'd just throw and rely on the automatic clean
> up RAII provides.


The problem isn't the missing file. The problem is when you
find something in it which makes further processing impossible,
and your 10 or more levels deep in "use". At that point, in C,
you have to propagate the error up manually, to ensure that the
close is called (and that any memory you allocated in the
intermediate functions is freed). In C++, the problem more or
less takes care of itself (providing you're using the
established idioms).

This is one of the things that make C so much more difficult
than C++.

--
James
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      05-03-2013
On Friday, 3 May 2013 10:05:40 UTC+1, Andy Champ wrote:

> I learned C first. I still find that for certain kinds of I/O the
> detailed control of printf & scanf is better than streams. Even though
> in general the divorcing of format and values is a PITA...


> eg:
> printf("%4x", i)
> std::cout << std::hex << std::setw(4) << i;


If you need something quick, and you've already invested the
time and effort to learn the printf markup language, then the
C style IO is OK. (Provided you only have to output simple
types, of course. In most larger applications, you'll mostly be
outputting user defined types. And usually through various
filters and such, and not directly to a file.)

But for larger applications, embedding the format in the output
string is a real maintenance nightmare. What happens when the
client says that all of the interest rates (but none of the
other values) should be output with one more digit of precision?
In C++, you modify the interest_rates manipulator, and the job
is done. In C, you have to find every fprintf in the code,
figure out which of the format specifiers concerns interest
rates, and modify it. And then get everything translated again
to other languages, since this information is embedded in the
translated strings. (Or if you're doing real local language
output, grammatically correct, etc., you have to rework all of
the language specific DLLs. Whereas in C++, all of the language
specific DLLs will have used your manipulator.)

--
James
 
Reply With Quote
 
 
 
 
Tiib
Guest
Posts: n/a
 
      05-03-2013
On Friday, 3 May 2013 18:44:37 UTC+3, James Kanze wrote:
> On Thursday, 2 May 2013 20:42:03 UTC+1, Ian Collins wrote:
> > Stefan Ram wrote:
> > > James Kanze <(E-Mail Removed)> writes:
> > >> maintainable programs in it. And it's a lot easier to write
> > >> robust, maintainable programs in C++ than in C.


> > > I am not sure about this. For example, exceptions introduce
> > > so many hidden paths into a seemingly simple piece of code
> > > in C++.


> > > http://www.gotw.ca/gotw/020.htm


> > > In C, one can write


> > > if( f = fopen( "file", "r" )){ use( f ); close( f ); }


> > > , i.e., apply the well-established style of structured
> > > programming. Without exception, we do not need no RAII,
> > > we can assert that close will be executed iff the file
> > > was opened successfully.


> > We can (and probably would) do the same in C++. A missing file isn't
> > necessarily an exceptional situation. Now if it were an exceptional
> > situation and the open was part of a group of resource allocations, C
> > code would have to include had written clean up code and quite likely a
> > "goto error". In C++, we'd just throw and rely on the automatic clean
> > up RAII provides.


> The problem isn't the missing file. The problem is when you
> find something in it which makes further processing impossible,
> and your 10 or more levels deep in "use". At that point, in C,
> you have to propagate the error up manually, to ensure that the
> close is called (and that any memory you allocated in the
> intermediate functions is freed). In C++, the problem more or
> less takes care of itself (providing you're using the
> established idioms).
>
> This is one of the things that make C so much more difficult
> than C++.


Also C is less efficient in the situation. When the case where
further processing is impossible occurs very rarely (is actually
exceptional) then all the eliminated error checking code in C++
makes it lot faster than same thing written in C.
 
Reply With Quote
 
88888 Dihedral
Guest
Posts: n/a
 
      05-03-2013
James Kanze於 2013年5月3日星期五UTC+8下午11時56分59秒 寫道:
> On Friday, 3 May 2013 10:05:40 UTC+1, Andy Champ wrote:
>
>
>
> > I learned C first. I still find that for certain kinds of I/O the

>
> > detailed control of printf & scanf is better than streams. Even though

>
> > in general the divorcing of format and values is a PITA...

>
>
>
> > eg:

>
> > printf("%4x", i)

>
> > std::cout << std::hex << std::setw(4) << i;

>
>
>
> If you need something quick, and you've already invested the
>
> time and effort to learn the printf markup language, then the
>
> C style IO is OK. (Provided you only have to output simple
>
> types, of course. In most larger applications, you'll mostly be
>
> outputting user defined types. And usually through various
>
> filters and such, and not directly to a file.)
>
>
>
> But for larger applications, embedding the format in the output
>
> string is a real maintenance nightmare. What happens when the
>
> client says that all of the interest rates (but none of the
>
> other values) should be output with one more digit of precision?
>
> In C++, you modify the interest_rates manipulator, and the job
>
> is done. In C, you have to find every fprintf in the code,
>
> figure out which of the format specifiers concerns interest
>
> rates, and modify it. And then get everything translated again
>
> to other languages, since this information is embedded in the
>
> translated strings. (Or if you're doing real local language
>
> output, grammatically correct, etc., you have to rework all of
>
> the language specific DLLs. Whereas in C++, all of the language
>
> specific DLLs will have used your manipulator.)
>
>
>
> --


Over-loadable I/O functions are not new in C++.
But C++ emphasizes the over-loadable part in I/O
just like the virtual member functions of objects in classes.

 
Reply With Quote
 
Rui Maciel
Guest
Posts: n/a
 
      05-03-2013
Öö Tiib wrote:

> Also C is less efficient in the situation. When the case where
> further processing is impossible occurs very rarely (is actually
> exceptional) then all the eliminated error checking code in C++
> makes it lot faster than same thing written in C.


Let me get this straight: you claim that C++ is more efficient because when
C++ programs crash, they crash faster?


Rui Maciel
 
Reply With Quote
 
Rui Maciel
Guest
Posts: n/a
 
      05-03-2013
Tony wrote:

> It's not nonsense. While unlearning ability varies with each individual,
> there is time and effort required beyond what is required for learning to
> do something "right" from the get go.


Well, it is nonsense. No one needs to unlearn anything to be able to learn
something new, let alone use a different tool.


Rui Maciel
 
Reply With Quote
 
Tiib
Guest
Posts: n/a
 
      05-03-2013
On Friday, 3 May 2013 19:09:41 UTC+3, Scott Lurndal wrote:
> Tiib <(E-Mail Removed)> writes:
> >Also C is less efficient in the situation. When the case where
> >further processing is impossible occurs very rarely (is actually
> >exceptional) then all the eliminated error checking code in C++
> >makes it lot faster than same thing written in C.

>
> For this, you must offer more evidence than simple assertion,
> I'm afraid.


This is easy to test. For example two functions one returns bool that
is false very rarely, one throws very rarely (not meant as example of
good code):

bool checkThingsWithIf( int param )
{
return param % 10000 != 0;
}

void checkThingsWithTry( int param )
{
if ( param % 10000 == 0 )
{
throw true;
}
}

Too simple so compilers want to inline those, make sure they don't.
Now just write two test functions. To keep it simple I won't throw far
and deep like usual, just replace 200 ifs in cycle with one try/catch:

int testWithIf( int param )
{
int ret = 0;
for ( int j = 0; j < 200; ++j )
{
if ( !checkThingsWithIf( param+j ) )
{
return ret;
}
++ret;
}
return ret;
}

int testWithTry( int param )
{
int ret = 0;
try
{
for ( int j = 0; j < 200; ++j )
{
checkThingsWithTry( param+j );
++ret;
}
}
catch ( bool )
{ }
return ret;
}

The stuff is fast so lets run the tests million times. I write it as C-ish
as I can ...

#include <cstdio>
#include <ctime>

int main( int argc, char* argv[] )
{
clock_t volatile start;
clock_t volatile stop;
int volatile sum;

start = clock();
sum = 0;
for ( int i = 0; i < 1000000; ++i )
{
sum += testWithIf( i );
}
stop = clock();
printf( "With if it took %d msec; (sum %d)\n", ms_elapsed( start, stop ), sum );

start = clock();
sum = 0;
for ( int i = 0; i < 1000000; ++i )
{
sum += testWithTry( i );
}
stop = clock();
printf( "With try it took %d msec; (sum %d)\n", ms_elapsed( start, stop ), sum );
}


Output with Visual studio 2010 (run it *without* debugging otherwise it
heavily hooks itself to exceptions):
With if it took 921 msec; (sum 197990000)
With try it took 782 msec; (sum 197990000)

So 17% better performance thanks to replacing 200 ifs in cycle with one
try/catch.
 
Reply With Quote
 
Tiib
Guest
Posts: n/a
 
      05-03-2013
On Friday, 3 May 2013 20:46:39 UTC+3, Tiib wrote:
> On Friday, 3 May 2013 19:09:41 UTC+3, Scott Lurndal wrote:
>
> > Tiib <(E-Mail Removed)> writes:

> #include <cstdio>
>
> #include <ctime>
>


Oh, I forgot to type the ms_elapsed here:

int ms_elapsed( clock_t start, clock_t stop )
{
return static_cast<int>( 1000.0 * ( stop - start ) / CLOCKS_PER_SEC);
}

> int main( int argc, char* argv[] )
> {
> clock_t volatile start;
> clock_t volatile stop;
> int volatile sum;
>
> start = clock();
> sum = 0;
> for ( int i = 0; i < 1000000; ++i )
> {
> sum += testWithIf( i );
> }
> stop = clock();
> printf( "With if it took %d msec; (sum %d)\n", ms_elapsed( start,stop ), sum );
>
> start = clock();
> sum = 0;
> for ( int i = 0; i < 1000000; ++i )
> {
> sum += testWithTry( i );
> }
> stop = clock();
> printf( "With try it took %d msec; (sum %d)\n", ms_elapsed( start, stop ), sum );
> }


 
Reply With Quote
 
Melzzzzz
Guest
Posts: n/a
 
      05-03-2013
On Fri, 03 May 2013 18:32:27 +0100
Rui Maciel <(E-Mail Removed)> wrote:

> Öö Tiib wrote:
>
> > Also C is less efficient in the situation. When the case where
> > further processing is impossible occurs very rarely (is actually
> > exceptional) then all the eliminated error checking code in C++
> > makes it lot faster than same thing written in C.

>
> Let me get this straight: you claim that C++ is more efficient
> because when C++ programs crash, they crash faster?
>
>
> Rui Maciel


Exceptions come with cost (runtime or space)...


 
Reply With Quote
 
Tiib
Guest
Posts: n/a
 
      05-03-2013
On Friday, 3 May 2013 20:32:27 UTC+3, Rui Maciel wrote:
> Tiib wrote:
> > Also C is less efficient in the situation. When the case where
> > further processing is impossible occurs very rarely (is actually
> > exceptional) then all the eliminated error checking code in C++
> > makes it lot faster than same thing written in C.

>
> Let me get this straight: you claim that C++ is more efficient because when
> C++ programs crash, they crash faster?


Let me get it straight, when you have your head full of nonsense then
whatever you look at looks like that nonsense in your head.
 
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
Unicode issue with Python v3.3 33 Python 80 04-19-2013 07:16 PM
newbie: Problem with $ and \ in strings vivek_12315 Perl Misc 2 04-12-2013 08:30 PM
name lookup failure using metaclasses with unittests Ulrich Eckhardt Python 7 04-12-2013 02:58 PM
Re: print to screen and file with one print statement amiwinter@gmail.com Python 0 04-09-2013 01:31 PM
USBLock : lock/unlock your computer with a USB key Sven Python 0 04-08-2013 03:21 PM



Advertisments