-6

I know that there are things like malloc/free for C language, and new/using-a-destructor for memory management in C++ language, but I was wondering why there aren't "new updates" to these languages automatically memory management(garbage collection)? java have Garbage collection facility,why dont have C & C++

can anybody explain briefly for this?

  • Java is a "C Familly Language" not the other way round. It all started with `C` – DrKoch Apr 22 '15 at 06:43
  • "why there aren't "new updates" to these java languages" What? C and C++ are not Java. – Docteur Apr 22 '15 at 06:43
  • [FYI](http://stackoverflow.com/a/147137/2749470) – Bhargav Modi Apr 22 '15 at 06:44
  • `"new updates" to these languages that allow the user to have the option to manually manage memory`...what do you think `malloc()`/`free()` do? – Sourav Ghosh Apr 22 '15 at 06:48
  • The function malloc is used to allocate a certain amount of memory during the execution of a program. The malloc function will request a block of memory from the heap. If the request is granted, the operating system will reserve the requested amount of memory. When the amount of memory is not needed anymore, you must return it to the operating system by calling the function free.java have garbage collection to do automatically memory management but c & c++ languages dont have garbage collection why – vallepu veerendra kumar Apr 22 '15 at 07:19

4 Answers4

4

C and C++ were designed to be close to the metal and when they were designed garbage collection was barely an option. When you think of garbage collection then there has to be a data structure for reference counting and to track the allocations which in turn create overhead to memory and performance. And when C and C++ were designed these were not the objecives as it was more near to the hardware.

Bjarne Stroustrup said:

I had hoped that a garbage collector which could be optionally enabled would be part of C++0x, but there were enough technical problems that I have to make do with just a detailed specification of how such a collector integrates with the rest of the language, if provided. As is the case with essentially all C++0x features, an experimental implementation exists.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
2

C and C++ are historically relatively low level programming languages, meaning they leave a lot of responsibility with the developer for correct functioning. Fine-grained control over the allocation and deallocation of memory blocks is part of that, as garbage collection can have notable performance impacts, and is not always 'planned'. It also takes up overhead in costly resources as every allocated object needs to be reference counted and 'catalogued' for automated removal later on.

Java and C# are higher level languages, in that they sacrifice performance with things like garbage collection to offload the developer from those 'menial' tasks. This makes them more accessible for a broader array of tasks, and decreases development and debugging times for larger projects.

The end result is that all languages are different 'tools' in the programmer's toolbox. C and C++ incur little overhead, and are thus inherently highly capable of performance-intensive operations, such as drivers, low level OS code and game engines. C# and Java are more used for productivity software and web applications where split second performance is of less importance.

Niels Keurentjes
  • 41,402
  • 9
  • 98
  • 136
0

There are some implementations of garbage collection in C++ but the current consensus if you don't want to handle memory deallocation yourself is to use reference counting and similar techniques.

One of the main features and objectives of both those languages when they were (and still are) designed was to leave a lot of liberty to the user so even if a garbage collector would be to enter the new standard, it wouldn't be forced on the user. It just wouldn't fit with the philosophy of C and C++.

meneldal
  • 1,717
  • 1
  • 21
  • 30
0

Java was made for rapid-development. Memory management is basically outsourced to the JVM so that you can focus more on the problem rather than worrying about things like memory leaking. It may never be as efficient as its counterpart in C/C++, but you can get it off the ground and it's arguably easier to learn.