Just another C vs C+ vs C++ debate

This is a discussion on Just another C vs C+ vs C++ debate within the A Brief History of Cprogramming.com forums, part of the Community Boards category; Originally Posted by Elysia Proof, please. It is known that templates can cause bloat, but may not necessarily do so, ...

  1. #16
    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. #17
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,249
    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. #18
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,788
    Visual Studio:
    C version: 7 KB
    C++ version: 8.5 KB
    Hardly code bloat.

    EDIT: Result is the same with both printf and puts.
    Last edited by Elysia; 01-12-2009 at 11:55 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,935
    Quote Originally Posted by maxorator
    I agree space is not a big problem but anyway the proof's here:
    Stroustrup has an FAQ on this: Why is the code generated for the "Hello world" program ten times larger for C++ than for C?

    Then again, your point that "it doesn't make a difference on larger projects anyway" makes sense either way.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    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 12:02 PM.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  6. #21
    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.

  7. #22
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Quote Originally Posted by laserlight View Post
    Stroustrup has an FAQ on this: Why is the code generated for the "Hello world" program ten times larger for C++ than for C?

    Then again, your point that "it doesn't make a difference on larger projects anyway" makes sense either way.
    Quote Originally Posted by Bjarne Stroustrup
    I tested using gcc -o2 on a Unix and the two versions
    Who wants to be a peon, and tell him that he made a typo?

  8. #23
    Banned
    Join Date
    Jan 2009
    Posts
    30
    Is a knife obsolete for making woodwork? Or should we all stick to sanders and lathes? I think you can see the relation to more simple tools to more advanced ones. I still have screw drivers. Sometimes its just less work than charging up a power tool.

  9. #24
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Quote Originally Posted by sphynxter View Post
    Is a knife obsolete for making woodwork? Or should we all stick to sanders and lathes? I think you can see the relation to more simple tools to more advanced ones. I still have screw drivers. Sometimes its just less work than charging up a power tool.
    But you speak of hobbyists (I am not implying you are a hobby programmer) who just do odd jobs, not of professionals who are faced not with the task of sanding down a little knick-knack but (to carry your analogy forward) of creating cabinets and furniture; when they get one project done, they move on to another, and so on. And furthermore, for the professional, time is of the essence, because time is money. You only get paid for what you produce so, in general terms, the more that is produced, the more money that is made.

    That said, I am in a more or less contentious mood at the moment, and so I would add that your point is noted.
    Last edited by kermit; 01-12-2009 at 07:54 PM.

  10. #25
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,935
    Quote Originally Posted by sphynxter
    Is a knife obsolete for making woodwork? Or should we all stick to sanders and lathes? I think you can see the relation to more simple tools to more advanced ones. I still have screw drivers. Sometimes its just less work than charging up a power tool.
    Neither C nor C++ is only a knife/screwdriver or only a power tool. It just so happens that the C++ toolkit comes with more power tools while retaining the more sensitive equipment of C. Consequently, I agree with Stroustrup's opinion as stated in the FAQ: C is better than C++ for small projects, right?
    Last edited by laserlight; 01-12-2009 at 10:06 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #26
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,739
    On this topic (somewhat) ... I thought C++'s ability to compile C89 was supposed to be a feature. Of course I get blasted when I tried to discuss things with Elysia earlier. It's my sincere hope that my original messages are understood as I intended them to be: I felt that others were doing a sufficient job ridiculing the book and I merely wanted to quiet Elysia's evangelism.

    The topic is stale now.

    But I have to say that if using C++ as a better C is worthy of such scorn, then I don't see why it is tolerated by the compiler anymore. It seems like something that should have ran its course long ago, rather than shifting the blame on the person using the tool. I'm probably just perturbed by Elysia's example. I'll get over it.

  12. #27
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    501
    Ironically,Stroustrup himself had said that language comparisons are rarely meaningful.
    http://www.research.att.com/~bs/bs_faq.html#compare
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  13. #28
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,788
    Quote Originally Posted by whiteflags View Post
    But I have to say that if using C++ as a better C is worthy of such scorn, then I don't see why it is tolerated by the compiler anymore. It seems like something that should have ran its course long ago, rather than shifting the blame on the person using the tool. I'm probably just perturbed by Elysia's example. I'll get over it.
    Using C++ as a better can be done true, but I wanted to say my piece, it would be done when the more advanced features cannot be used for some reason.
    Like why use a screw driver when you have an automatic screw driver?
    Further, I would like to add, as to the original topic, that a good, modern C++ book should teach modern C++ and not C with C++, which is again, why I wanted the OP to actually try out the rest of the language instead of feeling it's unnecessary.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 2 of 2 FirstFirst 12
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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21