Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Programmers' lounge. (Programming-related discussions only)
New Posts  All Forums:Forum Nav:

Programmers' lounge. (Programming-related discussions only) - Page 83

post #821 of 1143
Quote:
Originally Posted by stumped View Post

IIRC, it has to do with the way C++ sets up memory usage/access and C sets up memory usage/access.

Since C++ is object oriented, the functions and variables are behind an object which requires memory access to use the object. In C++, you are not always guaranteed to efficiently use this memory space.

In C however, memory access/usage/mapping is very well understood, optimized, and presented in a more straight forward fashion. Because of this, most people when coding in C are already programming in a way that can utilize the CPUs cache more efficiently which greatly speeds up memory access and allows the CPU to be working more of the time.


* discalimer: This is what I remember when I looked into why the Linux kernel is programmed in C and not C++ when there are a lot of constructs in the kernel that exhibit object oriented philosophies. I could also be very wrong here as well, but this is what I understand of the differences.

Reasons why the Linux kernel is written in C rather than C++:

- New architectures and platforms will typically have a C compiler long before they have a C++ compiler. C is a much simpler and easier language to implement.
- Portability of C code between compilers has been far better.
- C++ requires a more complicated runtime to support things like exception handling and RTTI.
- Apparently simple statements can hide expensive operations, thanks to operator-overloading. Good in user world, bad in kernel world.

NON-reasons why the Linux kernel is written in C rather than C++:

- C++ is slower than C. C++ has the same overheads as C. Additional overheads only arise when using features C doesn't support which you can't do at the kernel level anyway.
- Virtual functions are slow. Virtual dispatch is slower than static dispatch, but the performance penalty is modest, particularly when used sparingly. Many kernels already make wide use of function tables for performing dynamic dispatch.
- C++ Templates cause massive code bloat. This is potentially true. However again, many kernels use macros to perform similar code generation effects.
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 #822 of 1143
Hey, all how's it going
Quote:
Originally Posted by axipher View Post

Maybe we can start off by getting people's preferred IDE for writing code for different languages.

I use Notepad++ for most things (HTML, PHP, JS, CSS) Java coding I used to use BlueJ before using NetBeans
post #823 of 1143
Quote:
Originally Posted by tompsonn View Post

Eh.

In C++, "you only pay for what you use." So there is nothing that would make it any slower than C. In particular for scientific programs, template expressions make it possible to perform some custom optimization using the template engine to process program semantics. Well written code in any language is always better than poorly written code in any other language.

I don't think that the reason is so much related to performance as it is to interoperability. The C++ language is more complex than the C language, but from a performance point of view there shouldn't be a notable difference in either way. Some C++ constructs are faster than the C equivalent (std::sort is faster than qsort) and there are probably good examples of the other way around.

C++ may be slower if you use its more advanced features but you have to ask yourself if you are micro-optimizing or if there really is a bottleneck.

I think your mate is in fairy land if he is going around saying C is definitely faster than C++. The same code in either language will have zero difference in speed and in some cases C++ can be faster depending on what you do, just like C can be as well.

Well that explains it even more,but why use C for building OSs instead of C++ if the written code is what matters the most?

A while back I've started attending Computational Geometry lectures and damn I love that subject I've even implemented some of the algorithms it uses like the orientation test and it's been so much fun

this is the code I currently use Warning: Spoiler! (Click to show)
Code:
#include <iostream>
#include <cmath>
using namespace std;

struct Points{
        float X, Y;//The X and Y elements of any point

        void operator () (float X1, float Y1){
                X = X1;
                Y = Y1;
        }//Assigns the value of a point
        void operator = (Points P){
                X = P.X;
                Y = P.Y;
        }//Assigns the value of a point to another point

        Points operator + (Points P){
                Points P1;
                P1.X = X + P.X;
                P1.Y = Y + P.Y;
                return P1;
        }//Sums a point with another point
        Points operator - (Points P){
                Points P1;
                P1.X = X - P.X;
                P1.Y = Y - P.Y;
                return P1;
        }//Substracts a point from another point

        void operator * (float Num){
                X = X * Num;
                Y = Y * Num;
        }//Multiplies the point by a number
        void operator / (float Num){
                X = X / Num;
                Y = Y / Num;
        }//Divides a point by a number

        float Distance (Points P){
                float Distance = sqrt((pow((X - P.X), 2.0) + pow((Y - P.Y), 2.0)));
                return Distance;
        }//Calculates the distance between two points

        void Display(){
                cout << "(" << X << "," << Y << ")\n";
        }//Displays the Point on the console
};

class OrientationTest{
public:
        //Checks if a point is on the same line,left or right of it
        int Point_Check(Points Start_P, Points End_P, Points Check_P){
                float AreaOfTriangle = 0.5 * (Start_P.X *(End_P.Y - Check_P.Y)) - (End_P.X * (Start_P.Y - Check_P.Y)) + (Check_P.X * (Start_P.Y - End_P.Y));
                if (AreaOfTriangle > 0)
                        return 1;
                else if (AreaOfTriangle < 0)
                        return -1;
                else
                        return 0;
        }//-1 means the point is on the right of the line,1 means the point is on the left of the line,0 means the point is on the same line/line extention
        
        //Checks if a line is intersected,on the right or on the left of another line
        int Line_Check(Points Start_P, Points End_P, Points Start_Check, Points End_Check){
                int Result[2];
                Result[0] = Point_Check(Start_P, End_P, Start_Check);
                Result[1] = Point_Check(Start_P, End_P, End_Check);
                if (Result[0] == 1 && Result[1] == 1)
                        return 1;
                else if (Result[0] == -1 && Result[1] == -1)
                        return -1;
                else{
                        Result[0] = Point_Check(Start_Check, End_Check, Start_P);
                        Result[1] = Point_Check(Start_Check, End_Check, End_P);
                        if (Result[0] == Result[1])
                                return 0;
                        else
                                return 10;
                }
        }//-1 means the line is on the right,1 means is on the left,0 means the line isn't intersecting,10 means the line is intersecting

        //Checks if a point is inside a triangle or not
        bool Triangle_Check(Points T1, Points T2, Points T3, Points Check){
                Points TrianglePoints[3] = { T1, T2, T3 };
                int Results[3], ReturnValue = 1;
                for (int i = 0; i < 3; ++i){
                        if (i < 2)
                                Results[i] = Point_Check(TrianglePoints[i], TrianglePoints[i + 1], Check);
                        else
                                Results[i] = Point_Check(TrianglePoints[i], TrianglePoints[i - 2], Check);
                        if (i > 0)
                                if (Results[i] != Results[i - 1]){
                                        ReturnValue = 0;
                                        break;
                                }
                }
                return ReturnValue;
        }//True means it's inside the triangle,false means it's not

#define PI 3.14159265
        //Checks if a point is inside a circle or not
        bool Circle_Check(Points Center, float Radius, Points Check){
                Points TheCurrentPoint, ThePreviousPoint;//TheCurrentPoint is the point that'll be used along with the Center point to test the orientation of the Check point

                //Initialize the value of TheCurrentPoint
                TheCurrentPoint.X = Center.X + Radius;
                TheCurrentPoint.Y = Center.Y;

                float Angle = 180, VectorsAngel = 180, Angle_In_Radians;//Angle represents the angle between the current point and the maximum X point(Except for when the maximum X point is the same as the CurrentPoint,in that case the angle is 0 not 180),VectorsAngle is the angle between the two vectors,Angle_In_Radians is the angle in redians
                bool ReturnValue = false;
                while (VectorsAngel >= 0.1){
                        if (Point_Check(Center, TheCurrentPoint, Check) == 0){
                                if (Check.Distance(Center) <= TheCurrentPoint.Distance(Center)){
                                        ReturnValue = true;
                                        break;
                                }
                                else
                                        break;
                        }
                        else{
                                if (Angle != 180){
                                        if (Point_Check(Center, TheCurrentPoint, Check) == 1)
                                                Angle += 0.5 * Angle;
                                        else
                                                Angle -= 0.5 * Angle;
                                }
                                else{
                                        if (Point_Check(Center, TheCurrentPoint, Check) == 1)
                                                Angle -= 0.5 * Angle;
                                        else
                                                Angle += 0.5 * Angle;
                                }
                        }

                        //The Angle in redians
                        Angle_In_Radians = (Angle * PI) / 180;

                        //The new CurrentPoint
                        TheCurrentPoint.X = (Radius * cos(Angle_In_Radians)) + Center.X;
                        TheCurrentPoint.Y = (Radius * sin(Angle_In_Radians)) + Center.Y;

                        ThePreviousPoint = TheCurrentPoint;
                        if (Triangle_Check(Center, TheCurrentPoint, ThePreviousPoint, Check) == true){
                                ReturnValue = true;
                                break;
                        }
                        VectorsAngel /= 2;
                }
                return ReturnValue;
        }//If it returns false then the point isn't in the circle and vice versa
};

Do you guys have any tips for me like how to name my variables and such?
My First
(9 items)
 
My dream
(7 items)
 
 
CPUMotherboardGraphicsGraphics
i7 4960x ASUS Rampage IV Extreme Black Edition GTX 780 ti GTX 780 ti 
GraphicsRAMHard Drive
GTX 780 ti  64 GB corsair dominator Samsung 840 pro 256GB SSD 
  hide details  
Reply
My First
(9 items)
 
My dream
(7 items)
 
 
CPUMotherboardGraphicsGraphics
i7 4960x ASUS Rampage IV Extreme Black Edition GTX 780 ti GTX 780 ti 
GraphicsRAMHard Drive
GTX 780 ti  64 GB corsair dominator Samsung 840 pro 256GB SSD 
  hide details  
Reply
post #824 of 1143
Quote:
Originally Posted by ABD EL HAMEED View Post

Well that explains it even more,but why use C for building OSs instead of C++ if the written code is what matters the most?

A while back I've started attending Computational Geometry lectures and damn I love that subject I've even implemented some of the algorithms it uses like the orientation test and it's been so much fun

this is the code I currently use Warning: Spoiler! (Click to show)
Code:
#include <iostream>
#include <cmath>
using namespace std;

struct Points{
        float X, Y;//The X and Y elements of any point

        void operator () (float X1, float Y1){
                X = X1;
                Y = Y1;
        }//Assigns the value of a point
        void operator = (Points P){
                X = P.X;
                Y = P.Y;
        }//Assigns the value of a point to another point

        Points operator + (Points P){
                Points P1;
                P1.X = X + P.X;
                P1.Y = Y + P.Y;
                return P1;
        }//Sums a point with another point
        Points operator - (Points P){
                Points P1;
                P1.X = X - P.X;
                P1.Y = Y - P.Y;
                return P1;
        }//Substracts a point from another point

        void operator * (float Num){
                X = X * Num;
                Y = Y * Num;
        }//Multiplies the point by a number
        void operator / (float Num){
                X = X / Num;
                Y = Y / Num;
        }//Divides a point by a number

        float Distance (Points P){
                float Distance = sqrt((pow((X - P.X), 2.0) + pow((Y - P.Y), 2.0)));
                return Distance;
        }//Calculates the distance between two points

        void Display(){
                cout << "(" << X << "," << Y << ")\n";
        }//Displays the Point on the console
};

class OrientationTest{
public:
        //Checks if a point is on the same line,left or right of it
        int Point_Check(Points Start_P, Points End_P, Points Check_P){
                float AreaOfTriangle = 0.5 * (Start_P.X *(End_P.Y - Check_P.Y)) - (End_P.X * (Start_P.Y - Check_P.Y)) + (Check_P.X * (Start_P.Y - End_P.Y));
                if (AreaOfTriangle > 0)
                        return 1;
                else if (AreaOfTriangle < 0)
                        return -1;
                else
                        return 0;
        }//-1 means the point is on the right of the line,1 means the point is on the left of the line,0 means the point is on the same line/line extention
        
        //Checks if a line is intersected,on the right or on the left of another line
        int Line_Check(Points Start_P, Points End_P, Points Start_Check, Points End_Check){
                int Result[2];
                Result[0] = Point_Check(Start_P, End_P, Start_Check);
                Result[1] = Point_Check(Start_P, End_P, End_Check);
                if (Result[0] == 1 && Result[1] == 1)
                        return 1;
                else if (Result[0] == -1 && Result[1] == -1)
                        return -1;
                else{
                        Result[0] = Point_Check(Start_Check, End_Check, Start_P);
                        Result[1] = Point_Check(Start_Check, End_Check, End_P);
                        if (Result[0] == Result[1])
                                return 0;
                        else
                                return 10;
                }
        }//-1 means the line is on the right,1 means is on the left,0 means the line isn't intersecting,10 means the line is intersecting

        //Checks if a point is inside a triangle or not
        bool Triangle_Check(Points T1, Points T2, Points T3, Points Check){
                Points TrianglePoints[3] = { T1, T2, T3 };
                int Results[3], ReturnValue = 1;
                for (int i = 0; i < 3; ++i){
                        if (i < 2)
                                Results[i] = Point_Check(TrianglePoints[i], TrianglePoints[i + 1], Check);
                        else
                                Results[i] = Point_Check(TrianglePoints[i], TrianglePoints[i - 2], Check);
                        if (i > 0)
                                if (Results[i] != Results[i - 1]){
                                        ReturnValue = 0;
                                        break;
                                }
                }
                return ReturnValue;
        }//True means it's inside the triangle,false means it's not

#define PI 3.14159265
        //Checks if a point is inside a circle or not
        bool Circle_Check(Points Center, float Radius, Points Check){
                Points TheCurrentPoint, ThePreviousPoint;//TheCurrentPoint is the point that'll be used along with the Center point to test the orientation of the Check point

                //Initialize the value of TheCurrentPoint
                TheCurrentPoint.X = Center.X + Radius;
                TheCurrentPoint.Y = Center.Y;

                float Angle = 180, VectorsAngel = 180, Angle_In_Radians;//Angle represents the angle between the current point and the maximum X point(Except for when the maximum X point is the same as the CurrentPoint,in that case the angle is 0 not 180),VectorsAngle is the angle between the two vectors,Angle_In_Radians is the angle in redians
                bool ReturnValue = false;
                while (VectorsAngel >= 0.1){
                        if (Point_Check(Center, TheCurrentPoint, Check) == 0){
                                if (Check.Distance(Center) <= TheCurrentPoint.Distance(Center)){
                                        ReturnValue = true;
                                        break;
                                }
                                else
                                        break;
                        }
                        else{
                                if (Angle != 180){
                                        if (Point_Check(Center, TheCurrentPoint, Check) == 1)
                                                Angle += 0.5 * Angle;
                                        else
                                                Angle -= 0.5 * Angle;
                                }
                                else{
                                        if (Point_Check(Center, TheCurrentPoint, Check) == 1)
                                                Angle -= 0.5 * Angle;
                                        else
                                                Angle += 0.5 * Angle;
                                }
                        }

                        //The Angle in redians
                        Angle_In_Radians = (Angle * PI) / 180;

                        //The new CurrentPoint
                        TheCurrentPoint.X = (Radius * cos(Angle_In_Radians)) + Center.X;
                        TheCurrentPoint.Y = (Radius * sin(Angle_In_Radians)) + Center.Y;

                        ThePreviousPoint = TheCurrentPoint;
                        if (Triangle_Check(Center, TheCurrentPoint, ThePreviousPoint, Check) == true){
                                ReturnValue = true;
                                break;
                        }
                        VectorsAngel /= 2;
                }
                return ReturnValue;
        }//If it returns false then the point isn't in the circle and vice versa
};

Do you guys have any tips for me like how to name my variables and such?

I posted reasons in the next post why the Linux kernel uses C over 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 #825 of 1143
Quote:
Originally Posted by tompsonn View Post

I posted reasons in the next post why the Linux kernel uses C over C++.

I see,so C++ isn't that much slower than C after all but what about scientific applications performance using both?
My First
(9 items)
 
My dream
(7 items)
 
 
CPUMotherboardGraphicsGraphics
i7 4960x ASUS Rampage IV Extreme Black Edition GTX 780 ti GTX 780 ti 
GraphicsRAMHard Drive
GTX 780 ti  64 GB corsair dominator Samsung 840 pro 256GB SSD 
  hide details  
Reply
My First
(9 items)
 
My dream
(7 items)
 
 
CPUMotherboardGraphicsGraphics
i7 4960x ASUS Rampage IV Extreme Black Edition GTX 780 ti GTX 780 ti 
GraphicsRAMHard Drive
GTX 780 ti  64 GB corsair dominator Samsung 840 pro 256GB SSD 
  hide details  
Reply
post #826 of 1143
Quote:
Originally Posted by axipher View Post

Maybe we can start off by getting people's preferred IDE for writing code for different languages.

Eclipse for Java and Notepad++ for C. Don't do much web development these days but that's usually a combination of Notepad++ and DreamWeaver.
post #827 of 1143
Quote:
Originally Posted by ABD EL HAMEED View Post

I see,so C++ isn't that much slower than C after all but what about scientific applications performance using both?

I wouldn't read so much into it. Use what you know.

One of my tips: don't worry about performance until you have performance problems.
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 #828 of 1143
Quote:
Originally Posted by tompsonn View Post

I wouldn't read so much into it. Use what you know.

One of my tips: don't worry about performance until you have performance problems.

Well I was asking about performance because I was interested in CUDA parallel computing so performance is everything and that's why I was asking about it :/
My First
(9 items)
 
My dream
(7 items)
 
 
CPUMotherboardGraphicsGraphics
i7 4960x ASUS Rampage IV Extreme Black Edition GTX 780 ti GTX 780 ti 
GraphicsRAMHard Drive
GTX 780 ti  64 GB corsair dominator Samsung 840 pro 256GB SSD 
  hide details  
Reply
My First
(9 items)
 
My dream
(7 items)
 
 
CPUMotherboardGraphicsGraphics
i7 4960x ASUS Rampage IV Extreme Black Edition GTX 780 ti GTX 780 ti 
GraphicsRAMHard Drive
GTX 780 ti  64 GB corsair dominator Samsung 840 pro 256GB SSD 
  hide details  
Reply
post #829 of 1143
Quote:
Originally Posted by tompsonn View Post

Reasons why the Linux kernel is written in C rather than C++:

- New architectures and platforms will typically have a C compiler long before they have a C++ compiler. C is a much simpler and easier language to implement.
- Portability of C code between compilers has been far better.
- C++ requires a more complicated runtime to support things like exception handling and RTTI.
- Apparently simple statements can hide expensive operations, thanks to operator-overloading. Good in user world, bad in kernel world.

NON-reasons why the Linux kernel is written in C rather than C++:

- C++ is slower than C. C++ has the same overheads as C. Additional overheads only arise when using features C doesn't support which you can't do at the kernel level anyway.
- Virtual functions are slow. Virtual dispatch is slower than static dispatch, but the performance penalty is modest, particularly when used sparingly. Many kernels already make wide use of function tables for performing dynamic dispatch.
- C++ Templates cause massive code bloat. This is potentially true. However again, many kernels use macros to perform similar code generation effects.
New architectures aren't really a reason to write Linux in C. There hasn't been a new ISA (of any real significance) in a decade.

C++ is context-sensitive while C is a context-free grammar which means that a C compiler is far less prone to bugs and C++ is provably harder to optimize (context-sensitive grammars suffer from PSPACE-completeness).

C++ only has the same overhead as C provided that you stick to the subset of C++ that is already close to C (at which point you might as well use C).

Another important point is that many C programmers use object-oriented code (even in the Linux kernel) which reduces the appeal of C++ even more.

This is the actual reason why C is used is that Linus doesn't like it.
Warning: Spoiler! (Click to show)
note: there's more to the discussion here
http://article.gmane.org/gmane.comp.version-control.git/57918
Quote:


From: Linus Torvalds linux-foundation.org>
Subject: Re: [RFC] Convert builin-mailinfo.c to use The Better String Library.
Newsgroups: gmane.comp.version-control.git
Date: 2007-09-06 17:50:28 GMT (6 years, 28 weeks, 19 hours and 54 minutes ago)


On Wed, 5 Sep 2007, Dmitry Kakurin wrote:
>
> When I first looked at Git source code two things struck me as odd:
> 1. Pure C as opposed to C++. No idea why. Please don't talk about portability,
> it's BS.

*YOU* are full of bull****.

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.

In other words: the choice of C is the only sane choice. I know Miles
Bader jokingly said "to piss you off", but it's actually true. I've come
to the conclusion that any programmer that would prefer the project to be
in C++ over C is likely a programmer that I really *would* prefer to piss
off, so that he doesn't come and screw up any project I'm involved with.

C++ leads to really really bad design choices. You invariably start using
the "nice" library features of the language like STL and Boost and other
total and utter crap, that may "help" you program, but causes:

- infinite amounts of pain when they don't work (and anybody who tells me
that STL and especially Boost are stable and portable is just so full
of BS that it's not even funny)

- inefficient abstracted programming models where two years down the road
you notice that some abstraction wasn't very efficient, but now all
your code depends on all the nice object models around it, and you
cannot fix it without rewriting your app.

In other words, the only way to do good, efficient, and system-level and
portable C++ ends up to limit yourself to all the things that are
basically available in C. And limiting your project to C means that people
don't screw that up, and also means that you get a lot of programmers that
do actually understand low-level issues and don't screw things up with any
idiotic "object model" crap.

So I'm sorry, but for something like git, where efficiency was a primary
objective, the "advantages" of C++ is just a huge mistake. The fact that
we also piss off people who cannot see that is just a big additional
advantage.

If you want a VCS that is written in C++, go play with Monotone. Really.
They use a "real database". They use "nice object-oriented libraries".
They use "nice C++ abstractions". And quite frankly, as a result of all
these design decisions that sound so appealing to some CS people, the end
result is a horrible and unmaintainable mess.

But I'm sure you'd like it more than git.

Linus

Linus had a less-famous discussion here
Warning: Spoiler! (Click to show)
http://www.realworldtech.com/forum/?threadid=104196&curpostid=104333
Quote:
Oh Come On Dept (gat1024@hotmail.com) on 6/11/10 wrote:
>
>For debugging purposes, your debugger already knows which
>function is called and can tell you exactly.

I'm amazed by this total inability to understand the whole
message. It was about communication. I though that
was entirely clear, it was what the entire and whole thing
was talking about all the time.

There's no "debugger" in communication. There is no IDE
that helps figure out what's going on. There is no syntax
highlighting etc.

There are just emails with patches and suggestions flying
around. There's no room for special tools - if you as a
human cannot see what the code does from the change,
it's a problem. And you usually see about three lines of
context around the code.

So code that depends on types or other context that is not
immediately visible is bad.

Oh, you can do bad things in C too. You can do macros that
use variables (global or local) that aren't part of
the arguments to the macro, etc. But in C you really have to
work at it, or do things that are very much against all the
regular C "culture". When people do that (and it does
happen, I'll not deny it), everybody knows it's a crappy
hack.

And don't get me wrong. With three lines of context you
can't always tell what the code does in C either. You
are easily missing all the locking rules etc that are
required for the code to actually work correctly. But at
the same time, C at least has less room for the
kind of things that make it harder to see what happens.

So it's just much easier to create a mess in C++,
exactly because the language has all those features that
are literally designed to use invisible context.

The "invisible" context is all over in the C++ extensions
to the C language. It's in all the implicit object
creations, constructors and destructors. It's in default
argument values, and in the type-based polymorphism. It's
in the absolutely disgusting "pass by reference" crap.

And yes, you can use just a subset of C++ to limit the
damage from the crazy language. But the problem is, there
isn't even any "sane C++ culture". There are projects
that have done a good job at it, but some people love
using references ("it's safer than a pointer" - f*cking
moronic argument), and others think almost everything
should be done with a template.

So the "just use a subset" isn't actually a viable model
either, because there isn't any sane standard subset that
you can kind of rely on people agreeing to. What is an
abomination to one person is the greatest feature of C++
to another.

So what you get is "use the subset of C++ that is C". We
can all agree on that, and we can trivially enforce it by
using a C compiler. Problem solved. All the crap just goes
out the window.

Linus
Warning: Spoiler! (Click to show)
http://www.realworldtech.com/forum/?threadid=104196&curpostid=104335
Quote:
Mark Christiansen (aliasundercover@nospam.net) on 6/11/10 wrote:
>Linus Torvalds (torvalds@linux-foundation.org) on 6/11/10 wrote:
>
>> Much about context in C++ vs. C.
>
>Is there anything the compiler and language could do to
>help? What problems do you face that language might be
>able to mitigate?

The thing is, we seldom have problems that are really
about the language itself. C has a huge base of people who
know it, which is a big deal. And the people who know it
are the kind of people who tend to know about low-level
things, so it's a good audience too.

People who worry about language details often worry about
the wrong thing entirely. As long as the language is
expressive enough, and doesn't cause unnecessary pain, I
personally suspect the primary issues always end up being
other things.

For example, one big issue for us is compiler stability.
Maybe it's because we're the kernel, but the thing is, a
compiler bug is really painful for us.

Similarly, for the kernel, we really end up having some
very strict requirements about the code generated. We have
a very limited stack, so the compiler can't just go around
generating temporary structures on the stack willy nilly.

In fact, in the kernel, we tend to limit even our C use!
We may do some things that almost nobody else does (like
play with calling conventions, inline assembly etc), but
at the same time we eschew things like 'typedef's of
structures, exactly because if we have a local variable
that is a structure - and thus potentially uses lots of
stack space - it needs to be very visible in the source.

>What I wish for most is good descriptions of the data.

I violently agree in the sense that my personal opinion is
that you should design the data structures first, and that
good data structure design is much more important than the
code for many things. If you get the data structures and
locking right, the code will work - or at least be fixable
when it doesn't.

So good code design revolves around thinking about how
the data moves around, how it is organized, and how it is
looked up and associated with other data.

But the OO languages tend to think that that means that
objects are important, and should have methods that are
associated with them. That's just crazy talk. A single
object (or a trivial collection of objects of some type:
think iterators etc) is not a really interesting thing.

When it gets interesting is when different objects of
different types interact, and have locking rules etc. And
at that point, trying to encapsulate some "object interface"
is absolutely the wrong thing, because it's not about a
single object any more.

So I think most of the OO examples are for trivial code
that doesn't matter. It's very pretty to have operator
overloading for different types, and it makes it easy to
do things like vector or complex algebra without having
to make the types be intrinsic to the language and still
allow nice syntax.

But at the same time, that's a trivial and totally
uninteresting problem. Making it easy to express that kind
of stuff - which is what OO languages with type-based
polymorphism (of functions and/or operators) tend to do -
is simply not relevant for any complex data relationship.

So I agree that describing the data is important, but at
the same time, the things that really need the most
description are how the data hangs together, what the
consistency requirements are, what the locking rules are
(and not for a single data object either), etc etc.

And my suspicion is that you can't easily really describe
those to a compiler. So you end up having to write that
code yourself regardless.

And hey, maybe it's because I do just low-level programming
that I think so. As mentioned, most of the code I work with
really deeply cares about the kinds of things that most
software projects probably never even think about: stack
depth, memory access ordering, fine-grained locking, and
direct hardware access.

C is good at that. I think the preprocessor sucks, and
we abuse the thing mercilessly, but I don't think more
"compiler makes things easier by automating things behind
your back" would help for the kernel.

In fact, we have added things to make sure the compiler
does less behind our back. Implicit and silent
integer type conversions have caused lots of problems, so
we have some exceedingly ugly C/preprocessor code to have
our own "min()/max()" macros that simply error out if one
side has a different type or signedness than the other.

Because by default, C actually ends up not being strict
enough for us. The last thing I'd want is a compiler
that does even more silent "helping".

(Again, security issues are a big deal. Most programmers
don't much tend to worry about "what if somebody we don't
trust passes us two large integers and the addition of them
overflows?". We have tons of things like that in the
kernel, so you'll see code like

if (a + b < a)
return -EINVAL;

that protect against overflow. The "signed integer overflow
is undefined" thing is actually annoying to us. Any such
undefined behavior is not good. The compiler deciding that
it can move things around because they'd be undefined if
they ever happen is a bug for us.

(Example: we had a real security issue, because some
code ended up basically - after various inlining etc - doing
something like

var = ptr->xyz
if (!ptr)
return -EFAULT;
use var

and the compiler just removed the conditional, because it
"knew" that ptr cannot be NULL, because it would have
faulted if it was. But the thing is, users can map the
address at zero, and then the conditional actually did
matter. We now have various automatic checkers for
the above, but the thing is, for the kernel it would have
been better to warn about this than just silently have the
compiler be "helpful" and removing useless code).

It all ends up boiling down to:

- kernels are fragile and subtle things, and have very
strict rules that people really need to be aware of.

- if the language/compiler makes it easy to write code
where you can't see the things we have rules for (like
stack usage), that's a bad thing.

- hidden/implicit things are usually bad. If we want
code to be generated, we'd write that explicitly. Not
have the compiler do it for us to "help".

>C++ has methods for directing the differences to different
>code. I can't say I am real fond of inheritance as I don't
>often have problems which are heirarchical refinements of a
>generic base class.

Right. It's a trivial thing, and it's not very common in
the end. Complicated problems don't have some simple
strictly hierarchical data structures.

That said, the easy cases you can often easily do in C too.
Embedding structures inside each other is not rocket
science. The kernel does a few cases really well: see how
we do things like lists by having generic list structures
that can be embedded in other structures. Not just once,
either - so we have "multiple inheritance" of these things.

But yes, you'll need to understand pointers. And pointers
to pointers. And write some "interesting" macro wrappers
once. And it's still not going to be so pretty that you
can just consider them arbitrary iterators and pass them
around to your generic iterator-based sort routine.

But it's really not all that horrible either for the
trivial cases. And for the more complex cases, you really
did need to write the code specially anyway, because the
locking covers not a single operation, but multiple, and
you can't describe that by describing the individual data
structures anyway.

>Still, the features are good for directing differences to
>different code.

Sure. And in the kernel, we generally have to handle that
explicitly. Either we associate function pointers with
the data (and make the accessors very explicit), or we
do as you mention and have a common routine that just
handles all the cases. Both exist.

And then, occasionally, we do some really ugly crud with
the preprocessor and inlining and depend on the compiler
optimizing away dead code. There's a few cases where we
do "size-based polymorphism", where we literally have
one function (well, macro from hell) that can take integer
arguments of different size, and will do different things
to them. It's unbelievably ugly, but it wouldn't have been
any prettier in C++ either.

So you can do even polymorphism without strict language
support.

>C++ and the whole object oriented area is focused on the
>custom code model. Stroustrup in C++ specifically
>disparages parameterization.

I generally agree on trying to avoid parameterization. I
just don't think C++ is the answer to it. Or even the
object-oriented approach.

We try to do static types as far as possible (and make
the code-paths static, not parameterized). Avoid "flags"
in types.

And when that doesn't work, do the OO thing, but do it with
explicit function pointers. What's the advantage of the
C++ syntax, except to hide what is actually going on?

>Give me a good description of the data and I can make
>libraries which do useful things on structures, or classes
>if you like, which were created long after my library
>code was compiled.

I just don't think that the real problems are associated
with one particular data description. You don't want a
library routine for that kind of data - you want code that
interacts between that data and other kinds of data.

And I point to the almost total lack of any reusable C++
code as the proof in the pudding. Or just about any
"object oriented" library. They just don't exist. The
whole and only point of C++ seems to be Boost + STL. And
those aren't really all that interesting and exciting
problems, are they?

Why do you need a whole new language just to iterate over
a list?

Linus

Edited by hajile - 3/21/14 at 7:54pm
post #830 of 1143
Quote:
Originally Posted by hajile View Post


Wow....that guy REALLY hated C++,what do you think?are his reasons for hating it valid?
My First
(9 items)
 
My dream
(7 items)
 
 
CPUMotherboardGraphicsGraphics
i7 4960x ASUS Rampage IV Extreme Black Edition GTX 780 ti GTX 780 ti 
GraphicsRAMHard Drive
GTX 780 ti  64 GB corsair dominator Samsung 840 pro 256GB SSD 
  hide details  
Reply
My First
(9 items)
 
My dream
(7 items)
 
 
CPUMotherboardGraphicsGraphics
i7 4960x ASUS Rampage IV Extreme Black Edition GTX 780 ti GTX 780 ti 
GraphicsRAMHard Drive
GTX 780 ti  64 GB corsair dominator Samsung 840 pro 256GB SSD 
  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 › Programmers' lounge. (Programming-related discussions only)