Hot answers tagged

236

C predates many of the other languages you're thinking of. A lot of what we now know about how to make programming "safer" comes from experience with languages like C. Many of the safer languages that have come out since C rely on a larger runtime, a more complicated feature set and/or a virtual machine to achieve their goals. As a result, C has remained ...


93

The programmer is responsible for ensuring that objects they created via new are deleted via delete. If an object is created, but not destroyed before the last pointer or reference to it goes out of scope, it falls through the cracks and becomes a Memory Leak. Unfortunately for C, C++ and other languages which do not include a GC, this simply piles up over ...


78

C++ does not have garbage collection. C++ applications are required to dispose of their own garbage. C++ applications programmers are required to understand this. When they forget, the result is called a "memory leak".


61

Yes, it is a code smell (in lots of cases). I think it is difficult to replace if-else with virtual methods in tools In your example, it is quite simple to replace the if/else by virtual methods: class Tool{ public: virtual int GetAttack() const=0; virtual int GetDefense() const=0; }; class Sword : public Tool{ // ... public: virtual ...


40

First, C is a systems programming language. So, for example, if you write a Java virtual machine or a Python interpreter, you will need a systems programming language to write them in. Second, C provides performance that languages like Java and Python do not. Typically, high performance computing in Java and Python will use libraries written in a high-...


38

In C, C++ and other systems without a Garbage Collector, the developer is offered facilities by the language and its libraries to indicate when memory can be reclaimed. The most basic facility is automatic storage. Many times, the language itself ensures that items are disposed of: int global = 0; // automatic storage int foo(int a, int b) { static ...


29

Sorry to add yet another answer, but I don't think any of the existing answers directly address your first sentence stating: 'I am considering learning C' Why? Do you want to do the kinds of things C is usually used for today (e.g. device drivers, VMs, game engines, media libraries, embedded systems, OS kernels)? If yes, then yeah, sure learn C or C++ ...


22

The major problem with your code is, that whenever you introduce any new item, you not only have to write and update the item's code, you also have to modify your player (or wherever the item is used), which makes the whole thing a lot more complicated. As a general rule of thumb, I think it's always kinda fishy, when you can't rely on normal subclassing/...


22

C++ has this thing called RAII. Basically it means garbage gets cleaned up as you go rather than leave it in a pile and let the cleaner tidy up after you. (imagine me in my room watching the football - as I drink cans of beer and need new ones, the C++ way is to take the empty can to the bin on the way to the fridge, the C# way is to chuck it on the floor ...


21

It should be noted that it is, in the case of C++, a common misconception that "you need to do manual memory management". In fact, you don't usually do any memory management in your code. Fixed-size objects (with scope lifetime) In the vast majority of cases when you need an object, the object will have a defined lifetime in your program and is created on ...


18

There is nothing which is fundamentally flawed about this idea. What you have is two relationships. Boss owns one or more Workers. And Worker has a non-owning reference to a Boss. The use of a raw pointer suggests that Worker does not own the pointer it stores; it's merely using it. This means that it does not control that object's lifetime. There is ...


13

This is a HUGE question with tons of answers, but the short version is that each programming language is specialized for different situations. For example, JavaScript for web, C for low level stuff, C# for anything Windows, etc. It helps to know what you want to do once you know programming to decide what programming language to pick. To address your last ...


11

It is funny that you claim C is unsafer because "it has pointers". The opposite is true: Java and C# have practically only pointers (for non-native types). The most common error in Java is probably the Null Pointer Exception (cf. https://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare). The second most common error is ...


11

The C++ object lifecycle If you create local objects, you don't need to delete them: the compiler generates code to delete them automatically when the object goes out of scope If you use object pointers and create objects on the free store, then you have to take care of deleting the object when it's no longer needed (as you have described). Unfortunately,...


10

There is nothing that can guarantee that the code is compatible with a platform other than building it, running it, and testing it there. Therefore, the approach of all sane people is to build, run and test their application on every platform that they project it will need to be built, run, and test on. Continuous Integration (CI) can ease this burden a ...


9

Because "safety" costs speed, the "safer" languages perform at a slower speed. You ask why use a "dangerous" language like C or C++, have somebody write you a video driver or the like in Python or Java, etc. and see how you feel about "safety" :) Seriously though, you have to be as close to the core memory of the machine to be able to manipulate pixels, ...


9

Creating portable code can be very challenging. First some obvious language related advices: use standard C++ and avoid carefully any undefined behavior rely primarily on standard library (and portable libraries such as boost) always include all expected headers. Do not assume that you don't need a header because it's included in another one (i.e.on ...


8

A fundamental difficulty with C is that the name is used to describe a number of dialects with identical syntax but very different semantics. Some dialects are much safer than others. In C as originally designed by Dennis Ritchie, C statements would generally be mapped to machine instructions in predictable fashion. Because C could run on processors which ...


7

Besides all the above, there is also one pretty common use case, which is using C as a common library for other languages. Basically, nearly all the languages have an API interface to C. Simple example, try to create a common application for Linux/IOS/Android/Windows. Besides all the tools that are out there, what we ended up was doing a core library in C, ...


7

With respect to C specifically, the language gives you no tools to manage dynamically-allocated memory. You are absolutely responsible for making sure every *alloc has a corresponding free somewhere. Where things get really nasty is when a resource allocation fails midway through; do you try again, do you roll back and start over from the beginning, do ...


7

If you are asking for "development processes" and you primary development platform is Windows with Visual Studio then I would suggest to try building your project without "windows.h" included. You will get a lot of compilation errors that will point you to many places where you'll need to refactor your code. For example, 'DWORD' won't be #defined and you'll ...


6

You're asking the wrong question. The right question is "who should be responsible for ensuring that the desired values are within the limitations required by the data type?" Let's look at your cases: void function1(int16_t health, int8_t mana) { Object object; object.health = health; object.mana = mana; } void function2(int health, int mana) { ...


6

While you may have 4 CPUs, you have only one hard-drive (unless you don't). The total performance will therefore be limited by your disk drive's read/write rate. Multiple threads isn't going to change that. Having a single separate thread handle all the file IO will allow your application to remain responsive while still getting things done asynchronously. ...


5

Historical reasons. I don't often get to write brand new code, mostly I get to maintain and extend the old stuff which has been running for decades. I'm just happy it's C and not Fortran. I can get irritated when some student says, "but why on earth do you do this awful X when you could be doing Y?". Well, X is the job I've got and it pays the bills very ...


5

Short answer: the same way any other compiler works. Long answer: A program that takes programming code input in one language and transforms it to output in a different language is called a compiler. (An assembler is a special type of compiler whose input language is assembly language and whose output language is machine code.) A compiler's work can be ...


5

It depends, as usual. Generally, it's best practice to split each class into a header (.h or .hpp) and source (.cpp) file, where you put everything you can in the source file*, because it considerably speeds up building your program. This is incredibly important in real world development because large projects can take anywhere from 15 minutes to several ...


5

While @DocBrown has given a good answer, it doesn't go far enough, imho. Before you start evaluating the answers, you should evaluate your needs. What do you really need? Below I will show two possible solutions, which offer different advantages for different needs. The first is very simplistic and tailored specifically to what you have shown: class Tool {...


5

If the language supports it, I'd opt for: f = b ? foo : bar; for (auto a : as) { f(a); } This approach avoids worrying about whether the if is repeatedly evaluated and avoids code repetition.


4

Your int is 32 bits wide, so it needs 4 bytes of storage space. The pointer data type is smart enough to know this, so incrementing it by 1 actually points 4 memory cells farther down the memory, and that's what you see in the output.


4

It's not a git problem at all. In fact, it has nothing to do with Git or even a program you're trying to install. It's just a version conflict. To solve this type of problems, there are many possible approaches: Installing each version in a separate virtual machine ... in a separate Docker containers Using smart package managers like nix which would allow ...



Only top voted, non community-wiki answers of a minimum length are eligible