views:

830

answers:

14

While I agree reading books is always a learning experience, sometimes you'd like to skip the theory and just jump till you reach the practical aspects. For example, I'd like to see a book that tells me that by writing:

char* a = "a string";

the value of a will (usually) get stored in the readonly portion of an executable, and explain what happens if you try to modify it. I'd like a book that explains why sometimes you need to cast something to a type, then to another, before dereferencing it, or when it all boils down to machine code, what will the difference between references and pointers be.

I'm sure there are other numerous language gotchas and quirks that you only encounter while doing practical stuff, this is why I'd like to find such a book. I hope you can point me towards one.

If it matters, I have previous programming experience, but my area of expertise are higher-level languages.

+22  A: 

Effective C++ and More effective C++ by Scott Meyers. Although it's a very enlightening book, it's useful only if you know a little C++. The book explains many special cases, gotchas and coding best practices gained after years of experience. Although, I'm afraid it might not mention things like the one in your example, cause these scenarios are usually known to C++ programmers. Edit: Adding Effective STL on public demand and yes of course C++ FAQ lite.

Samrat Patil
+1 from me. Scott Meyers is essential reading.
duffymo
Add Effective STL to the list.
DevSolar
+1. Effective C++ offers exactly what the OP is asking.
StackedCrooked
+3  A: 

I think Accelerated C++ has what you're looking for.

RobbR
It's a really good book, but doesn't deal much with quirks and gotchas, which was what the question was about (the title of the question is quite misleading - Accelerated C++ indeed *is* a very practical C++ book).
Joonas Pulakka
+6  A: 

If you want to find out about that, you want to start understanding compilers and assembler. Once you look at assembler, many of these details become clear.

In your particular case, the value of a is probably on the stack (or certainly in mutable memory), and it contains a value that points to constant data. This is part of "understanding" pointers -> "a" is a pointer, and it points to data. The characteristics of the pointer are different to the characteristics of the thing it points to.

janm
Why do you think it would be on the stack? What use would that be? What compiler would do that?
xtofl
I agree with your first remark though!
xtofl
@xtofl: the pointer a is on the stack when declared in a function. You're problably referring to pointer a being declared outside of functions, in which case a is in the static data.
stefaanv
As stefaanv says, the value of "a" is on the stack when declared in a function, and when declared outside of a function it as in the question it is global. In both cases it is mutable, not "read only" as described in the question. If the definition were 'char const a[] = "a string";', then things would be different. In that case "a" would be a symbol pointing to part of the executable image and it would be read only. The point of my answer is that much of understanding C is understanding that kind of distinction.
janm
A: 

Code Complete

Paul Mitchell
Although it's an equally good read to Mayers, it's focus is on general software engineering and not C++ gotchas.
Samrat Patil
Code Complete is not an answer to all questions about good programming book.
ya23
@ya23 - I didn't say it was an answer to *all* questions about good programming. To be frank, I don't think such a book exists.
Paul Mitchell
I don't recommend "Code Complete". There is lot of much better books to spend time on. Too loot of text, too low things really helpful. And of course this is not really C++ book.
Roman Nikitchenko
+16  A: 

I believe the C++ faq lite is what you need. If you like it, you can buy a book version.

ya23
Yep. Forgot this one.
Samrat Patil
+1 I like this one, even a beginner can start with this!
Devil Jin
A: 

This question is not programming related. Most of the books provides the concepts and its entirely upto the reader which author or which book he likes and implement the concepts in the real world.

Sachin Chourasiya
Any suggesttion for downvoting me
Sachin Chourasiya
How recommending a practical book about programming is not programming-related?
ya23
I mean to say its all depends on your flavours, this question is not real and depends on individual taste.
Sachin Chourasiya
Indeed, it is subjective, maybe not even a real question. But whatever it is, it still is programming related :)
ya23
Disagree. There is number of definitely good C++ books and author of this question was pretty clear of what kind of books was requested.
Roman Nikitchenko
+4  A: 

I suggest you find "Exceptional C++" and others Herb Sutter's books very useful in any case. This is why I placed Amazon link (check references). They are written usually in "what is wrong here and why" style, very useful. Mayers' books are even better as for my opinion ("Effective C++" and so on).

But I'm in doubt if they have right the question you posted as example.

Roman Nikitchenko
+2  A: 

I'd go with either Effective C++ by Meyers, or C++ Primer by Addison Wesley. They're both much more about "Here's what the language does" than "Here's how you should design a bank program"

Afcrowe
I like all the down-voted answers here with no explanations as to why or what a better book might be...
Afcrowe
@Afcrowe, agreed
Luis B
A: 

It's been a while since I did anything in C++, but my go-to book on the subject is The C++ Programming Language. I would recommend at least seeing if you can find it in a library (it can be an older version of the book) and checking it out to see if it meets your needs before you buy it, but it's helped me out when I needed it.

Thomas Owens
Google books is usually good enough place to do such things ;).
Roman Nikitchenko
Why the -1 on this?
Thomas Owens
+1  A: 

Programming -- Principles and Practice Using C++ is without any doubt the best book to learn in a practical way C++. Bjarne who is the creator of this language say that. This book is the book used by himself in his classroom.

I have hundred of C++ books. Without any doubt this is the best one to learn and to get one step beyond.

A: 

the value of a will (usually) get stored in the readonly portion of an executable

Wrong. The character array a string\0 will be stored there, but the value of a is just a pointer to that array.

Fred
That's a terminology issue. Not the point of the question, though.
xtofl
+3  A: 

This one

alt text

grigy
+2  A: 

I'd recommend C++ FAQs (also a free Lite version on the web) and C++ Common Knowledge.

Wyzard
+2  A: 

Modern C++ Design by Andrei Alexandrescu

Prasoon Saurav