📜 ⬆️ ⬇️

What I don't like in C and C ++

Please do not read professional C / C ++ programmers).

In the article, I express my point of view, if they disagree - justify in the comments.
The purpose of this article is to point out the shortcomings of C and C ++, which I really dislike and encourage you to use the new version of the language or maybe even offer some ideas for improving the standard.

Well, it's time to rekindle the holivar.

I think everyone knows that it’s awesome strings in C ++. Especially if we are talking about the old type, in the new string a lot has been fixed and improved, but there is still no support for Unicode (!).
')
In the standard C ++ 20, they seem to be about to introduce unicode strings.

C ++ 20! And this is despite the fact that C ++ has existed since 1983 .

Open your favorite IDE and try to compile the following code:

#include <iostream> #include <cstdio> int main() { char string [256]; std::cout << ": "; gets(string); std::cout << ": " << string; return 0; } 

UPD1: the commentator says that krakozyabry only on Windows. That's for sure, I forgot to write about it.
But still unpleasant.

I compiled in Dev Cpp, the GCC compiler.

Compile and see:



Good screen output, huh?

Now let's replace char string [256] with char * string.

I’m not saying that this should work, but the compiler should have thrown the error to its maximum.

We got a work program that hung.
It would be better if the compiler threw an error.
And the whole garbage is that the compiler is not only compiling it, it has not yet
Gave out warnings.

Here's another joke:

 #include <iostream> using namespace std; int main(){ int arr[100]={}; cout<<arr[101]<<endl; return 0; } 

What do we expect? The compiler will tell us that it is impossible to refer to 101 array elements, since there are only 100 elements. But we compile, run and see ... 32765 (at least on my hardware).

Hmm.

Now let's test this code:

 int i = 5; i = ++i + ++i; std::cout<<i; 

What do you think he will bring?

The correct answer depends on the compiler.

In GCC it will be 14, but depending on the optimization flags.

In a different compiler, this can easily be 12 ...

I think everyone knows that in C and in the pluses a bunch of syntactic sugar, which is not always needed.

For example, std::cout<<4["string"]; This is a valid code
It prints n, as well as std::cout<<"string"[4];

Great, huh?

And now about the patient.
C ++ and network.
These are 2 sooo badly matched concepts.
Try to just download the image of the cat from your favorite site using the standard C ++ library.
This was not possible until the adoption of the C ++ 17 standard.
In the same standard library you cannot work with JSON.
Excellent comment about this.
In general, working with JSON in C ++ is like a nightmare.
Source of
Do you think the condition will always be false?

 if(sizeof ('a') != sizeof (char)){ //do something } 

No, you are mistaken.

If you compile it as a c ++ project, then the condition is likely to fail.
Should not. [1]
And if like a C project, then in that case sizeof ('a') == sizeof (int).
That's it.
[1] In general, many different C and C ++ compilers are also a problem.
Because a lot of solutions are non-standardized and they will work only in certain compilers.

For example, 128 bit numbers in C ++. In gcc and clang there is the type __int128, while in Visual Studio it is not, because it is not a standard. Or, for example, strings in Visual Studio.

 String^ MyString3 = "Hello, world!"; //   GCC 

Or, for example, in the old Borland C ++ Builder, you can code written in Object Pascal.
And there are many such moments.

Of particular pain is the lack of a list of C and C ++ packages.

What follows from this? Use the new version of C ++, for example C ++ 17 and some problems will be solved.

I must say that in the nearest competitor C ++ - Rust there are not most of the problems from this list, for example, there is a wonderful cargo, but of course it is not perfect either.

And what problems C and C ++ do you know?
Write in the comments.

UPD: Many people seem to have misunderstood my article:
In no case do I want to criticize C / s ++ and say write on the plant.
Just pointing out the flaws in s / s ++, because they got a bit of it myself.

Everything has its drawbacks, I just shared my thoughts.
UPD2:

In the comments, many people write that this is how OS works and in general it is a feature. You guys are wrong.
The same proof that you can make a system programming language in which it is not so easy to shoot yourself in the foot.

It’s just that C / C ++ is full of solutions that nobody can fix, because it can break backward compatibility.

And yes, this is my opinion, it is subjective .

If you do not agree - better comment, and not stupidly minus, because the opinion of all of us
subjectively.

Source: https://habr.com/ru/post/432488/


All Articles