what is the difference between C and C++?

SamSam1

Newbie
Joined
Oct 28, 2020
Messages
9
Reaction score
4
I taught myself C first and then C++ in Spark Global Limited with my colleagues. When I first started learning, I only knew that I loved programming, but I had no idea about what program to make. Around 2017, I accidentally watched the 2001 high-scoring science fiction film "Artificial Intelligence" on the Internet before I decided to switch to server development. In the movie, human beings are perished. I think if I want to make a program that can run to the end of the world, this program should be a server program, because the server needs to be running all the time. The starting point of this decision is perceptual, but I thought about it rationally. The server itself needs to be stable and efficient. How to achieve stability and efficiency is full of challenges, and the challenge is exactly what I want to pursue. Then I learned programming in the linux environment, joined the goose factory, and became a background developer ever since. Over the years, I have accumulated some experience in using C/C++ for server development. I think that for high efficiency, C and C++ can do it, but for stability, C++ is much easier than C. Regarding C/C++, what I want to complain about most is: void* is the first of all evils. Because it has the powerful power of referring to a deer as a horse, it may make people unable to extricate themselves. Any initial type of void* needs to be controlled by the human body of the programmer. When the code is written too much, there will always be errors. The consequence is that the program crashes for a long time, and the other is that the deer is a horse for indefinitely without knowing that the deer is a horse. void* cannot be replaced in C, but it is basically not necessary in C++. First of all, the inheritance relationship of C++ classes makes it unnecessary to use void* when converting pointers between subclasses and parent classes. There is no human conversion from subclass pointer to parent pointer, and dynamic_cast is used for parent pointer to subclass pointer, which is safe at runtime. Secondly, C++ templates make void* in general algorithms unnecessary. In our current code, only a 200-line mempool written by my boss in 2008 uses void*. In fact, the void* inside can also be changed with a template, but it is because it is already very stable. There is no need to add new features. Secondly, because my boss no longer writes code, this file is the only code with his signature, so I have not changed it. In short, one of the biggest improvements of C++ over C is the addition of a lot of type-safe functions. It avoids the occurrence of blameless events during compilation (or runtime such as dynamic_cast), and the program runs more safely. From the perspective of type safety, I think that there is no human type conversion code is good code, I am trying to do this now. The human flesh conversion here includes C language style conversion as well as C++ style ***_cast. If any type conversion is required, then there is a design flaw. Of course, if it is unavoidable, it is recommended to isolate the code containing type conversion, put it in a separate module or file, and do a sufficient code review and write a complete unit test for it. The form is short s = 5; int i = 8888888; s = i; this narrow conversioin is also extremely dangerous, but modern C++ compilers will give warnings or even errors for this kind of code. Memory leaks and cross-borders are two major disasters in the C/C++ world. Regarding memory leaks, because the server can use mempool, in addition to not requiring delete, it can also avoid memory fragmentation and increase operating speed, so I have no special experience on how to deal with memory leaks.
 
to avoid memory leak:

use smart pointers where applicable.
use std::string instead of char *.
The std::string class handles all memory management internally, and it’s fast and well-optimized.
Never use a raw pointer unless it’s to interface with an older lib.
The best way to avoid memory leaks in C++ is to have as few new/delete calls at the program level as possible – ideally NONE.
Anything that requires dynamic memory should be buried inside an RAII object that releases the memory when it goes out of scope.
RAII allocate memory in constructor and release it in destructor, so that memory is garanteed to be deallocated when the variable leave the current scope.
Allocate memory by new keyword and deallocate memory by delete keyword and write all code between them.

and many more available in the internet to avoid memory leak.
 
to avoid memory leak:

use smart pointers where applicable.
use std::string instead of char *.
The std::string class handles all memory management internally, and it’s fast and well-optimized.
Never use a raw pointer unless it’s to interface with an older lib.
The best way to avoid memory leaks in C++ is to have as few new/delete calls at the program level as possible – ideally NONE.
Anything that requires dynamic memory should be buried inside an RAII object that releases the memory when it goes out of scope.
RAII allocate memory in constructor and release it in destructor, so that memory is garanteed to be deallocated when the variable leave the current scope.
Allocate memory by new keyword and deallocate memory by delete keyword and write all code between them.

and many more available in the internet to avoid memory leak.
Thank you .
 
I wouldn't like to see your code, I can barely read your sentences!
 
both these languages is C is a procedural programming language and does not support classes and objects, while C++ is a combination of both procedural and object-oriented programming languages. Otherwise, C++ supports both procedural and object oriented programming paradigms.
 
Are you asking a question or giving an opinion? In any case, void pointers in C are useful when you simply want to reference a location in memory. You wouldn't use one to point to a deer or a horse.
 
Both languages have similar syntax
C++ can be said a superset of C. Major added features in C++ are object oriented, exception handling and rich C++ Library.
 
I didn't actually read the thread, i wanted to, but my eyes hurt when seeing a giant text block without any spaces,
please use text formats to make the sentences clearer and more readable
 
Comparing c with c++
Is like comparing vaginal sex with anal sex.
 
Back
Top