Thread: Just another C vs C+ vs C++ debate

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Quote Originally Posted by Elysia View Post
    Proof, please.
    It is known that templates can cause bloat, but may not necessarily do so, and it's still a very, very poor argument.
    People's hard drives today are so big anyway that size hardly matters, mostly.
    I agree space is not a big problem but anyway the proof's here:

    "C++" style
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main() {
        cout << "Hello world!";
        return 0;
    }
    Result:
    Code:
    Output size is 269.50 KB
    "C" style
    Code:
    #include <stdio.h>
    
    int main() {
        puts("Hello world!");
        return 0;
    }
    Code:
    Output size is 5.50 KB
    Compile settings were exactly the same, I just changed the code.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by maxorator View Post
    Compile settings were exactly the same, I just changed the code.
    What if you used printf() which is far more equivalent to an ostream in terms of its formatting capabilities? And are you linking dynamically or statically?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Quote Originally Posted by brewbuck View Post
    What if you used printf() which is far more equivalent to an ostream in terms of its formatting capabilities? And are you linking dynamically or statically?
    printf() results the same size.

    And I was using MinGW.
    Last edited by maxorator; 01-12-2009 at 01:02 PM.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    gcc-mingw automatically links the libstdc++ as a static library, since it is not normally distributed to other machines, so your code would not necessarily work on those machines if it wasn't that way. Of course, if you link in the stdc library for you C executable, you would also end up with a fairly large executable. The latests MSVC version presumes that the library files are also installed on the target system, so you need to supply a few megabytes of DLL's if you distribute that code (unless you link statically, and then you end up with a LARGE executable).

    Obviously, producing similar code many times over, as template code CAN do, that isn't actually particularly different. Say for example we write a templated function that produces a string from a number - now, we could simply produce the exact same piece of code for both signed and unsigned integer, short int, unsigned short, etc, etc, and another for double and float values. That would produce (say) four variants of the integer code, essentially identical, and two variants of essentially identical code for the float/double variants. With some clever use of specialization, you could do:
    Code:
    std::string tostring(int x)
    {
       std::string result;
       if (x < 0)
       {
           result = '-';
           x = -x;
       }
       return result + tostring(static_cast<unsigned int>(x));
    }
    
    std::string tostring(short int x)
    {
        return tostring(static_cast<int>(x));
    }
    
    std::string tostring(unsigned short int x)
    {
        return tostring(static_cast<unsigned int>(x));
    }
    
    std::string tostring(float f)
    {
        return tostring(static_cast<double>(f));
    }
    Now, we only need to implement a full version of unsigned int and double variants of the tostring functions - which I'm too lazy to do here [and admittedly, it's probably not a lot of difference between this way and the expand everything variant - but I think it shows that you MAY be able to reduce the overhead from templates by thinking about it a little bit].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Who Won The Debate, and Why?
    By B0bDole in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 10-10-2004, 04:26 AM
  2. Oil debate continued
    By axon in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 07-24-2004, 02:22 AM
  3. Moderatorship Debate: Civix and Fordy.
    By civix in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 09-06-2002, 11:55 PM
  4. Ethics & Programming Debate
    By cozman in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 10-09-2001, 04:59 PM
  5. compiler/editor/ide debate
    By cozman in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 08-12-2001, 06:01 PM