Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

clearsnake

A Must Read for the Absolute C++ n00b

This topic is 5275 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''ve been browsing through a lot of the questions and have been looking at some people''s code snippets and I''ve noticed that a lot of the C++ noobs on this board tend to make some pretty horrible coding decisions. For those people, please read this before you try writing something like a game: http://www.parashift.com/c++-faq-lite/ The author, Marshall Cline, talks about a lot of useful things including: 1) why you should be using standard library classes like string and vector and not character pointers and arrays 2) why you should avoid pointers whenever possible 3) why you should avoid macros whenever possible 4) good style (actually, the entire FAQ is, in a way, geared towards this ultimate goal) 5) how to properly inline functions 6) the big 3: destructor, copy constructor, assignment operator and their defaults 7) obscure stuff like the difference between a pointer to a static or global function and a pointer to a member function 8) why you shouldn''t mix new/malloc and delete/free and a lot more. Try reading his FAQ before you develop some hard-to-kick bad habits. In fact, some of you might want to skip directly to the "newbie section": http://www.parashift.com/c++-faq-lite/newbie.html And one more thing, because I''m not sure if Mr. Cline talks about it much, but a lot of you seem to have trouble with memory management and keeping your pointers straight. You should look into using smart pointers if this is you. The standard library has a template class called auto_ptr that acts as a pointer; except, you don''t have to worry about cleaning it up: it will clean itself up when it falls out of scope. If it hasn''t hit you yet, then you should be thinking about how auto_ptrs can help you with class members. 9 times out of 10, an auto_ptr is what you really want inside of a class. Honestly, how many times have you dynamically allocated memory inside an object and purposely wanted that allocated memory to outlive the object?

Share this post


Link to post
Share on other sites
Advertisement
quote:
Original post by Lawgiver
noob is not a word


Since you want to get technical about it. Taken from the first definition of "word" on Dictionary.com:

quote:

A sound or a combination of sounds, or its representation in writing or printing, that symbolizes and communicates a meaning and may consist of a single morpheme or of a combination of morphemes.



Just because you can''t find a definition for it in an English language dictionary does not mean it''s not a word.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I''m early in learning C++ and while I haven''t made any of the mistakes the author talks about, he''s got some good explanations I haven''t seen anywhere else yet. Thanks for the links, man.

BTW, I talked with a linguistics major once about the use and meaning of words. According to academic standards, as long as a word conveys the meaning correctly and this meaning is known within the context of social communication, it''s not incorrect. Proper English has only been proper for maybe 150 years.

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
I''m early in learning C++ and while I haven''t made any of the mistakes the author talks about, he''s got some good explanations I haven''t seen anywhere else yet. Thanks for the links, man.



Your welcome. I changed my ways quite a bit after one of my cs professors told me about it.

quote:

BTW, I talked with a linguistics major once about the use and meaning of words. According to academic standards, as long as a word conveys the meaning correctly and this meaning is known within the context of social communication, it''s not incorrect. Proper English has only been proper for maybe 150 years.


Yeah, I took a few linguistics courses to satisfy my social science requirements. Turns out that grammar is the same way. My professor said that the way people use the language is what determines what is and is not correct.

Share this post


Link to post
Share on other sites
er, I didn''t read the article cos I have enough trouble with the points you have stated.

Maybe he should say something like:
If you aren''t very familiar with the techniques listed you should avoid their use or learn them well.

Blanket statements like this ("you should") are almost always wrong because there are almost always acceptable exceptions. Agreed, points 2 and 3 have a "whenever possible" attached which makes them acceptable.

I personally only use C++ as an extension of C, and I don''t think anybody can say that doing that is wrong.

Pointers are extremely powerful because they are fast, flexible and can do many things that templated object references cannot do. Vectors have an overhead that makes them way less efficient than simple arrays (they undeniably take up more room because of their size variable, which makes a massive difference when you''re using 2d or 3d coordinates as arrays).

You may disagree with my specific complaints but a set of blanket concrete rules like 1-8 without an "unless you know what you''re doing" is immediately wrong if just one example of where the rules don''t apply can be shown. (such as a case where efficiency is important.

And yeah, noob is a word afaik.

Share this post


Link to post
Share on other sites
Good info; thanks for the site link. Just one thought:

Arrays are a lot faster than containers. Bounds-checking ought to be habit for any programmer worth his or her salt: if you overshoot your own array and can''t find the bug, I think you skipped one class too many. And since memory is plentiful whereas cycles are precious, arrays are a good way to go.

But the rest of your thoughts I agree with.

Share this post


Link to post
Share on other sites
quote:
"memory is plentiful but cycles are precious"


Maybe ten years ago. These days, memory ACCESS is what kills a program. If your memory read misses the cache, that''s a 500 cycle penalty with modern, high-bandwidth, high-latency DDR memories. Just because your CL is 2.0 doesn''t mean the CPU will get the data it needs in 2 CPU clocks (it''s a totally different measurement!)

This means that doing some extra computation to avoid an extra memory access is almost always a WIN. Cycles are really quite freely available, compared to memory accesses.

Thus, choose the container that causes you to miss the cache the least. Which, nine times out of ten, is some kind of array. Whether that array is a std::vector<> or a Foo[] is really up to you; they both have their uses.

Share this post


Link to post
Share on other sites
quote:
Original post by Krylloan
er, I didn''t read the article cos I have enough trouble with the points you have stated.

Maybe he should say something like:
If you aren''t very familiar with the techniques listed you should avoid their use or learn them well.



That probably would have been the better thing to say, actually.

quote:

Blanket statements like this ("you should") are almost always wrong because there are almost always acceptable exceptions. Agreed, points 2 and 3 have a "whenever possible" attached which makes them acceptable.



I think blanket statements like "you must" or "you should *always*" are almost always wrong. Saying "you should" implies that there are situations when you wouldn''t. I mean, that''s kind of obvious, imo.

quote:

I personally only use C++ as an extension of C, and I don''t think anybody can say that doing that is wrong.



It''s not wrong. I just happen to think it''s pointless. You might as well just program in pure C.

quote:

Pointers are extremely powerful because they are fast, flexible and can do many things that templated object references cannot do. Vectors have an overhead that makes them way less efficient than simple arrays (they undeniably take up more room because of their size variable, which makes a massive difference when you''re using 2d or 3d coordinates as arrays).



I probably should have clarified my take on arrays vs vectors more. Arrays are great when you know that the size of your list won''t change during the lifetime of your program. I mean, it makes perfect sense to use them in this case because of the fact that you have to specify their size at compile-time. But what bugs me is when people either start to dynamically allocate arrays or when they think of the largest number they can and write that as the array size because they *might* need the extra space. Then they''ll try to keep track of how many elements are currently in the array, adding and deleting elements from it, and etc. What they''re basically doing is rewriting the standard vector, except the standard vector is already a working (and most importantly, tested) implementation (and it''s standard).

quote:

You may disagree with my specific complaints but a set of blanket concrete rules like 1-8 without an "unless you know what you''re doing" is immediately wrong if just one example of where the rules don''t apply can be shown. (such as a case where efficiency is important.

And yeah, noob is a word afaik.


You might want to read over my post again. I said that those are some of the points that Cline talks about. I don''t know where you got this "blanket concrete rules" thing from. Please don''t twist my words. Just by saying the word "should" I am not making any concrete, universal assumptions. Therefore, you cannot somehow disprove me by providing counterexamples because I haven''t made any solid claims. Not even he lays his opinions out as concrete rules (although he sort of does in order to make his FAQ a more enjoyable read, but he explains why). I don''t know why people are so averse to reading.

You also might want to consider the subject of my post. This is mainly meant for people who are having trouble with things like keeping pointers straight i.e. people that should be thinking more about correctness than about efficiency.

Share this post


Link to post
Share on other sites
quote:
Original post by serratemplar
Arrays are a lot faster than containers. Bounds-checking ought to be habit for any programmer worth his or her salt: if you overshoot your own array and can''t find the bug, I think you skipped one class too many. And since memory is plentiful whereas cycles are precious, arrays are a good way to go.



Yes, arrays are a lot faster, and they''re also a lot more error prone, and that''s my point. And I disagree on your statement that bounds checking ought to be a habit. I''m of the opinion that bounds checking every time you use an array is tedious. It''s the kind of thing you''d want the computer to do for you, because computers were meant to do tedious work. So you''d write a class (or just use a class that somebody else wrote...vector comes to mind) to handle all that for you so you don''t have to think about it.

Bounds checking isn''t really the problem anyway. It''s handling pointers. It''s just something you shouldn''t always have to think about. It''s something you''d think about once when you write your vector class, then you''d never have to think about it again (insofar as using arrays as you would vectors is concerned, and if we''re talking about multidimensional arrays then you could just make a vector of vectors).

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!