News for hackers. Stuff that matters.
Re:C++ Frequently Questioned Answers by timr on Monday May 11, 2009 @01:29AM (#603029)
"Unfortunately, you can't tell the class of a C++ object given a pointer to it at run time....which is why you can't automatically collect the garbage of C++ programs."

What nonsense. Here's what I learned from this link: trolling isn't as effective when it requires the trollee to read 10 pages of text.

This is garbage. Please flag it for collection.

Re:C++ Frequently Questioned Answers by DarkShikari on Monday May 11, 2009 @02:11AM (#603075)
Just because you disagree with something does not automatically make it a troll.
Re:C++ Frequently Questioned Answers by dave_au on Monday May 11, 2009 @04:16AM (#603173)
AFAIK it's reasonably well known as an unintentional troll - there was a stackoverflow question about the FQA (still looking for the link) that basically mooted almost every point.

I was reading it as a neutral party, however some very knowledgeable C++ appeared to convince all comers that the FQA is either based on ignorance or is disingenuous.

Re:C++ Frequently Questioned Answers by finnw on Monday May 11, 2009 @01:25PM (#603848)
I also once tried referencing the C++ FQA on stackoverflow and was quickly downvoted. Maybe SO is just the wrong place to mention it, probably because of a C++ bias from the high percentage of SO users who develop on MS platforms.
Re:C++ Frequently Questioned Answers by nickpp on Monday May 11, 2009 @04:31AM (#603181)
Maybe this is it:

http://stackoverflow.com/questions/385297/whats-wrong-with-c...

Summarizing: "C++ is hard but makes some things possible that aren't in other languages."

The FQA dude is a troll: he criticizes without offering better alternatives, ignoring tradeoffs and considerations and overlooking any advantages offered by C++.

Re:C++ Frequently Questioned Answers by timr on Monday May 11, 2009 @11:53AM (#603681)
My disagreement has nothing to do with it -- the quoted remark was completely without basis in fact.
Re:C++ Frequently Questioned Answers by kmavm on Monday May 11, 2009 @02:14AM (#603077)
"Unfortunately, you can't tell the class of a C++ object given a pointer to it at run time....which is why you can't automatically collect the garbage of C++ programs."

That's a somewhat self-serving use of ellipses. Here's the actual text:

"Unfortunately, you can't tell the class of a C++ object given a pointer to it at run time. So if you debug a crashed C++ program and find a pointer somewhere in its guts, and you don't know its type, you'll have to guess that "0000000600000005" is a Point. Which is completely obvious, because that's the way a pair of adjacent integers looks like in hexadecimal memory listings of a little endian 32 bit machine. And two adjacent integers might be a Point. Or some other two-integer structure. Or a part of a three-integer-and-a-float structure. Or they might be two unrelated numbers which just happen to be adjacent.

Which is why you can't automatically collect the garbage of C++ programs."

Which part are you claiming is wrong? (Be careful.) The entire first paragraph even Bjarne Stroustrup would agree with once he was done hemming and hawing. Post-mortem inference of types of dynamic objects in C++ programs is undecidable. In higher-level OO languages, memory is organized as framed objects, not as a bag of bytes. The main thrust of this questioned answer had nothing to do with GC.

And he's right about this being what makes GC and C++ bad fits for one another, too, though he skipped a few steps. Getting out the sock puppets, without the ability to tell the type of an in-memory object, you can't compact the heap. If you want to move an object, you can't just assume anything in memory with the bit pattern of that object's address is a pointer, because it might be two shorts or a double or whatever that happens to have that bit pattern.

While conservative, non-compacting garbage collectors can be built in theory, they're too slow in practice. Check out the source to Hans Boehm's C/C++ collector; it stops all the threads in the process, and analyzes liveness starting from each thread's CPU registers, each thread's entire stack, and the entire static data segment. By the implementors' own admission, this will not scale to large numbers of threads, or large address spaces. Even if this approach is refined significantly, if your garbage-collected program lives long enough, you need compacting garbage collection, or fragmentation will kill you just as surely as exhaustion would have.

And if you are so convinced that performant, general purpose GC can be dropped into large C++ codebases, you have a straightforward way of both shutting me up, and having a very interesting ycombinator startup. A drop-in operator new overload that does compacting, generational, safe GC would have a readymade market, with very deep pockets, from companies stuck maintaining enormous piles of C++ code with intractable memory management bugs.

Re:C++ Frequently Questioned Answers by timr on Monday May 11, 2009 @03:29AM (#603124)
I used ellipses because none of the quoted argument was correct, and there was no point making a long quote, when a short one would suffice.

First, the premise was false: you can tell the type of a pointer to a dynamic object -- you use RTTI. You don't have to "guess" that the object is a Point, because you can use dynamic_cast<>() or typeid() to tell for sure.

Second, the conclusion was false: it is possible to write a garbage collector for C++. You cited a very popular one in your response.

So what do we have? You can tell the type of an object at runtime, and you can write a garbage collector for C++. When both the premise and the conclusion of an argument are incorrect, well...let's just say that it's hard to be less correct than that. It's Not Even Wrong.

As for your defense, let's be clear: you're not suggesting that it's impossible to "automatically collect the garbage of C++ programs" -- you're arguing that it's impossible to do it efficiently. I dispute that argument, too, but it has absolutely nothing to do with the comment that I quoted. It's a straw-man.

The bottom line is that the quoted statement was absurd, and you're only setting up a straw-man (about the efficiency of C++ GCs, and about the ability to drop them into large codebases.) We could perhaps have an interesting discussion about those topics, but our discussion wouldn't make the original article any less of a flame-bait.

Re:C++ Frequently Questioned Answers by fhars on Monday May 11, 2009 @07:26AM (#603299)
So does C++ really guarantee that every valid pointer in a program will always point to the start of an object with an attached RTTI structure, and not for example somewhere into the middle of an array of integers that just happen to mimic the signature of an object with RTTI?
Re:C++ Frequently Questioned Answers by timr on Monday May 11, 2009 @05:44PM (#604395)
What does that have to do with the question of whether it's possible to infer types in C++ at runtime? What does it have to do with the question of whether it's possible to have garbage collection in C++?

This debate is becoming inane. I challenge you to name another language that allows for arbitrary references to blocks of memory, while still guaranteeing that the data type of said references can always be correctly inferred. You're just nit-picking.

Re:C++ Frequently Questioned Answers by calambrac on Monday May 11, 2009 @10:26PM (#604745)
I challenge you to name another language that allows for arbitrary references to blocks of memory, while still guaranteeing that the data type of said references can always be correctly inferred

That's exactly the point! Allowing arbitrary references to blocks of memory means inferring types is hard! That's exactly what the FQA entry is saying! ... I'm stunned at how adamant you're being about this. Do you really think the author doesn't know about RTTI? Seriously, dude, you're wrong.

Re:C++ Frequently Questioned Answers by dryicerx on Monday May 11, 2009 @10:30AM (#603510)
You decide where the pointer points, and how the compiler should start treating the data. That's the point of the pointer, you are given total control.
Re:C++ Frequently Questioned Answers by calambrac on Monday May 11, 2009 @01:46PM (#603876)
How does the existence of RTTI falsify the statement "you can't tell the class of a C++ object given a pointer to it at run time"? Not all objects are instances of classes with vtables.
Re:C++ Frequently Questioned Answers by timr on Monday May 11, 2009 @05:25PM (#604351)
Because RTTI lets you tell the class of a C++ object given a pointer to it at run time. The statement was false. This is not a subtle argument.

Again, you're trying to set up a straw man: the question is not whether or not it's efficient to do type inference in C++ (for the record: it's NOT any more efficient in a dynamic language), the question is whether it's possible at all.

It is possible to do so, the original argument was incorrect, and you're trying to have an entirely different debate.

Re:C++ Frequently Questioned Answers by calambrac on Monday May 11, 2009 @05:57PM (#604416)
Dude, RTTI only works for classes with virtual functions or their derivatives.
Re:C++ Frequently Questioned Answers by timr on Monday May 11, 2009 @06:26PM (#604464)
Yes, I know. You're arguing that not all classes have vtables. I'm arguing that there's no reason that they can't, save for efficiency concerns. And if you're so concerned about efficiency that vtables are a burden, then you're already in a situation where an interpreted language is inappropriate for your needs.
Re:C++ Frequently Questioned Answers by calambrac on Monday May 11, 2009 @06:42PM (#604484)
Now you're arguing something different. Just because all classes can, doesn't mean that they do, or that a general purpose tool (like, say, a garbage collector) can assume that they do. It's not a question of efficiency, it's a question of what the language allows. If you're saying "everything has to have a vtable", then you aren't talking about C++.
Re:C++ Frequently Questioned Answers by mrtron on Monday May 11, 2009 @01:52AM (#603053)
This is C++. Implement your own deconstructor, and manage your own damn memory.

I would probably use Boost's smart pointers to handle the reference counting though.

Re:C++ Frequently Questioned Answers by swolchok on Monday May 11, 2009 @02:12AM (#603076)
No, GP's point is that there are garbage collectors for C++. I believe Stroustrup even mentions one in The C++ Programming Kanguage. Furthermore, you CAN tell the type of an object given a pointer to it, assuming it has at least one virtual function, using the typeid operator. As the GP said, the original quote is nonsense.
Re:C++ Frequently Questioned Answers by calambrac on Monday May 11, 2009 @01:59PM (#603905)
assuming it has at least one virtual function

Which you can't do. Seriously, when did it become okay to call out a problem with the general case as nonsense because there's a specific case that avoids the issue?