Is it possible to introduce Automatic Reference Co

2019-03-12 04:42发布

Objective C has introduced a technology called ARC to free the developer from the burden of memory management. It sounds great, I think C++ developers would be very happy if g++ also has this feature.

ARC allows you to put the burden of memory management on the (Apple LLVM 3.0) compiler, and never think about retain, release and autorelease ever again

So, if LLVM3.0 can do that, I think g++ also can free C++ developers from the tough jobs of memory management, right?

Is there any difficulties to introduce ARC to C++?

What I mean is: If we don't use smart pointers, we just use new/new[], is it possible for a compiler to do something for us to prevent memory leaks? For example, change the new to a smart pointer automatically?

9条回答
\"骚年 ilove
2楼-- · 2019-03-12 05:18

Recently I wrote some Objective-C++ code using Clang and was surprised to find that Objective-C pointers were actually handled as non-POD types in C++ that I could use in my C++ classes without issues.
They were actually freed automatically in my destructors!
I used this to store weak references in std::vectors because I couldn't think of a way to hold an NSArrary of weak references..
Anyways, it seems to me like Clang implements ARC in Objective-C by emulating C++ RAII and smart pointers in Objective-C. When you think about it, every NSObject* in ARC is just a smart pointer (intrusive_ptr from Boost) in C++.
The only difference I can see between ARC and smart pointers is that ARC is built into the language. They have the same semantics besides that.

查看更多
手持菜刀,她持情操
3楼-- · 2019-03-12 05:19

What's the advantage of using ARC rather than full garbage collection? There was a concrete proposal for garbage collection before the committee; in the end, it wasn't handled because of lack of time, but there seems to be a majority of the committee (if not truly a consensus) in favor of adding garbage collection to C++.

Globally, reference counting is a poor substitute for true garbage collection: it's expensive in terms of run time, and it needs special code to handle cycles. It's applicable in specific limited cases, however, and C++ offers it via std::shared_ptr, at the request of the programmer, when he knows it's applicable.

查看更多
The star\"
4楼-- · 2019-03-12 05:28

One of the reasons C++ is used at all is full control over memory management. If you don't want that in a particular situation there are smart pointers to do the managing for you.

Managed memory solutions exist, but in the situation C++ is chosen rightfully (for large-scale big applications), it is not a viable option.

查看更多
Juvenile、少年°
5楼-- · 2019-03-12 05:28

Microsoft C++/CX has ARC for ref classes. Embarcadero has 2 C++ compilers, one of them has ARC.

查看更多
时光不老,我们不散
6楼-- · 2019-03-12 05:32

Take a look of Qt. Qt has implemented this feature by leverage the hierarchy chain. You can new a pointer and assign a parent to it, Qt will help you to manage the memory.

查看更多
女痞
7楼-- · 2019-03-12 05:32
  1. There are already some implementations of similar technologies for C++; e.g., Boehm-Demers-Weiser garbage collector.
  2. C++11 has a special Application Binary Interface for anyone wishing to add her own garbage collection.
  3. In the vast majority of cases, techniques like smart pointers can do the job of painless memory management for C++ developers.
查看更多
登录 后发表回答