4 ms·
"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++ pr
by timr 18y ago
"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.
- mrtron 18y agoThis 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.
- swolchok 18y agoNo, 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.
- calambrac 18y agoassuming 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?
- DarkShikari 18y agoJust because you disagree with something does not automatically make it a troll.
- deleted 18y ago[deleted]
- dave_au 18y agoAFAIK 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.
- nickpp 18y agoMaybe this is it: http://stackoverflow.com/questions/385297/whats-wrong-with-c-compared-to-other-languages 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++.
- finnw 18y agoI 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.
- timr 18y agoMy disagreement has nothing to do with it -- the quoted remark was completely without basis in fact.
- kmavm 18y ago"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.