<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[[C++03] Ist das definiertes Verhalten?]]></title><description><![CDATA[<p>Folgendes habe ich in einer Bibliothek gefunden und mich gefragt, ob das nicht undefiniertes Verhalten ist:</p>
<pre><code class="language-cpp">class base
{
private:
  int* m_data;

protected:
  int get_data() const { return *m_data; }

public:
  base(base const&amp; other)
  {
    // nicht trivial
  }

  base&amp; operator =(base other)
  {
    // nicht trivial
  }
};

class derived
  : public base
{
public:
  derived(/* mehrere parameter */)
  {
    // umfassende Initialisierung
    // kein trivialer Konstruktor
  }

  // copy constructor und assignment operator ebenfalls nicht trivial

  int get_xyz() { return get_data(); }
};

void create_data(int** data)
{
  *data = new int(10);
}

void release_data(int** data)
{
  delete *data;
}

// ...

derived d;
create_data((int**)&amp;d);  // 1

std::cout &lt;&lt; d.get_xyz() &lt;&lt; '\n';

release_data((int**)&amp;d); // 2
</code></pre>
<p>Es kommt keinerlei <code>virtual</code> vor.</p>
<p>Das ist doch undefiniertes Verhalten? Oder täusche ich mich da? <code>derived</code> ist doch kein POD? Dass der Cast bei 1 und 2 das gewünschte macht, ist zwar wahrscheinlich aber nicht definiert?</p>
<p>Grüssli</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/304257/c-03-ist-das-definiertes-verhalten</link><generator>RSS for Node</generator><lastBuildDate>Sun, 09 Aug 2026 21:02:28 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/304257.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 01 Jun 2012 22:02:15 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to [C++03] Ist das definiertes Verhalten? on Fri, 01 Jun 2012 22:02:15 GMT]]></title><description><![CDATA[<p>Folgendes habe ich in einer Bibliothek gefunden und mich gefragt, ob das nicht undefiniertes Verhalten ist:</p>
<pre><code class="language-cpp">class base
{
private:
  int* m_data;

protected:
  int get_data() const { return *m_data; }

public:
  base(base const&amp; other)
  {
    // nicht trivial
  }

  base&amp; operator =(base other)
  {
    // nicht trivial
  }
};

class derived
  : public base
{
public:
  derived(/* mehrere parameter */)
  {
    // umfassende Initialisierung
    // kein trivialer Konstruktor
  }

  // copy constructor und assignment operator ebenfalls nicht trivial

  int get_xyz() { return get_data(); }
};

void create_data(int** data)
{
  *data = new int(10);
}

void release_data(int** data)
{
  delete *data;
}

// ...

derived d;
create_data((int**)&amp;d);  // 1

std::cout &lt;&lt; d.get_xyz() &lt;&lt; '\n';

release_data((int**)&amp;d); // 2
</code></pre>
<p>Es kommt keinerlei <code>virtual</code> vor.</p>
<p>Das ist doch undefiniertes Verhalten? Oder täusche ich mich da? <code>derived</code> ist doch kein POD? Dass der Cast bei 1 und 2 das gewünschte macht, ist zwar wahrscheinlich aber nicht definiert?</p>
<p>Grüssli</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2218325</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2218325</guid><dc:creator><![CDATA[Dravere]]></dc:creator><pubDate>Fri, 01 Jun 2012 22:02:15 GMT</pubDate></item><item><title><![CDATA[Reply to [C++03] Ist das definiertes Verhalten? on Fri, 01 Jun 2012 22:04:10 GMT]]></title><description><![CDATA[<p>Natürlich ist das UB.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2218327</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2218327</guid><dc:creator><![CDATA[dot]]></dc:creator><pubDate>Fri, 01 Jun 2012 22:04:10 GMT</pubDate></item><item><title><![CDATA[Reply to [C++03] Ist das definiertes Verhalten? on Fri, 01 Jun 2012 22:23:54 GMT]]></title><description><![CDATA[<p>Natürlich ist das DB.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2218332</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2218332</guid><dc:creator><![CDATA[begründer]]></dc:creator><pubDate>Fri, 01 Jun 2012 22:23:54 GMT</pubDate></item><item><title><![CDATA[Reply to [C++03] Ist das definiertes Verhalten? on Fri, 01 Jun 2012 22:47:14 GMT]]></title><description><![CDATA[<p>Sollte <strong>definiertes</strong> Verhalten sein:</p>
<p>C++-Standard schrieb:</p>
<blockquote>
<p>A standard-layout class is a class that:<br />
— has no non-static data members of type non-standard-layout class (or array of such types) or reference,<br />
— has no virtual functions (10.3) and no virtual base classes (10.1),<br />
— has the same access control (Clause 11) for all non-static data members,<br />
— has no non-standard-layout base classes,<br />
— either has no non-static data members in the most derived class and at most one base class with<br />
non-static data members, or has no base classes with non-static data members, and<br />
— has no base classes of the same type as the first non-static data member.108<br />
A standard-layout struct is a standard-layout class defined with the class-key struct or the class-key class.<br />
A standard-layout union is a standard-layout class defined with the class-key union.</p>
<p>...</p>
<p>A pointer to a standard-layout struct object, suitably converted using a reinterpret_cast, points to its<br />
initial member (or if that member is a bit-field, then to the unit in which it resides) and vice versa. [ Note:<br />
There might therefore be unnamed padding within a standard-layout struct object, but not at its beginning,<br />
as necessary to achieve appropriate alignment. — end note ]</p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/post/2218337</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2218337</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Fri, 01 Jun 2012 22:47:14 GMT</pubDate></item><item><title><![CDATA[Reply to [C++03] Ist das definiertes Verhalten? on Fri, 01 Jun 2012 22:52:20 GMT]]></title><description><![CDATA[<p>I stand corrected...trotzdem etwas, was ich nie im Leben tun würde...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2218339</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2218339</guid><dc:creator><![CDATA[dot]]></dc:creator><pubDate>Fri, 01 Jun 2012 22:52:20 GMT</pubDate></item><item><title><![CDATA[Reply to [C++03] Ist das definiertes Verhalten? on Fri, 01 Jun 2012 22:57:45 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/19375">@SeppJ</a>,<br />
Ist das der C++11 oder C++03 Standard? In C++11 wurde sowas doch gelockert? Dein Zitat klingt mir ehrlich gesagt nach dem C++11 Standard. Ich mag mich gerade nicht daran erinnern, jemals etwas von standard-layout im C++03 Standard gelesen zu haben. Daher frage ich kurz nach <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
<p>Grüssli</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2218341</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2218341</guid><dc:creator><![CDATA[Dravere]]></dc:creator><pubDate>Fri, 01 Jun 2012 22:57:45 GMT</pubDate></item><item><title><![CDATA[Reply to [C++03] Ist das definiertes Verhalten? on Fri, 01 Jun 2012 23:01:47 GMT]]></title><description><![CDATA[<p>Ja das ist C++ 11. In C++ 03 war das definitiv noch UB...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2218343</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2218343</guid><dc:creator><![CDATA[dot]]></dc:creator><pubDate>Fri, 01 Jun 2012 23:01:47 GMT</pubDate></item><item><title><![CDATA[Reply to [C++03] Ist das definiertes Verhalten? on Fri, 01 Jun 2012 23:19:37 GMT]]></title><description><![CDATA[<p>Ja, das ist C++11, ich dachte, deine Frage wäre aktuell. Den alten kann ich ja auch noch mal rausholen. Moment...</p>
<p>...edit: Ja, ist undefiniert, weil dort die Klausel mit dem Cast für POD-structs gilt und die dürfen keinen nutzerdefinierten Konstruktor haben.</p>
<p>Ich würde da aber ganz pragmatisch mich trotzdem drauf verlassen. Gibt schließlich einen technischen Grund, wieso der Standard gelockert werden konnte. Keine normale Implementierung wird das falsch machen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2218345</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2218345</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Fri, 01 Jun 2012 23:19:37 GMT</pubDate></item><item><title><![CDATA[Reply to [C++03] Ist das definiertes Verhalten? on Fri, 01 Jun 2012 23:16:21 GMT]]></title><description><![CDATA[<p>dot schrieb:</p>
<blockquote>
<p>...trotzdem etwas, was ich nie im Leben tun würde...</p>
</blockquote>
<p>Da sind die Leute der Khronos Group wohl anderer Meinung. Ich habe das nämlich von ihrem OpenCL C++ Header:<br />
<a href="http://www.khronos.org/registry/cl/api/1.1/cl.hpp" rel="nofollow">http://www.khronos.org/registry/cl/api/1.1/cl.hpp</a></p>
<p>Der hat sowieso ein paar seltsame Eigenheiten. Ganz nett finde ich z.B. sowas:</p>
<pre><code class="language-cpp">cl::Program program(/* ... */);
cl::Kernel kernel(/* ... */);

program(); // 1
kernel();  // 2
</code></pre>
<p>Falls hier jemand wirklich meint, dass bei 1 das Programm oder bei 2 der Kernel ausgeführt wird, weit gefehlt! Damit erhält man nur das C Handle auf das Programm, bzw. den Kernel, zurück. Ist doch logisch!</p>
<p>Grüssli</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2218346</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2218346</guid><dc:creator><![CDATA[Dravere]]></dc:creator><pubDate>Fri, 01 Jun 2012 23:16:21 GMT</pubDate></item><item><title><![CDATA[Reply to [C++03] Ist das definiertes Verhalten? on Fri, 01 Jun 2012 23:31:39 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/19375">@SeppJ</a>,<br />
Danke für das Nachschauen.</p>
<p>Ja, ich nehme auch an, dass es sehr wahrscheinlich ist, dass es meistens funktionieren wird. Finde es aber trotzdem sehr fragwürdig, sowas einzusetzen. Erst recht in einem Header eines Standards.</p>
<p>Grüssli</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2218347</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2218347</guid><dc:creator><![CDATA[Dravere]]></dc:creator><pubDate>Fri, 01 Jun 2012 23:31:39 GMT</pubDate></item><item><title><![CDATA[Reply to [C++03] Ist das definiertes Verhalten? on Fri, 01 Jun 2012 23:34:28 GMT]]></title><description><![CDATA[<p>Dravere schrieb:</p>
<blockquote>
<p>Da sind die Leute der Khronos Group wohl anderer Meinung. Ich habe das nämlich von ihrem OpenCL C++ Header:<br />
<a href="http://www.khronos.org/registry/cl/api/1.1/cl.hpp" rel="nofollow">http://www.khronos.org/registry/cl/api/1.1/cl.hpp</a></p>
</blockquote>
<p>Ja, diese OpenCL C++ API ist grottig...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2218348</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2218348</guid><dc:creator><![CDATA[dot]]></dc:creator><pubDate>Fri, 01 Jun 2012 23:34:28 GMT</pubDate></item><item><title><![CDATA[Reply to [C++03] Ist das definiertes Verhalten? on Fri, 01 Jun 2012 23:38:20 GMT]]></title><description><![CDATA[<p>SeppJ schrieb:</p>
<blockquote>
<p>Sollte <strong>definiertes</strong> Verhalten sein:</p>
<p>C++-Standard schrieb:</p>
<blockquote>
<p>A standard-layout class is a class that:<br />
— has no non-static data members of type non-standard-layout class (or array of such types) or reference,<br />
— has no virtual functions (10.3) and no virtual base classes (10.1),<br />
— has the same access control (Clause 11) for all non-static data members,<br />
— has no non-standard-layout base classes,<br />
— either has no non-static data members in the most derived class and at most one base class with<br />
non-static data members, or has no base classes with non-static data members, and<br />
— has no base classes of the same type as the first non-static data member.108<br />
A standard-layout struct is a standard-layout class defined with the class-key struct or the class-key class.<br />
A standard-layout union is a standard-layout class defined with the class-key union.</p>
<p>...</p>
<p>A pointer to a standard-layout struct object, suitably converted using a reinterpret_cast, points to its<br />
initial member (or if that member is a bit-field, then to the unit in which it resides) and vice versa. [ Note:<br />
There might therefore be unnamed padding within a standard-layout struct object, but not at its beginning,<br />
as necessary to achieve appropriate alignment. — end note ]</p>
</blockquote>
</blockquote>
<p>Allerdings ist ein Objekt, das ein Basisklassensubobjekt eines Standardlayout-Typen enthält, kein Standardlayout-Objekt (auch nicht in Teilen). Also doch UB. siehe auch 1.8/1 (&quot;standard-layout struct object&quot; oben impliziert, dass der dynamische Typ ein Standardlayout-Typ ist) und 10./4.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2218349</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2218349</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Fri, 01 Jun 2012 23:38:20 GMT</pubDate></item></channel></rss>