How well do you know memory? What different memory areas are there?This problem covers basics about C++'s main distinct memory stores. The following problem goes on to attack some deeper memory management questions in more detail.C++ has several
They are not. Probably "compared to what?" is a more useful answer. When people complain about standard-library container performance, I usually find one of three genuine problems (or one of the many myths and red herrings):I suffer copy overhead I
In terms of time and space, an array is just about the optimal construct for accessing a sequence of objects in memory. It is, however, also a very low level data structure with a vast potential for misuse and errors and in essentially all cases
"Who am I, really?" This problem addresses how to decide whether two pointers really refer to the same object.The "this != &other" test (illustrated below) is a common coding practice intended to prevent self-assignment. Is the condition
"To be, or not to be…" When does an object actually exist? This problem considers when an object is safe to use.Critique the following code fragment.void f() { T t(1); T& rt = t; //--- #1: do something with t or rt --- t.~T(); new (&t)
Are you thinking about doing your own class-specific memory management, or even replacing C++'s global new and delete? First, try this problem on for size.The following code shows classes that perform their own memory management. Point out as many
Automatic conversions from one type to another can be extremely convenient. This Item covers a typical example to illustrate why they're also extremely dangerous.The standard C++ string has no implicit conversion to a const char*. Should it?It is
size/resize 以及 capacity/reserve 之間的區別size 取得 vector 中元素的個數,resize 重新分配空間,並且將分配的空間初始化為0,capacity : return number of elements for while memory has been allocatedreserve:Allocates elements for vector to ensure a minimum size,preserving its contents if
To a novice,qsort(array,asize,sizeof(elem),elem_compare);looks pretty weird, and is harder to understand thansort(vec.begin(),vec.end());To an expert, the fact that sort() tends to be faster than qsort() for the same elements and the same comparison