Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Ever wonder why not just code in C instead of C++, esp numerical stuff?
New Posts  All Forums:Forum Nav:

Ever wonder why not just code in C instead of C++, esp numerical stuff?

post #1 of 13
Thread Starter 
I am learning C++ with an eye towards coding numerical stuff, from statistics to financial applications, and I am surprised with the mind-boggling complexity of the programming language. It's not just the arcane syntax, but also how many things C++ does implicitly, and only gods knows what really goes on unless you peruse all class definitions yourself.

Ok, I get it. With a matrix class and operator overloading, in C++ you can code matrix addition by simply writing

A = B + C;
D = B/C

where all three are 2D matrices. Kind of neat. Kind of what you do in Matlab. Shouldn't be this just great? In C, if all three are matrix types, you'd have to do something like

A = add(B,C);
D = solve(B,C);

C++ certainly looks nicer, but what scares me is how much stuff happens behind the black box as that line is executed. For example, if the addition operator expects the matrix arguments, but "C" above happens to be something else, the code may still be executed with no errors if there happens to be a constructor for a matrix object from the type of whatever C is.

Another scary thing is when you look at the code like this:


Blahblah();

What did just happen? Did you just call a function Blahblah() or did you just silently create an anonymous object of class Blahblah? And of such examples, there are probably many more. While C++ always you as low level access to what the machine is doing, sort of like in C, the complexity and what this does to the size of language is tremendous. One example is "move semantics". While the concept lets you do some great optimization, and catch up with the speed of comparable code in C, the concept is pretty complex. Every time I see some code using move semantics or an example with R-value reference, I have to go all the way back to my C++ tutorial and spend an hour or more to read the whole section(s) on move semantics again.

Anyways, I don't know if this is a genuine question, or maybe just a rant. C++ looks like a fantastic concept, but every once in a while, I kind of wonder.. why wouldn't you just code this in C. C code looks clumsier, but it's more "explicit" and much easier to optimize for speed.

One big advantage I see in C++ is that it may be conceptually possible to code complex software without touching raw pointers, which are in my view the equivalent of software land-mine if not handled carefully, at least outside of low-level class definitions.

[/end rant]
Edited by ZAKOH - 1/9/14 at 9:33pm
post #2 of 13
Why use C++ for statistics or finance? If you're doing finance, look into the R language. If you're doing matrix math, Julia is the low-level language of choice. Python's numpy library should handle matrices well (it's quickly replacing matlab as the standard in many places) and J language is also worth a look (it's an APL-like language that's built from the ground up to deal with matrix math and despite being interpreted is as fast or faster than C due to all the optimizations). Lush is another great language (lisp dialect) specifically designed and optimized for matrix math (and it's designed from the ground up to integrate tightly with C if you need that extra bit of performance).

All that said, C++ has many syntactical problems and doesn't offer anything of value over C. If you want OOP in C, there's a great paper on how to do this (www.cs.rit.edu/~ats/books/ooc.pdf‎). Another language worth a look is common lisp (or scheme) as it offers everywhere from interpreted to VM to compiled code that is as fast as C (for example, gambit scheme compiles to C and turning off the safety makes the code do some extreme optimizations).
Edited by hajile - 1/9/14 at 11:43pm
post #3 of 13
I guess the question is....why are you doing it in C++ if you'd rather do it in C?

For mathematical stuff I don't know why would use C++ over another (depending on what your end goal is).
 
Play Server
(10 items)
 
NAS
(14 items)
 
CPUCPUMotherboardRAM
Intel Xeon L5640 Intel Xeon L5640 Dell R710 Mobo Hynix 144GB DDR3 ECC PC3L-10600R 
Hard DrivePowerCaseOther
Intel X25-M G2 80GB Redundant 840W PSU Dell 710 2.5" - Google Search Appliance Dell Perc H700 Raid Controller 
OtherOther
Dell iDRAC6 Enterprise Mellanox Connectx-2 10Gb SFP+ NIC 
CPUCPUMotherboardRAM
Intel Xeon E5-2670 v1 Intel Xeon E5-2670 v1 SuperMicro X9DRL-iF 64GB Kingston ECC (kvr16r11d4/16HA) 
Hard DriveHard DriveHard DriveCooling
12x 4TB HGST 5K4000 SanDisk SSD 960GB (cache) 6x 8TB WD Red Scythe AP-29 
CoolingOSPowerCase
2x Noctua U9DXi4 FreeNAS Corsair AX750 Norco RPC-4224 
OtherOther
3x LSI 9211-8i (HBA) Mellanox Connectx-2 10Gb NIC 
  hide details  
Reply
 
Play Server
(10 items)
 
NAS
(14 items)
 
CPUCPUMotherboardRAM
Intel Xeon L5640 Intel Xeon L5640 Dell R710 Mobo Hynix 144GB DDR3 ECC PC3L-10600R 
Hard DrivePowerCaseOther
Intel X25-M G2 80GB Redundant 840W PSU Dell 710 2.5" - Google Search Appliance Dell Perc H700 Raid Controller 
OtherOther
Dell iDRAC6 Enterprise Mellanox Connectx-2 10Gb SFP+ NIC 
CPUCPUMotherboardRAM
Intel Xeon E5-2670 v1 Intel Xeon E5-2670 v1 SuperMicro X9DRL-iF 64GB Kingston ECC (kvr16r11d4/16HA) 
Hard DriveHard DriveHard DriveCooling
12x 4TB HGST 5K4000 SanDisk SSD 960GB (cache) 6x 8TB WD Red Scythe AP-29 
CoolingOSPowerCase
2x Noctua U9DXi4 FreeNAS Corsair AX750 Norco RPC-4224 
OtherOther
3x LSI 9211-8i (HBA) Mellanox Connectx-2 10Gb NIC 
  hide details  
Reply
post #4 of 13
Thread Starter 
Well, C++ is a sort of a standard in computational finance industry, although pretty much anything is used, from spreadsheet to FORTRAN. R and Octave are great. R is already the biggest language in statistics, and there is a growing finance R community. Nonetheless, when you want to run things as fast as allowed by hardware, C and C++ can be pretty fast. If you can't vectorize your code in Matlab or R, there will be a big performance hit. Why is C++ so popular? I don't know. I guess people were sold out on operator overloading as in the example above. This started in the 80/90s and continued since then.
Edited by ZAKOH - 1/10/14 at 12:42am
post #5 of 13
Try doing COM in C.
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
post #6 of 13
The primary benefit of C++ lies in its object-oriented approach to programming, which can be tremendously helpful for a lot of complex applications. C++ is a particularly interesting/widely used language because it provides the deep powers of C (like pointer manipulation) while still being an object-oriented language.
post #7 of 13
I, on the other hand, don't see any advantage of using C over C++ for purposes other than legacy systems. C++ is just a more modern language which, especially with C++11, allows you to write shorter, more maintainable, more reusable and safer code. I believe it's actually easier to run into unexpected behaviour in C than in C++, as long as you understand what the compiler is doing behind the scenes. It's up to you or library creators to make interfaces which are easy to use correctly and difficult to use incorrectly. C++ is in fact often more explicit than C (e.g. casting).

As for your examples it's not nearly as complicated as you make it out to be. Consider the following code:
Code:
class Matrix
{
public:
        Matrix();
        Matrix(const float fillValue);
        Matrix(const Matrix& other);

        Matrix operator + (const Matrix& other) const;

        // ...
};

int main()
{
        Matrix A, B;
        int C = 42;

        A = B + C;  // Works as intended: calls operator+ ( Matrix((float) C) )
}

Don't like this behaviour? Make float constructor explicit:
Code:
class Matrix
{
public:
        Matrix();
        explicit Matrix(const float fillValue);
        Matrix(const Matrix& other);

        Matrix operator + (const Matrix& other) const;

        // ...
};

int main()
{
        Matrix A, B;
        int C = 42;

        A = B + C;  // compiler error: there is no acceptable conversion
}

Want to specifically disable operator+ for ints?
Code:
class Matrix
{
public:
        Matrix();
        Matrix(const float fillValue);
        Matrix(const Matrix& other);

        Matrix operator + (const Matrix& other) const;

private:
        Matrix operator + (int illegal);
};

int main()
{
        Matrix A, B;
        int C = 42;

        A = B + C;  // compiler error
}

As for your second example I'm not really sure where you're coming from. Are complaining that you can't say what a given function is doing by just looking at the function call? How is it any different in C?

Overall though C++ is just a tool. If you feel more comfortable with C, you can write pure C code in C++ and it will work just fine.

Edit: You may find this video helpful in understanding move semantics:

http://channel9.msdn.com/Events/GoingNative/2013/An-Effective-Cpp11-14-Sampler#!
Edited by poroboszcz - 1/12/14 at 1:16pm
buka
(17 items)
 
  
Reply
buka
(17 items)
 
  
Reply
post #8 of 13
Quote:
Originally Posted by poroboszcz View Post

I, on the other hand, don't see any advantage of using C over C++ for purposes other than legacy systems. C++ is just a more modern language which, especially with C++11, allows you to write shorter, more maintainable, more reusable and safer code. I believe it's actually easier to run into unexpected behaviour in C than in C++, as long as you understand what the compiler is doing behind the scenes. It's up to you or library creators to make interfaces which are easy to use correctly and difficult to use incorrectly. C++ is in fact often more explicit than C (e.g. casting).

As for your examples it's not nearly as complicated as you make it out to be. Consider the following code:
Don't like this behaviour? Make float constructor explicit:
Want to specifically disable operator+ for ints?
As for your second example I'm not really sure where you're coming from. Are complaining that you can't say what a given function is doing by just looking at the function call? How is it any different in C?
Overall though C++ is just a tool. If you feel more comfortable with C, you can write pure C code in C++ and it will work just fine.

Edit: You may find this video helpful in understanding move semantics:

http://channel9.msdn.com/Events/GoingNative/2013/An-Effective-Cpp11-14-Sampler#!

C and C++ are not completely compatible. Stroustrup (creator of C++) has made it clear that compatibility is desirable, but doesn't actually exist.


http://www.stroustrup.com/bs_faq.html#C-is-subset
http://en.wikipedia.org/wiki/Compatibility_of_C_and_C
http://thread.gmane.org/gmane.comp.version-control.git/57643/focus=57918 (I recommend reading the various comments by Linus and the others opposed to C++. As he points out, there are many existing issues with c++ compilers due to language complexity, STL and other parts of the language are not portable, the standard runtime is much larger, C++ programs are slower than C, etc.)

If you are doing work that requires things like classes, you should probably be looking toward a safer language than C++ (perhaps haskell or lisp both of which are just as fast while being much safer).
post #9 of 13
Quote:
Originally Posted by hajile View Post

C and C++ are not completely compatible. Stroustrup (creator of C++) has made it clear that compatibility is desirable, but doesn't actually exist.


http://www.stroustrup.com/bs_faq.html#C-is-subset
http://en.wikipedia.org/wiki/Compatibility_of_C_and_C
http://thread.gmane.org/gmane.comp.version-control.git/57643/focus=57918 (I recommend reading the various comments by Linus and the others opposed to C++. As he points out, there are many existing issues with c++ compilers due to language complexity, STL and other parts of the language are not portable, the standard runtime is much larger, C++ programs are slower than C, etc.)

If you are doing work that requires things like classes, you should probably be looking toward a safer language than C++ (perhaps haskell or lisp both of which are just as fast while being much safer).

You seem pretty biased. What I meant is that you don't have to use any of the C++ specific features like polymorphism, exceptions, templates etc. and can stay within the C subset of C++ if that's your desire, not that both languages are 100% compatible.

Linus Torvalds is a known C++ hater and I wouldn't take anything he says seriously. Especially something he said 7 years ago when compilers weren't half as good as they are now and C++11 wasn't yet in the making. Even GCC, which he uses to compile his precious code is now written in C++, and here's their rationale:
Quote:
  • C++ is a standardized, well known, popular language.
  • C++ is nearly a superset of C90 used in GCC.
  • The C subset of C++ is just as efficient as C.
  • C++ supports cleaner code in several significant cases.
  • C++ makes it easier to write and enforce cleaner interfaces.
  • C++ never requires uglier code.
  • C++ is not a panacea but it is an improvement

This to me sounds somewhat more convincing than "C++ sucks because C++ programmers are idiots and C programmers, like myself, are gods.".
buka
(17 items)
 
  
Reply
buka
(17 items)
 
  
Reply
post #10 of 13
Quote:
Originally Posted by hajile View Post

If you are doing work that requires things like classes, you should probably be looking toward a safer language than C++ (perhaps haskell or lisp both of which are just as fast while being much safer).

That's great if you work for yourself.
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Ever wonder why not just code in C instead of C++, esp numerical stuff?