All Questions

863
votes
4answers
84751 views

Undefined behavior and sequence points

What are "sequence points"? What is the relation between undefined behaviour and sequence points? I often use funny and convoluted expressions like a[++i] = i;, to make myself feel better. Why shoul...
1388
votes
9answers
387176 views

How can I profile C++ code running in Linux?

I have a C++ application, running on Linux, which I'm in the process of optimizing. How can I pinpoint which areas of my code are running slowly?
567
votes
16answers
131110 views

What is object slicing?

Someone mentioned it in the IRC, but google doesn't have a good answer.
2494
votes
74answers
1884872 views

The most elegant way to iterate the words of a string

What is the most elegant way to iterate the words of a string? The string can be assumed to be composed of words separated by whitespace. Note that I'm not interested in C string functions or that ki...
504
votes
19answers
252346 views

What should main() return in C and C++?

What is the correct (most efficient) way to define the main() function in C and C++ — int main() or void main() — and why? If int main() then return 1 or return 0? There are numerous duplicate...
405
votes
8answers
36015 views

Undefined, unspecified and implementation-defined behavior

What is the difference between undefined, unspecified, and implementation-defined behavior in C and C++?
1550
votes
5answers
249031 views

What is the copy-and-swap idiom?

What is this idiom and when should it be used? Which problems does it solve? Does the idiom change when C++11 is used? Although it's been mentioned in many places, we didn't have any singular "what i...
20363
votes
21answers
1149367 views

Why is it faster to process a sorted array than an unsorted array?

Here is a piece of C++ code that seems very peculiar. For some strange reason, sorting the data miraculously makes the code almost six times faster. #include <algorithm> #include <ctime> ...
416
votes
5answers
109884 views

How do I use arrays in C++?

C++ inherited arrays from C where they are used virtually everywhere. C++ provides abstractions that are easier to use and less error-prone (std::vector<T> since C++98 and std::array<T, n>...
2383
votes
31answers
732253 views

What are the differences between a pointer variable and a reference variable in C++?

I know references are syntactic sugar, so code is easier to read and write. But what are the differences? Summary from answers and links below: A pointer can be re-assigned any number of times w...

Previous Next