Follow Slashdot blog updates by subscribing to our blog RSS feed

 



Forgot your password?
typodupeerror
×
Programming Software IT Technology

Tools for Analyzing C++ Class Code Generation? 48

Milo_Mindbender submits this query: "I've got a midsize Linux project which uses a lot of STL and other C++ template code. Even considering this, I end up with a lot bigger text (generated code) segment than expected. I know the information about the amount of code generated for each class is in the objdump, but prying it out by hand is a problem when you get five line long template invocations and hundreds of methods to wade through. Can anyone can recommend some tools that analyze binary or objdump output and summarize the amount of code generated for each class, including each unique template or STL class?"
This discussion has been archived. No new comments can be posted.

Tools for Analyzing C++ Class Code Generation?

Comments Filter:
  • by GigsVT ( 208848 ) on Friday September 05, 2003 @11:26PM (#6885580) Journal
    "Why you shouldn't use OOP for anything important."
    • by be-fan ( 61476 ) on Friday September 05, 2003 @11:32PM (#6885607)
      Too bad that C++ templates have nothing to do with OOP, but rather procedural programming. And while they have lots of problems (like code bloat) they are also phenomenally powerful for certain things (generic data structures). Even if you write nothing about procedural code, you could benifet from templates.
      • Of course, that second "procedural" should be "generic" :)
      • Yes, you can use simple templates with procedural programming. In that sense, knowledge of OOP is not needed to use templates. But, there are ways in which templates make OOP easier.

        Templates can be combined nicely with several design patterns, such as observer, visitor, factory, and iterator. Template base classes for each of these design patterns can provide the common behaviors and implementations of these design patterns.

        In addition to augmenting traditional OOP, templated classes can still have a
    • It'd be interesting to hear what you'd use, given that you fail to grasp that a template has nothing to do with OOP.
  • I want to write an insanely clever and informative post and answer Milo's question in a way which would cause the calculating hordes of Slashdot moderaters to moderate my post to Score:5, Astounding!

    Unfortunately, I've never used C++ before in my life. (except for a "Hello World!" back in High School)
  • Me Too! (Score:4, Funny)

    by kzadot ( 249737 ) on Saturday September 06, 2003 @12:12AM (#6885760)
    Our large corporation also has a project, a client has requested that we implement BubbleSort, QuickSort, ShellSort, and one other sort that we learnt in cl^H^H^H^H^H^H^H^H^H err, discussed in a business meeting. The err, client also requests a 1 page analysis comparing and contrasting the differences between the searches.

    Can code generation do this? It could really increase my chances of a good mar^H^H^H err profit yeah.
  • There's a very cheap STL project visualization tool, made by a company called "Prince." Their fine visualization tools are available in the pasta aisle of your favorite grocer. Simply bring water to boiling, stir in the Visualization Tool, and soon you'll have a representation of the calling methods employed by the average STL application.

    For each additional programmer involved in the project, double the contents by stirring in a random different kind of Visualization Tool.

    If you're targeting multiple arc

  • by ComputerSlicer23 ( 516509 ) on Saturday September 06, 2003 @12:28AM (#6885807)
    Uhhh, use your favorite scripting language. Doing the demangle might be a bit tricky, but nm or obj dump should do either of those for you. Then just parse the class names.

    Use a map or a hash. Oh, and lastly, move all the code in headers out into the .cxx/.cpp file, your code will compile faster, and probably run nearly as fast, with the exception of a handful of performance critical classes (finding them is important).

    Kirby

    • To demangle, use c++filt [gnu.org].
    • How much do you use C++? :) g++ is quite intellegant about what is / isn't worth inlining. Also template code MUST go in headers, because it has to be included in every file that makes use of it, because templates are retarded... :(
      • I've written C++ for a living for about 5 years, and I'm reasonable well read about the subject.... I didn't consider that it was all templated code (I was thinking about cutting down the binary size, I rarely use templates). I use g++ for a living, every day of the week. It might be pretty smart about it, but I all I know is that it takes about 5 times as long to compile the code using 2.96 as when you leave things in the header. The binaries are twice the size, and if you do it while leaving optimizati
  • Nope (Score:1, Informative)

    by joto ( 134244 )
    I've got a midsize Linux project which uses a lot of STL and other C++ template code. Even considering this, I end up with a lot bigger text (generated code) segment than expected.

    Huh? What did you expect? Templates lead to bloat per definition. That's what they do: a lot of binary code is generated by a small amount of typing. If bloat becomes a problem: redesign.

    I know the information about the amount of code generated for each class is in the objdump, but prying it out by hand is a problem when you

  • OpenC++ (Score:5, Interesting)

    by angel'o'sphere ( 80593 ) <angelo.schneider ... e ['oom' in gap]> on Saturday September 06, 2003 @10:24AM (#6887323) Journal
    Search at source forge for Open C++ or OpenCPP, not sure how it is written.

    That is a programmable source to source transformer for C++.

    It spits out one singel file of c++ for every compiled C++ unit, removing the includes, by expanding them into the output.

    That way yo can source level debug the processed c++ code, not exactly what you want but likely more powerfull in the long run anyway.

    Your request varies far to heavy from compiler to compiler.

    angel'o'sphere
  • John Lakos (Score:4, Informative)

    by sohp ( 22984 ) <snewton.io@com> on Saturday September 06, 2003 @11:12AM (#6887602) Homepage
    If you do not already have a copy of Large-Scale C++ Software Design by John Lakos, get it. I realize this isn't a tool per se, but he has a terrific section in chapter 10 on Using C++ Templates in Large Projects.
    • I second that. Great book. I don't use templates much so I can't speak to that particular chapter, but his other advice is sound.
  • Turn off inlining (Score:2, Interesting)

    by Anonymous Coward
    This is more of a suggestion than a debugging tool but...

    You didn't say which compiler you were using but, for example, with a recent g++, try turning off some of the inlining and turn on -frepo. This will make sure that, for example, there is only ever one copy of map::find.

    I don't know; maybe the latest g++ does a good job of this already.
  • by mattgreen ( 701203 ) on Saturday September 06, 2003 @12:02PM (#6887868)
    Enough uninformed comments already. Most STL code bloat can be controlled pretty easily. What you do is make a wrapper over an STL class. For example if you're using std::vector you'd make a Vector class:
    template<typename T>
    class Vector
    {
    public:
    //ctors as necessary
    Vector();

    T get(size_t i)
    {
    return static_cast<T>(vec[i]);
    }

    void insert(T item)
    {
    vec.push_back(static_cast<void*>(item));
    }

    pri vate:
    std::vector<void*> vec;
    };
    You may wish to provide the exact functionality of the container you're wrapping, or you may wish to change the semantics. It's up to you.

    It is kinda ugly for what it does but it works. Only one instantiation of std::vector is made, it is void*. Putting the member function definitions in the header file ensures they will be inlined if necessary.

    I think Scott Meyers came up with this tip first.
    • Yes it was Scott Meyers and it works great!
    • by saurik ( 37804 ) on Saturday September 06, 2003 @02:50PM (#6888861) Homepage
      If your data type really can be static_cast'd to a void * then there's probably a much better solution to this problem in almost 90% of the cases: it's called a "better compiler". I don't know what compiler you are using, but Visual Studio .NET finds functions whose assembly code generated the same and merges them (or whatever you want to call it: links all usages to the same implementation) at link time.

      So if you have a vector of int * and a vector of long * and a vector of MyObject * and a vector of unsigned long they should all generate the same assembly code and you should only end up with a single set of these in your resulting binary.
      • News to me. The Effective C++ books are somewhat dated, but I hadn't heard of compilers being able to do this.
      • If your data type really can be static_cast'd to a void * then there's probably a much better solution to this problem in almost 90% of the cases: it's called a "better compiler".

        Alternately, it's called a vector implementation that has a partial specialization for pointers, like the example in Stroustrup.

    • I think you mean
      T get(size_t i)
      {
      return *static_cast<T*>(vec[i]);
      }

      void insert(T item)
      {
      vec.push_back(static_cast<void*>(new T(item)));
      }
      You also need a destructor that walks the vector and calls delete() on each pointer-to-void.
      • You also need a destructor that walks the vector and calls delete() on each pointer-to-void.

        Calling delete or delete[] on a void pointer is not safe. A void pointer is opaque, and so how would the compiler know which delete function to call? Each class can have its own delete and delete [] operators. You can get around this problem by casting the void pointer to the original type before deleting it.

        If you don't have delete and delete [] operators for your classes, but always use the global delete a

    • Many of the STL classes do this already...it's called pointer specialization.
    • Huh? Any half-decent implementation of the standard library will already have a specialization of vector for pointer types so that all pointer types use the same object code. And things that aren't pointers cannot safely (and portably) be cast to void* and back again.
    • Are you retarded?

      You'll just be creating multiple instances of Vector for each type. You save nothing. The STL classes are not that large at all. Most of the vector functions are really small and by the time you wrap them all you'll have the same size wrapper class.
  • ...

    {} - The empty sig
  • Having used C++ templates extensively, I can say that compiler-generated code is a bad idea. A compiler should generate assembly, not other code. That said, I really like the *idea* of C++ templates, yet they fail in a number of subtle ways, not the least of which is that if the implementer of the STL construct screws up, you're screwed. Personally, I rather like keeping my own classes (containers and all) under my own control, so, when (not if) there's a bug, I can put in my own debug statements to trac
    • I totally agree with the poster. C++ (and most other programming languages) are implementation languages. Most programmers are not aware of the fact that the main reason why we use implementation languages lies in the fact that we have to produce highly optimized code to make computers perform. Specification languages are hardly used, because we lack the proper tools to transform (formal) specification into implementation code. The reason behind this is that much of the engineering in "software engineering"
  • STL: that bad? (Score:2, Insightful)

    by cronie ( 698178 )
    Just wanted to bring your kind attention to this amazing fact: everyone tries to handle STL code bloat, is looking for a (inexistent) STL debugging tool, suggests to completely re-design compiling principles just to make STL compile a bit better, writes books about good STL coding (how to make your binary 3 times smaller), rends the air by saying "do you know what 'S' means in STL????" (one of slashdot postings)...

    So why use it at all? Just because it is standard? Then go program with Fortran or Ada, they
    • I'll take STL containers over anyone's home-brewed containers any day. Do your containers work with the functional include file? Do they support forward and reverse iteration? Do they even support iterators? Probably not. STL has consistant semantics. If you're the type of person that doesn't want to learn anything new then you don't have to use them.
      • I'm definitely not that kind of person :)

        I'm afraid STL is responsible for discrediting the C++ language and for the fact that C is still in wide use novadays, whereas it should have been replaced with its successor, C++. In C vs C++ wars people usually argue just around efficiency. It is obvious (to me at least) that the C++ language itself with good OO design can produce sound code. Only when it comes to STL and templates in general you easily loose control over code generation and also source code porta
        • Re:STL: that bad? (Score:3, Interesting)

          by bmac ( 51623 )
          Yup, STL is *partly* responsible for C++ being mostly worthless, but let's not forget the other big nail in C++'s coffin: multiple-inheritance. The ol' diamond of death means compiler-determined (and not in any kind of spec, that I know of, tho I may be wrong) implementation, which is an auto-no-go in my book. Then again, I've *never* seen a practical reason to use multiple inheritance. Most books just use some assinine example like animals, mammals, and cows. WTF?! I use C because it's deterministic,
          • Yes, but... (Score:3, Insightful)

            by Nicolay77 ( 258497 )
            I've seen template bloat in a lot of places, but MI bloat?

            Just because something can be done in the language doesn't mean everybody just goes and uses it. You're right that MI is too prone to mistakes, and it can be replaced most of the time by java-like interfaces.

            I use C++ because is deterministic, can be used to really big proyects, when it runs is fast as hell (can be faster than C when using inline functions), and because I don't use every shitty feature of the language just because it's there. The r
            • Ah, grasshoppa, you understand :-)

              I'd still like a clean C++ compiler that
              doesn't include all the cruft necessary
              to implement templates & MI, tho. I think
              that would streamline things even more.
              And yes, I do desire the inline directive
              for my C code. I mean, c'mon, it has to
              be pretty easy, right, especially if they
              can do all the MI and template crap, what's
              a little parse tree substitution and one
              more keyword :-)

              Peace & Blessings,
              bmac
              www.mihr.com -- True peace & happiness
              are just a wish away...
              • Actually I'd leave MI but without virtual bases, which cause this "MI bloat". I do use MI sometimes and I can't convince myself that I could have avoided it.

                I'd also leave templates, they are useful and potentially powerful, but should be used with great care - that's it.

                He-he, so your circumcised compiler gets back to life, almost.

                Just don't use STL, and leave the compiler alone :)
    • Maybe aerospace software engineers have something to say about Ada not being practical. Probably similar to what the (real) engineers would say about your comment about FORTRAN. The fact is most engineering/scientific software (think PDE solvers, etc) is available and maintained in FORTRAN. There is no real alternative to BLAS (Basic Linear Algebra System) for example. It's often free too.

      STL is incredibly useful, but like many powerful tools it is not 100% intuitive to use effectively at first go. It is e
      • ...and not all compilers are optimized to remove redundant code, but such compilers are becoming available.

        This reminds me the situation with Java: Sun promised to its army of Java fans that there would be hardware Java VM by 2000. Sun failed to implement Java processor but the trick worked and Java is still popular.

        So where are those compilers?

    • These is the point where teory and practice met. And in theory STL does everything right and good once for all. But in practice there is MFC, Qt, wxWin, and so on for real programs, because STL is still a teoric tool unless you are the writer of the STL itself and know how to debug templates.

      I believe that mastering templates is more complex and long than mastering everything else in the C++ language. And that's their main pitfall, despite being absolutely powerfull and even turing complete (just see some

"The whole problem with the world is that fools and fanatics are always so certain of themselves, but wiser people so full of doubts." -- Bertrand Russell

Working...