Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>it is illegal for a vector to contain pointers to invalid memory</p> </blockquote> <p>This is what the Standard has to say about the contents of a container:</p> <p>(23.3) : The type of objects stored in these components must meet the requirements of <em>CopyConstructible</em> types (20.1.3), and the additional requirements of <em>Assignable</em> types.</p> <p>(20.1.3.1, CopyConstructible) : In the following Table 30, T is a type to be supplied by a C + + program instantiating a template, t is a value of type T, and u is a value of type const T.</p> <pre><code>expression return type requirement xxxxxxxxxx xxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxx T(t) t is equivelant to T(t) T(u) u is equivelant to T(u) t.~T() &amp;t T* denotes the address of t &amp;u const T* denotes the address of u </code></pre> <p>(23.1.4, Assignable) : 64, T is the type used to instantiate the container, t is a value of T, and u is a value of (possibly const) T.</p> <pre><code>expression return type requirement xxxxxxxxxx xxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxx t = u T&amp; t is equivilant to u </code></pre> <p>That's all that is says about the contents of an STL collection. It says nothing about pointers and it is particularly silent about the pointers pointing to valid memory.</p> <p>Therefore, <code>delete</code>ing pointers in a <code>vector</code>, while most likely a very bad architectural decision and an invitation to pain and suffering with the debugger at 3:00 AM on a Saturday night, is perfectly legal.</p> <h2>EDIT:</h2> <p>Regarding Kranar's comment that "assigning a pointer to an invalid pointer value results in undefined behavior." No, this is incorrect. This code is perfectly valid:</p> <pre><code>Foo* foo = new Foo(); delete foo; Foo* foo_2 = foo; // This is legal </code></pre> <p>What is illegal is trying to do something with that pointer (or <code>foo</code>, for that matter):</p> <pre><code>delete foo_2; // UB foo_2-&gt;do_something(); // UB Foo&amp; foo_ref = *foo_2; // UB </code></pre> <p>Simply creating a wild pointer is legal according to the Standard. Probably not a good idea, but legal nonetheless.</p> <h2>EDIT2:</h2> <p>More from the Standard regarding pointer types. </p> <p>So sayeth the Standard (3.9.2.3) :</p> <blockquote> <p>... A valid value of an object pointer type represents either the address of a byte in memory (1.7) or a null pointer (4.10)...</p> </blockquote> <p>...and regarding "a byte in memory," (1.7.1) :</p> <blockquote> <p>The fundamental storage unit in the C + + memory model is the byte. A byte is at least large enough to contain any member of the basic execution character set and is composed of a contiguous sequence of bits, the number of which is implementation-defined. The least significant bit is called the low-order bit; the most significant bit is called the high-order bit. The memory available to a C + + program consists of one or more sequences of contiguous bytes. Every byte has a unique address.</p> </blockquote> <p>There is nothing here about that byte being part of a living <code>Foo</code>, about you having access to it, or anything of the sort. Its just a byte in memory.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload