Which programming language is more useful?

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

Gian-Carlo Pascutto
Posts: 1243
Joined: Sat Dec 13, 2008 7:00 pm

Re: Which programming language is more useful?

Post by Gian-Carlo Pascutto »

mcostalba wrote:
MattieShoes wrote:Haha, I write in C++ and use memset all the time. Is there some simpler C++ alternative to this to zero out a history table?

Code: Select all

memset(history, 0, sizeof(history));

Code: Select all

*history = EmptyHistory;
But where is the code that defines EmptyHistory?

Seriously, memset can be dangerous when mixed with C++ objects (if they're non POD).
User avatar
sje
Posts: 4675
Joined: Mon Mar 13, 2006 7:43 pm

Re: Which programming language is more useful?

Post by sje »

Some compilers like g++ will use calls to memcpy() as code for the default copy constructor and for (part of) the code for the default assignment operator. This is okay, but for speed it's sometimes better to provide explicit definitions that copy more than one byte at a time.

For several of Symbolic's classes, the above overrides copy data eight bytes at a time using loops.
andretaff

Torvalds

Post by andretaff »

Bo Persson wrote:It teaches the use of the C++ standard library, containers, iterators, overloads, generic functions, and user defined classes, before going into the really difficult stuff like pointers and arrays. :-)

...

There is a lot of C that is of very little use, once you have learned the better ways of C++. So start there!
Just for the pleasure of pumping gas into our little flame war, I show you the words of Linus Torvalds:
C++ is a horrible language. It's made more horrible by the fact that a lot of substandard programmers use it, to the point where it's much much easier to generate total and utter crap with it. Quite frankly, even if the choice of C were to do *nothing* but keep the C++ programmers out, that in itself would be a huge reason to use C.
http://thread.gmane.org/gmane.comp.vers ... ocus=57918

Man, this guy is insane. He's great.
:D

(Linus doesn't like operator overloading, too much abstraction, boost, stl)
Gian-Carlo Pascutto
Posts: 1243
Joined: Sat Dec 13, 2008 7:00 pm

Re: Which programming language is more useful?

Post by Gian-Carlo Pascutto »

When I first leaned C over thirty years ago, I was struck by the resemblance of scanf()/printf() functions with the horrible old Fortran read/write/format statements. I thought to myself that there must be a better way, and the C++ formatted character streams are that better way.
Funny you should bring that up as an example. From http://horstmann.com/

Code: Select all

The March of Progress

1980: C 
    printf("%10.2f", x);
1988: C++
    cout << setw&#40;10&#41; << setprecision&#40;2&#41; << showpoint << x;
1996&#58; Java

    java.text.NumberFormat formatter = java.text.NumberFormat.getNumberInstance&#40;); formatter.setMinimumFractionDigits&#40;2&#41;; formatter.setMaximumFractionDigits&#40;2&#41;; String s = formatter.format&#40;x&#41;; for &#40;int i = s.length&#40;); i < 10; i++) System.out.print&#40;' '); System.out.print&#40;s&#41;;

2004&#58; Java

    System.out.printf&#40;"%10.2f", x&#41;;

2008&#58; Scala and Groovy

    printf&#40;"%10.2f", x&#41;
mcostalba
Posts: 2684
Joined: Sat Jun 14, 2008 9:17 pm

Re: Which programming language is more useful?

Post by mcostalba »

sje wrote:Some compilers like g++ will use calls to memcpy() as code for the default copy constructor and for (part of) the code for the default assignment operator. This is okay, but for speed it's sometimes better to provide explicit definitions that copy more than one byte at a time.
Does memcpy() copy one byte at a time?


P.S: Yes EmptyHistory was not defined, ahaaa you caught me :-)
Tord Romstad
Posts: 1808
Joined: Wed Mar 08, 2006 9:19 pm
Location: Oslo, Norway

Re: Which programming language is more useful?

Post by Tord Romstad »

Gian-Carlo Pascutto wrote:
When I first leaned C over thirty years ago, I was struck by the resemblance of scanf()/printf() functions with the horrible old Fortran read/write/format statements. I thought to myself that there must be a better way, and the C++ formatted character streams are that better way.
Funny you should bring that up as an example. From http://horstmann.com/

Code: Select all

The March of Progress

1980&#58; C 
    printf&#40;"%10.2f", x&#41;;
1988&#58; C++
    cout << setw&#40;10&#41; << setprecision&#40;2&#41; << showpoint << x;
1996&#58; Java

    java.text.NumberFormat formatter = java.text.NumberFormat.getNumberInstance&#40;); formatter.setMinimumFractionDigits&#40;2&#41;; formatter.setMaximumFractionDigits&#40;2&#41;; String s = formatter.format&#40;x&#41;; for &#40;int i = s.length&#40;); i < 10; i++) System.out.print&#40;' '); System.out.print&#40;s&#41;;

2004&#58; Java

    System.out.printf&#40;"%10.2f", x&#41;;

2008&#58; Scala and Groovy

    printf&#40;"%10.2f", x&#41;
That's somewhat similar to the hilarious The Evolution of a Programmer. But as always, Haskell is more fun. I particularly like the "Ph.D. Haskell Programmer" and the "Post-doc Haskell Programmer". :)

Tord
User avatar
sje
Posts: 4675
Joined: Mon Mar 13, 2006 7:43 pm

Re: Which programming language is more useful?

Post by sje »

mcostalba wrote:
sje wrote:Some compilers like g++ will use calls to memcpy() as code for the default copy constructor and for (part of) the code for the default assignment operator. This is okay, but for speed it's sometimes better to provide explicit definitions that copy more than one byte at a time.
Does memcpy() copy one byte at a time?
Well, memcpy() can copy one byte at a time; see the man page.

If memcpy can optimize by copying multiple bytes per iteration, then it must figure sizes and maybe alignments at run time. I doubt that this happens. Handling the general case is usually the most expensive approach. Also, it's not guaranteed that memcpy() will be expanded inline, so it has some call overhead as well.
User avatar
sje
Posts: 4675
Joined: Mon Mar 13, 2006 7:43 pm

Re: Which programming language is more useful?

Post by sje »

Gian-Carlo Pascutto wrote:
When I first leaned C over thirty years ago, I was struck by the resemblance of scanf()/printf() functions with the horrible old Fortran read/write/format statements. I thought to myself that there must be a better way, and the C++ formatted character streams are that better way.
Funny you should bring that up as an example. From http://horstmann.com/

Code: Select all

The March of Progress

1980&#58; C 
    printf&#40;"%10.2f", x&#41;;
1988&#58; C++
    cout << setw&#40;10&#41; << setprecision&#40;2&#41; << showpoint << x;
1996&#58; Java

    java.text.NumberFormat formatter = java.text.NumberFormat.getNumberInstance&#40;); formatter.setMinimumFractionDigits&#40;2&#41;; formatter.setMaximumFractionDigits&#40;2&#41;; String s = formatter.format&#40;x&#41;; for &#40;int i = s.length&#40;); i < 10; i++) System.out.print&#40;' '); System.out.print&#40;s&#41;;

2004&#58; Java

    System.out.printf&#40;"%10.2f", x&#41;;

2008&#58; Scala and Groovy

    printf&#40;"%10.2f", x&#41;
Heh. Actually, I always explicitly code the package name:

Code: Select all

    std&#58;&#58;cout << std&#58;&#58;setw&#40;10&#41; << std&#58;&#58;setprecision&#40;2&#41; << std&#58;&#58;showpoint << x;
Overall, C++ looks even more reasonable when symbolic expressions are needed for field parameter specifiers; it's a pain to do the same in C.

In the old days I used to teach introductory computer science (a.k.a "How To Get Fortran To Do Something Useful"). It seems that my fellow instructors and I had to spend at least a week and a half of each course imparting the arcane knowledge of READ, WRITE, and FORMAT. What a waste that was.
User avatar
Bo Persson
Posts: 243
Joined: Sat Mar 11, 2006 8:31 am
Location: Malmö, Sweden
Full name: Bo Persson

Re: Which programming language is more useful?

Post by Bo Persson »

sje wrote:
mcostalba wrote:
sje wrote:Some compilers like g++ will use calls to memcpy() as code for the default copy constructor and for (part of) the code for the default assignment operator. This is okay, but for speed it's sometimes better to provide explicit definitions that copy more than one byte at a time.
Does memcpy() copy one byte at a time?
Well, memcpy() can copy one byte at a time; see the man page.

If memcpy can optimize by copying multiple bytes per iteration, then it must figure sizes and maybe alignments at run time. I doubt that this happens. Handling the general case is usually the most expensive approach. Also, it's not guaranteed that memcpy() will be expanded inline, so it has some call overhead as well.
It depends on your compiler!

MSVC, for example, will figure out aligning at runtime and get up to speed after the first 1-3 bytes. If it can figure out size and alignment at compile time, like when initializing some fixed data :-), it will inline optimized code.
User avatar
Bo Persson
Posts: 243
Joined: Sat Mar 11, 2006 8:31 am
Location: Malmö, Sweden
Full name: Bo Persson

Re: Which programming language is more useful?

Post by Bo Persson »

Gian-Carlo Pascutto wrote:
When I first leaned C over thirty years ago, I was struck by the resemblance of scanf()/printf() functions with the horrible old Fortran read/write/format statements. I thought to myself that there must be a better way, and the C++ formatted character streams are that better way.
Funny you should bring that up as an example. From http://horstmann.com/

Code: Select all

The March of Progress

1980&#58; C 
    printf&#40;"%10.2f", x&#41;;
1988&#58; C++
    cout << setw&#40;10&#41; << setprecision&#40;2&#41; << showpoint << x;
1996&#58; Java

    java.text.NumberFormat formatter = java.text.NumberFormat.getNumberInstance&#40;); formatter.setMinimumFractionDigits&#40;2&#41;; formatter.setMaximumFractionDigits&#40;2&#41;; String s = formatter.format&#40;x&#41;; for &#40;int i = s.length&#40;); i < 10; i++) System.out.print&#40;' '); System.out.print&#40;s&#41;;

2004&#58; Java

    System.out.printf&#40;"%10.2f", x&#41;;

2008&#58; Scala and Groovy

    printf&#40;"%10.2f", x&#41;
That's funny! :-)

However, should you ever want to display something other than a double, how do you do that with printf?

Code: Select all


// In C++

Chess&#58;&#58;Move   CurrentMove;

std&#58;&#58;cout << CurrentMove;


/* or in C */

printf&#40;"what goes here", &CurrentMove&#41;;

As long as we only need some ints or a double, add them up, and display the result, C works just fine.