CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Deploying Windows Server 2008 with System Center
  • Remote Desktop Protocol Performance Improvements in Windows Server 2008 R2 and Windows 7
  • The Microsoft Dynamics CRM Security Model
  • SQL Server Modeling Services with Microsoft Visual Studio 2010 Beta 2

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > C++ (Non Visual C++ Issues)
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    C++ (Non Visual C++ Issues) Ask or answer C and C++ questions not related to Visual C++. This includes Console programming, Linux programming, or general ANSI C++.

    Reply
     
    Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 1.00 average. Display Modes
      #1    
    Old August 6th, 2009, 01:53 AM
    umanga umanga is offline
    Member
     
    Join Date: Jun 2009
    Posts: 42
    umanga is an unknown quantity at this point (<10)
    Question C++ standards and good-practises ?

    Hi all,
    I want to know which standards i should follow (C++0x , TR1..etc?) if i want to continue further on C++ development.
    I came from heavy Java programming background and fairly new to C++ .I do almost all the development under linux/GCC (4>) .
    I want to know what kind good-practices I have to study more.For example ,instead of dynamic memory allocation for arrays,now I know I can use vectors,usage of smart pointers..etc.
    Since I came from Java , I tend to compare C++ features with Java.For example,I saw several GC libraries and confused about choosing correct one.
    What other good-practices I should learn?
    Is there a standard I should follow?

    Thanks in advance.
    Reply With Quote
      #2    
    Old August 6th, 2009, 02:02 AM
    laserlight laserlight is online now
    Elite Member
    Power Poster
     
    Join Date: Jan 2006
    Location: Singapore
    Posts: 4,123
    laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+) laserlight is a name known to all (1000+)
    Re: C++ standards and good-practises ?

    Quote:
    Originally Posted by umanga
    I want to know which standards i should follow (C++0x , TR1..etc?) if i want to continue further on C++ development.
    The current version of the C++ standard is the 2003 edition, which might be called C++03. C++0x refers to the next version of the C++ standard, originally expected to be finalised this year (or maybe last year, for those who were particularly optimistic). TR1 refers to a set of extensions to the standard library that is intended as a step towards future expansion of the standard library, but strictly speaking the TR1 components are not part of the standard library at the moment.

    Quote:
    Originally Posted by umanga
    Since I came from Java , I tend to compare C++ features with Java.For example,I saw several GC libraries and confused about choosing correct one.
    Typically, in C++, deterministic destruction to associate the lifetime of a resource with the lifetime of an object (the Resource Acquisition is Initialisation (RAII) idiom) is used instead of garbage collection. This is the basis of memory management with the standard (and other) containers and smart pointers.
    __________________
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful
    Reply With Quote
      #3    
    Old August 6th, 2009, 03:18 AM
    D_Drmmr's Avatar
    D_Drmmr D_Drmmr is offline
    Member
     
    Join Date: Jul 2005
    Location: Netherlands
    Posts: 442
    D_Drmmr is a jewel in the rough (200+) D_Drmmr is a jewel in the rough (200+) D_Drmmr is a jewel in the rough (200+)
    Re: C++ standards and good-practises ?

    Quote:
    Originally Posted by umanga View Post
    What other good-practices I should learn?
    Is there a standard I should follow?
    Most of the good practices are explained in a series of books called 'In depth C++ series'. There are also some other well-known and valuable books. Have a look at the FAQ section for a starter.
    __________________
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
    Reply With Quote
      #4    
    Old August 8th, 2009, 05:43 PM
    nuzzle nuzzle is online now
    Member +
     
    Join Date: May 2009
    Posts: 738
    nuzzle has a spectacular aura about (150+) nuzzle has a spectacular aura about (150+)
    Re: C++ standards and good-practises ?

    Quote:
    Originally Posted by umanga View Post
    Hi all,
    I want to know which standards i should follow (C++0x , TR1..etc?) if i want to continue further on C++ development.
    I came from heavy Java programming background and fairly new to C++ .I do almost all the development under linux/GCC (4>) .
    I want to know what kind good-practices I have to study more.For example ,instead of dynamic memory allocation for arrays,now I know I can use vectors,usage of smart pointers..etc.
    Since I came from Java , I tend to compare C++ features with Java.For example,I saw several GC libraries and confused about choosing correct one.
    What other good-practices I should learn?
    Is there a standard I should follow?
    C++ is different from Java (and C#) because it doesn't come with huge standard library. The C++ library is quite small actually and the best reference to it is The C++ Standard Library by Josuttis. But before you buy a copy you should know there's a new C++ standard pending so if you buy a copy, buy it used.

    There's an initiative called Boost. It's a kind of a testing ground for what would be good to have in the C++ standard. It's a very high quality library you should use rather than implementing the corresponding features yourself. And eventually many Boost classes make it into the C++ standard.

    http://www.boost.org/

    There's also a book called Beyond The C++ Standard Library by Karlson which discusses extensively some of the most important Boost classes.

    When it comes to good C++ usage there's an outstanding book called C++ Coding Standards by Sutter & Alexandrescu. The Java equivalent to this book would be Effective Java by Bloch.

    Finally it's better for you to treat C++ as a brand new language rather that looking back at Java. These languages may look alike because of the shared C syntax, but that's only superfluously. At a deeper level they're extremely different.
    Reply With Quote
      #5    
    Old August 8th, 2009, 06:08 PM
    Paul McKenzie Paul McKenzie is offline
    Elite Member
    Power Poster
     
    Join Date: Apr 1999
    Posts: 20,959
    Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+)
    Re: C++ standards and good-practises ?

    Quote:
    Originally Posted by umanga View Post
    Since I came from Java , I tend to compare C++ features with Java.
    No, do not compare the two languages. It is a big mistake to do so if you're just learning C++.

    The problem occurs when a Java programmer attempts to program in C++, but then uses Java as a model in programming C++. This lends to disastrous results at worst (buggy programs, memory leaks, etc.), and writing nonsensical, slow C++ code (though the code may work) at the least.
    Quote:
    For example,I saw several GC libraries and confused about choosing correct one.
    You should be learning how to write proper C++ programs without aid of a "garbage collector".

    Regards,

    Paul McKenzie
    Reply With Quote
      #6    
    Old August 8th, 2009, 08:02 PM
    nuzzle nuzzle is online now
    Member +
     
    Join Date: May 2009
    Posts: 738
    nuzzle has a spectacular aura about (150+) nuzzle has a spectacular aura about (150+)
    Re: C++ standards and good-practises ?

    Quote:
    Originally Posted by Paul McKenzie View Post
    You should be learning how to write proper C++ programs without aid of a "garbage collector".
    Why, if you have a C++ garbage collector at your disposal?
    Reply With Quote
      #7    
    Old August 8th, 2009, 09:09 PM
    Speedo Speedo is offline
    Member +
     
    Join Date: Aug 2007
    Posts: 736
    Speedo is a jewel in the rough (200+) Speedo is a jewel in the rough (200+) Speedo is a jewel in the rough (200+)
    Re: C++ standards and good-practises ?

    Learning how to manage memory, both manually and with methods like RAII, is an integral part of programming with C++. Even if you have a GC that you're ok with using, about 99.99% of the C++ code in existance doesn't use a GC.
    Reply With Quote
      #8    
    Old August 8th, 2009, 09:50 PM
    nuzzle nuzzle is online now
    Member +
     
    Join Date: May 2009
    Posts: 738
    nuzzle has a spectacular aura about (150+) nuzzle has a spectacular aura about (150+)
    Re: C++ standards and good-practises ?

    Quote:
    Originally Posted by Speedo View Post
    Learning how to manage memory, both manually and with methods like RAII, is an integral part of programming with C++. Even if you have a GC that you're ok with using, about 99.99% of the C++ code in existance doesn't use a GC.
    Doesn't learning how to manage C++ memory also include the possibility of a GC?

    What about smart pointers? Are they integral to C++? If so, why more so than a GC?

    Last edited by nuzzle; August 8th, 2009 at 10:13 PM.
    Reply With Quote
      #9    
    Old August 8th, 2009, 10:22 PM
    Speedo Speedo is offline
    Member +
     
    Join Date: Aug 2007
    Posts: 736
    Speedo is a jewel in the rough (200+) Speedo is a jewel in the rough (200+) Speedo is a jewel in the rough (200+)
    Re: C++ standards and good-practises ?

    Quote:
    Doesn't learning how to manage C++ memory also include the possibility of a GC?
    IMO, no. GC is not part of C++, so like any other 3rd party library you only learn it if you want to or need to.

    Quote:
    What about smart pointers? Are they integral to C++?
    They should be. They're fairly essential to managing memory in the C++ model. But of course C++ being what it is, there is no requirement to use them.

    Quote:
    If so, why more so than GC?
    They are part of the standard library. GC isn't.
    Reply With Quote
      #10    
    Old August 8th, 2009, 11:16 PM
    Paul McKenzie Paul McKenzie is offline
    Elite Member
    Power Poster
     
    Join Date: Apr 1999
    Posts: 20,959
    Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+) Paul McKenzie has a reputation beyond repute (3000+)
    Re: C++ standards and good-practises ?

    Quote:
    Originally Posted by nuzzle View Post
    Doesn't learning how to manage C++ memory also include the possibility of a GC?
    I have been programming in C++ for almost 17 years, and not once have I ever used a garbage collector or even had any inkling to "learn" to use one. In addition, in all of that time, I have yet to work on or even see a source code library written in C++ that uses garbage collection. Sorry, but GC just doesn't rank up there in something that is "important" in C++ repertoire.

    In addition, once you write a program that is tied to a garbage collector, you're stuck using that GC unless you rewrite the program to do memory management the normal way. If GC were standard in C++, then that's another story, but it isn't. C++ programmers are not going to tie an integral part of C++ (memory management) to something that is non-standard.

    What shold be learned is proper handling of dynamic memory, or in other words, how to avoid doing a lot of that work yourself and rely on properly built classes that handle the memory for you (i.e. RAII).
    Quote:
    What about smart pointers? Are they integral to C++?
    You have std::auto_ptr. It is standard C++. Garbage collection isn't standard.

    Regards,

    Paul McKenzie

    Last edited by Paul McKenzie; August 8th, 2009 at 11:22 PM.
    Reply With Quote
      #11    
    Old August 8th, 2009, 11:31 PM
    JVene JVene is offline
    Senior Member
     
    Join Date: Nov 2006
    Posts: 1,611
    JVene is a name known to all (1000+) JVene is a name known to all (1000+) JVene is a name known to all (1000+) JVene is a name known to all (1000+) JVene is a name known to all (1000+) JVene is a name known to all (1000+) JVene is a name known to all (1000+) JVene is a name known to all (1000+) JVene is a name known to all (1000+)
    Re: C++ standards and good-practises ?

    umanga,

    Along with the "In Depth" series, I also recommend the "Exceptional C++" series, and any good texts on design patterns.

    However, coding standards are a slightly different subject than these.

    MISRA, JSF++ or other formalized standards more accurately reflect the notion.

    You're really asking mostly about best practices vs known bad ideas.

    You've received good advice thus far. I suggest TR1 is a good idea, especially for shared_ptr.

    C++0x will take some time, so for now stay with those libraries which function well with your current compiler targets (TR1 qualifies).

    It's important to keep in mind why C++ and Java are different (to a Java developer studying and using C++). C was originally conceived as an architecturally independent assembler, and C++ retains that heritage. The focus is on performance and adaptability within that paradigm. From the beginning, the adaptation between this language and the underlying machine and operating system is through libraries (the standard C library started this convention). The language does not incorporate features and functionality directly, but through interfaces to libraries. Java and C#, among others, take the opposite view - that of incorporating ever increasing features within the language or it's model of the operating system or machine. A great many complaints are resolved if this is better understood.

    It causes me to inquire as to why you're now using C++?



    As the the point about garbage collection....it's a solution in search of a problem. Garbage collection may be central to languages as far back as BASIC, but in 30 years of development in C and C++, I've never had a reason to want for it. It is more often a bad idea or bad solution, because of the performance implications, but mostly, as has been suggested already, there are methods available which better solve the issues for which garbage collection is often indicated. Generally, memory fragmentation isn't the problem it is claimed to be, except in the tightest of memory operating circumstances - and specifically when available RAM is small, garbage collection is generally not as effective as better use of that RAM in the first place.

    In other words, garbage collection is better suited for chaotic memory usage problems (when usage isn't a problem, garbage collection is a solution for which there is no problem - thus it's in the way). However, if one uses custom memory allocation features available in the STL, or other similar measures, memory usage isn't chaotic, it's organized. There again, the need for garbage collection isn't strong. Consider that a great many high performance applications of considerable ambition don't use garbage collection, and don't appear to need it.


    Quote:
    Doesn't learning how to manage C++ memory also include the possibility of a GC?
    ...of the possibility, yes, but it is not a common solution. Garbage collection implies the relocation of memory, which does not happen "naturally" in the raw operation of the CPU - that is, at the level of an assembler. C and C++ language constructs do operate at the assembler level (they translate into one or two machine instructions generally). The relocation of memory is generally incompatible with the notion of using raw pointers under the paradigms of high performance application work.


    Quote:
    What about smart pointers? Are they integral to C++? If so, why more so than a GC?
    The language keywords, built in data types, language constructs like loops and switches, the creation of classes, virtual functions, even templates - these are all that's integral to C++.

    Concepts like RAII, design patterns, the STL - these are not exactly integral to C++ (even though the STL is standard, you can write C++ applications without it, even more so than you can write applications without the use of the C library).

    However, these concepts are to C++ what writing style is to English (or any spoken/written language). You must use several if not all of these concepts in order to write in C++ effectively.

    Smart pointers are no more integral to C++ than RAII, but they are among the most basic implementations of RAII - the control over the acquisition and release of memory allocation. You can write in C++ without them, but the leverage of increased reliability and productivity are obvious enough that choosing not to use them is all but a liability.

    Smart pointers were not widely available until the introduction of templates. Prior to templates it was nearly impossible to create a smart pointer (what was available required derivation from a common base class, and was difficult to use effectively). When templates were first created, smart pointers were among the first classes any fair C++ developer would create. Even naive implementations were useful. They perform a task which is very close to the native behavior of C, adding merely the RAII paradigm to allocation and de-allocation.

    On the other hand, garbage collection is not so native to C or C++. In most instances a form of smart pointer or representative object is best for a garbage collection library, because the application code need not be written all that differently from that which uses smart pointers without garbage collection, but the concept that pointers may be relocated at any time can be so foreign to some C or C++ coding methods as to create odd bugs and crashes.

    To put it simply, if the CPU doesn't do it, C and C++ are going to need a library to adapt to it - it is the nature of an assembler like language.


    In the case of C++/CLI an interesting solution is presented, but careful analysis of the approach reveals that what's happening is very similar to the implementation of a "smart pointer like handle", but instead of implementing that as a library, it's implemented within the compiler. The result is an uncomfortable "new" language (my opinion) which has a small following that might be better served if they worked in C# (though I realize it would also be the only way to provide C++ native code products TO .NET applications - a potential market).
    __________________
    If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).
    Reply With Quote
      #12    
    Old August 9th, 2009, 12:01 PM
    ninja9578 ninja9578 is offline
    Member +
     
    Join Date: Jan 2009
    Posts: 512
    ninja9578 will become famous soon enough (65+)
    Re: C++ standards and good-practises ?

    While C++0x isn't finalized yet, the GNU compiler already supports the majority of it's features. With g++ add the -std=c++0x or -std=gnu++0x compiler options.
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > C++ (Non Visual C++ Issues)


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 02:12 PM.



    Acceptable Use Policy


    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.