<?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[new char problem]]></title><description><![CDATA[<p>Ich kriege im folgenden Programm ein unerwartetes Ergebnis:</p>
<pre><code class="language-cpp">#include &lt;cstdlib&gt;
#include &lt;iostream&gt;
#include &lt;windows.h&gt;
#include &lt;tchar.h&gt;
#include &lt;string&gt;
using namespace std;

LPCTSTR text = _T(&quot;testtest&quot;);
char* hilfe;

int main(int argc, char *argv[])
{

    cout &lt;&lt; text; // Ausgabe: testtest

    int size = strlen(text);
    cout &lt;&lt; &quot; &quot; &lt;&lt; size; // Ausgabe: 8

    hilfe = new char(size+1);
    cout &lt;&lt; &quot; &quot; &lt;&lt; strlen(hilfe) &lt;&lt; &quot;\n&quot;; // Ausgabe: 3 (???)

    for(int i=0; i&lt; size; i++){hilfe[i] = text[i]; cout &lt;&lt; &quot;\n&quot; &lt;&lt; i &lt;&lt; &quot;:&quot;&lt;&lt; hilfe[i];} // Ausgabe: 0: t 1: e ... ist korrekt

    hilfe[size+1] = '\0';

    cout &lt;&lt; &quot;\n\n&quot; &lt;&lt; hilfe; // Ausgabe: testtest plus i.welcher zeichen..

    system(&quot;PAUSE&quot;);
    return EXIT_SUCCESS;
}
</code></pre>
<p>Ich habe schon überlegt, ob eventuell bei &quot;new char()&quot; etwas schief läuft, oder ob es später einen Überlauf von &quot;hilfe&quot; gibt.</p>
<p>Warum überhaupt strlen(hilfe): 3 ???</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/272996/new-char-problem</link><generator>RSS for Node</generator><lastBuildDate>Fri, 28 Aug 2026 12:27:38 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/272996.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 29 Aug 2010 12:37:45 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to new char problem on Sun, 29 Aug 2010 12:59:25 GMT]]></title><description><![CDATA[<p>Ich kriege im folgenden Programm ein unerwartetes Ergebnis:</p>
<pre><code class="language-cpp">#include &lt;cstdlib&gt;
#include &lt;iostream&gt;
#include &lt;windows.h&gt;
#include &lt;tchar.h&gt;
#include &lt;string&gt;
using namespace std;

LPCTSTR text = _T(&quot;testtest&quot;);
char* hilfe;

int main(int argc, char *argv[])
{

    cout &lt;&lt; text; // Ausgabe: testtest

    int size = strlen(text);
    cout &lt;&lt; &quot; &quot; &lt;&lt; size; // Ausgabe: 8

    hilfe = new char(size+1);
    cout &lt;&lt; &quot; &quot; &lt;&lt; strlen(hilfe) &lt;&lt; &quot;\n&quot;; // Ausgabe: 3 (???)

    for(int i=0; i&lt; size; i++){hilfe[i] = text[i]; cout &lt;&lt; &quot;\n&quot; &lt;&lt; i &lt;&lt; &quot;:&quot;&lt;&lt; hilfe[i];} // Ausgabe: 0: t 1: e ... ist korrekt

    hilfe[size+1] = '\0';

    cout &lt;&lt; &quot;\n\n&quot; &lt;&lt; hilfe; // Ausgabe: testtest plus i.welcher zeichen..

    system(&quot;PAUSE&quot;);
    return EXIT_SUCCESS;
}
</code></pre>
<p>Ich habe schon überlegt, ob eventuell bei &quot;new char()&quot; etwas schief läuft, oder ob es später einen Überlauf von &quot;hilfe&quot; gibt.</p>
<p>Warum überhaupt strlen(hilfe): 3 ???</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1945669</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1945669</guid><dc:creator><![CDATA[System_Shutdown]]></dc:creator><pubDate>Sun, 29 Aug 2010 12:59:25 GMT</pubDate></item><item><title><![CDATA[Reply to new char problem on Sun, 29 Aug 2010 12:43:51 GMT]]></title><description><![CDATA[<p>Um ein Array anzulegen nutzt man die eckigen Klammern <code>new char[size + 1];</code> .<br />
Was du machst, ist ein einzelner <code>char</code> -Wert auf dem Heap anzulegen und diesen mit dem Wert <code>size + 1</code> zu initialisieren.</p>
<p>Im übrigen solltest du globale Variablen vermeiden. Vielleicht einmal <code>std::string</code> in betracht ziehen. Und das Lesen von diesem Thread dürfte auch nicht schaden:<br />
<a href="http://www.c-plusplus.net/forum/viewtopic-var-t-is-111042.html" rel="nofollow">http://www.c-plusplus.net/forum/viewtopic-var-t-is-111042.html</a></p>
<p>Grüssli</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1945673</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1945673</guid><dc:creator><![CDATA[Dravere]]></dc:creator><pubDate>Sun, 29 Aug 2010 12:43:51 GMT</pubDate></item><item><title><![CDATA[Reply to new char problem on Sun, 29 Aug 2010 12:45:18 GMT]]></title><description><![CDATA[<p><strong>new char []</strong> ?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1945674</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1945674</guid><dc:creator><![CDATA[padreigh]]></dc:creator><pubDate>Sun, 29 Aug 2010 12:45:18 GMT</pubDate></item><item><title><![CDATA[Reply to new char problem on Sun, 29 Aug 2010 12:51:30 GMT]]></title><description><![CDATA[<p>und (mal schon die klammern ersetzt):</p>
<pre><code class="language-cpp">hilfe = new char[size+1];
/*
    ...
*/
    hilfe[size+1] = '\n'; //BUMM
</code></pre>
<p>new char[size+1]<br />
-&gt; 0...size<br />
size+1 ist eins dahinter - erzeugt undefiniertes verhalten <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1945678</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1945678</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Sun, 29 Aug 2010 12:51:30 GMT</pubDate></item><item><title><![CDATA[Reply to new char problem on Sun, 29 Aug 2010 12:51:50 GMT]]></title><description><![CDATA[<p>das system(&quot;pause&quot;) ist erstmal zweitrangig, werds mir aber merken. <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>ich bekomme selbst mit eckigen Klammern die selbe Ausgabe..<br />
ich hatte ja zuvor eckige, weswegen ich dann runde probiert habe.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1945679</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1945679</guid><dc:creator><![CDATA[System_Shutdown]]></dc:creator><pubDate>Sun, 29 Aug 2010 12:51:50 GMT</pubDate></item><item><title><![CDATA[Reply to new char problem on Sun, 29 Aug 2010 13:01:44 GMT]]></title><description><![CDATA[<p>EDIT:<br />
oooops, ich hab garnicht gemerkt, dass ich statt hilfe[size+1] = '\n' statt '\0' hatte..</p>
<p>jetzt gehts mit hilfe[size] = '\0'.<br />
hab mich schon gewundert eh. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1945681</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1945681</guid><dc:creator><![CDATA[System_Shutdown]]></dc:creator><pubDate>Sun, 29 Aug 2010 13:01:44 GMT</pubDate></item></channel></rss>