<?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[stringstream und bus error.. ich werd noch wahnsinnig ;)]]></title><description><![CDATA[<p>Hello World,</p>
<p>ich habe nun seit einiger Zeit an folgendem im Grunde total simplen Poblem zu knacken: Um eine Hash-Map sinnvoll zu fuellen habe ich eine Methode geschrieben, die aus zwei int keys einen string erzeugen soll. Also test(3,4) liefert den string &quot;3:4&quot; zurueck. Soweit sogut.</p>
<pre><code class="language-cpp">std::string HashMapWrap::test(int key1, int key2) {
    std::stringstream stream;
    stream &lt;&lt; key1 &lt;&lt; &quot;:&quot; &lt;&lt; key2;
    std::string s = stream.str();
    return s;
}
</code></pre>
<p>wenn ich das ganze aufrufe mit einem simplen:</p>
<pre><code class="language-cpp">HashMapWrap hm();
std::cout &lt;&lt; hm.test(1,2);
</code></pre>
<p>bekomme ich immer einen Bus error.</p>
<p>Nun kommt was mich noch mehr wundert: Wenn ich folgendes schreibe geht alles:</p>
<pre><code class="language-cpp">std::string HashMapWrap::test2() {
    return &quot;test&quot;;
}
</code></pre>
<p>ABER wenn ich z.B. das hier hinzufuege:</p>
<pre><code class="language-cpp">std::string HashMapWrap::test2() {
    std::stringstream s1;
    std::cout &lt;&lt;&quot;1\n&quot;;
    int i = 22;
    s1 &lt;&lt; &quot;testMethod &quot; &lt;&lt; i;
    std::cout &lt;&lt;&quot;2\n&quot;;
    std::string s2 = s1.str();
    std::cout &lt;&lt;&quot;3\n&quot;;

    return &quot;test&quot;;
}
</code></pre>
<p>fliegt mir wieder ein 'bus error' an den Kopf. Vorher aber gibt er noch 1,2 und 3 aus. Was ich nun nicht verstehe ist</p>
<p>1. wenn ich alles bis hin zur 3 sehe, sollte der code darueber keinen Error schmeissen, oder?<br />
2. der Code ueber der Rueckgabe beeinflusst die Methode in keinster Weise, trotzdem bekomme ich mit ihm den fehler, ohne aber nicht.<br />
3. Der Code der nun also anscheinend die Fehler produziert (der stringstreamkram) funktioniert als solches wunderbar in der main()-Methode aber nicht in der Member-Methode..</p>
<p>Das macht alles keinen Sinn, wo liegt mein Fehler? Bitte klaert mich auf!</p>
<p>Danke Danke<br />
viele Gruesse</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/150391/stringstream-und-bus-error-ich-werd-noch-wahnsinnig</link><generator>RSS for Node</generator><lastBuildDate>Sat, 05 Sep 2026 20:30:02 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/150391.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 15 Jun 2006 22:16:53 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to stringstream und bus error.. ich werd noch wahnsinnig ;) on Thu, 15 Jun 2006 22:16:53 GMT]]></title><description><![CDATA[<p>Hello World,</p>
<p>ich habe nun seit einiger Zeit an folgendem im Grunde total simplen Poblem zu knacken: Um eine Hash-Map sinnvoll zu fuellen habe ich eine Methode geschrieben, die aus zwei int keys einen string erzeugen soll. Also test(3,4) liefert den string &quot;3:4&quot; zurueck. Soweit sogut.</p>
<pre><code class="language-cpp">std::string HashMapWrap::test(int key1, int key2) {
    std::stringstream stream;
    stream &lt;&lt; key1 &lt;&lt; &quot;:&quot; &lt;&lt; key2;
    std::string s = stream.str();
    return s;
}
</code></pre>
<p>wenn ich das ganze aufrufe mit einem simplen:</p>
<pre><code class="language-cpp">HashMapWrap hm();
std::cout &lt;&lt; hm.test(1,2);
</code></pre>
<p>bekomme ich immer einen Bus error.</p>
<p>Nun kommt was mich noch mehr wundert: Wenn ich folgendes schreibe geht alles:</p>
<pre><code class="language-cpp">std::string HashMapWrap::test2() {
    return &quot;test&quot;;
}
</code></pre>
<p>ABER wenn ich z.B. das hier hinzufuege:</p>
<pre><code class="language-cpp">std::string HashMapWrap::test2() {
    std::stringstream s1;
    std::cout &lt;&lt;&quot;1\n&quot;;
    int i = 22;
    s1 &lt;&lt; &quot;testMethod &quot; &lt;&lt; i;
    std::cout &lt;&lt;&quot;2\n&quot;;
    std::string s2 = s1.str();
    std::cout &lt;&lt;&quot;3\n&quot;;

    return &quot;test&quot;;
}
</code></pre>
<p>fliegt mir wieder ein 'bus error' an den Kopf. Vorher aber gibt er noch 1,2 und 3 aus. Was ich nun nicht verstehe ist</p>
<p>1. wenn ich alles bis hin zur 3 sehe, sollte der code darueber keinen Error schmeissen, oder?<br />
2. der Code ueber der Rueckgabe beeinflusst die Methode in keinster Weise, trotzdem bekomme ich mit ihm den fehler, ohne aber nicht.<br />
3. Der Code der nun also anscheinend die Fehler produziert (der stringstreamkram) funktioniert als solches wunderbar in der main()-Methode aber nicht in der Member-Methode..</p>
<p>Das macht alles keinen Sinn, wo liegt mein Fehler? Bitte klaert mich auf!</p>
<p>Danke Danke<br />
viele Gruesse</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1078576</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1078576</guid><dc:creator><![CDATA[silversurfer2025]]></dc:creator><pubDate>Thu, 15 Jun 2006 22:16:53 GMT</pubDate></item><item><title><![CDATA[Reply to stringstream und bus error.. ich werd noch wahnsinnig ;) on Thu, 15 Jun 2006 23:43:55 GMT]]></title><description><![CDATA[<p>Also leider bin ich noch ziemlich unerfahren mit C++ etc, und deshalb hier der Code der in der in der anderen Datei die Fehler aufwirft..</p>
<p>Hat jemand eine Idee? Ich verstehe das ganze nun wirklich nicht mehr... Ich bin echt ratlos (und doktere nun schon den ganzen Tag an diesem Fehler rum)...</p>
<p>Vielleicht hat ja doch jemand seine Kristallkugel dabei?</p>
<p>Meine main.cpp:</p>
<pre><code class="language-cpp">#include &lt;string&gt;
#include &lt;sstream&gt;
#include &quot;HashMapWrap.h&quot;

using namespace Tribots;

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

    HashMapWrap hm(20);

    std::cout &lt;&lt; hm.test(); 
    return 0;
}
</code></pre>
<p>Und hier die andere Klasse: HashMapWrap.h</p>
<pre><code class="language-cpp">#include &quot;hash_map.h&quot;
#include &lt;string&gt;

class HashMapWrap {

struct eqstr {
    bool operator()(const char* s1, const char* s2) const{
    return strcmp(s1, s2) == 0;
    }
};

public:
    HashMapWrap::HashMapWrap(int initSize);
    HashMapWrap::HashMapWrap();
    HashMapWrap::~HashMapWrap();

    float getValue(int key1, int key2);
    void setValue(int key1, int key2, float value);

    void resize(int i);
    int size();

    std::string makeStringFromKeys(int key1, int key2);
    std::string test();

private:
    hash_map&lt;const char*, float, hash&lt;const char*&gt;, eqstr&gt; connections;
};
</code></pre>
<p>und die passende .cpp datei:</p>
<pre><code class="language-cpp">/*
 *  HashMapWrap.cpp
 *  dynObjRec
 *
 *  Created by Tim Kietzmann on 15.06.06.
 *  Copyright 2006 __MyCompanyName__. All rights reserved.
 *
 */

#include &quot;HashMapWrap.h&quot;
#include &lt;sstream&gt;

HashMapWrap::HashMapWrap() {
}

HashMapWrap::HashMapWrap(int initSize) {
    connections.resize(initSize);
}

HashMapWrap::~HashMapWrap() {
}

float HashMapWrap::getValue(int key1,int key2) {
    return connections[makeStringFromKeys(key1,key2).c_str()];
}

void HashMapWrap::setValue(int key1,int key2,float value) {
    connections[makeStringFromKeys(key1,key2).c_str()] = value;
}

void HashMapWrap::resize(int initSize) {
    connections.resize(initSize);
}

int HashMapWrap::size() {
    return connections.size();
}

std::string HashMapWrap::makeStringFromKeys(int key1, int key2) {
    std::ostringstream os;
    os &lt;&lt; key1 &lt;&lt; &quot;:&quot; &lt;&lt; key2;
    std::string s = os.str();
    return s;
}

std::string HashMapWrap::test() {
    std::stringstream s1;
    std::cout &lt;&lt;&quot;1\n&quot;;
    int i = 22;
    s1 &lt;&lt; &quot;testMethod &quot; &lt;&lt; i;
    std::cout &lt;&lt;&quot;2\n&quot;;
    std::string s2 = s1.str();
    std::cout &lt;&lt;&quot;3\n&quot;;
    s1.flush();
    s1.clear();

    return &quot;test&quot;;
}
</code></pre>
<p>Ich hoffe dass dieser Code nun fuer etwas mehr Klarheit sorgt, ich hatte vorhin nur Code-Schnipsel hinzugefuegt um euch die lesearbeit zu ersparen..</p>
<p>Die test() methode ist die die ich oben auch beschrieben habe, und die makeStringFromFile ebenfalls <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>Danke euch!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1078589</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1078589</guid><dc:creator><![CDATA[silversurfer2025]]></dc:creator><pubDate>Thu, 15 Jun 2006 23:43:55 GMT</pubDate></item><item><title><![CDATA[Reply to stringstream und bus error.. ich werd noch wahnsinnig ;) on Fri, 16 Jun 2006 06:28:32 GMT]]></title><description><![CDATA[<p>und die definition von hash_map?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1078644</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1078644</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Fri, 16 Jun 2006 06:28:32 GMT</pubDate></item><item><title><![CDATA[Reply to stringstream und bus error.. ich werd noch wahnsinnig ;) on Fri, 16 Jun 2006 07:01:23 GMT]]></title><description><![CDATA[<p>Die Klasse hash_map.h ist im GNU ISO C++ Library (also auch standartmaessig im gcc (g++ 4.0.0)) vorhanden..</p>
<p>Hier sind die deklaration und implementation (hoffe ich)</p>
<p><a href="http://web350.server15.greatnet.de/hash_map.h" rel="nofollow">http://web350.server15.greatnet.de/hash_map.h</a><br />
<a href="http://web350.server15.greatnet.de/hash_map" rel="nofollow">http://web350.server15.greatnet.de/hash_map</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1078652</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1078652</guid><dc:creator><![CDATA[silversurfer2025]]></dc:creator><pubDate>Fri, 16 Jun 2006 07:01:23 GMT</pubDate></item><item><title><![CDATA[Reply to stringstream und bus error.. ich werd noch wahnsinnig ;) on Fri, 16 Jun 2006 07:11:14 GMT]]></title><description><![CDATA[<p>Also das Problem ist auch vorhanden wenn man die Zeilen mit HashMap auskommentiert..</p>
<p>Hier eine neue Version, aber gleicher Fehler:</p>
<p>HashMapWrap.h:</p>
<pre><code class="language-cpp">#include &lt;string&gt;

class HashMapWrap {

struct eqstr {
    bool operator()(const char* s1, const char* s2) const{
    return strcmp(s1, s2) == 0;
    }
};

public:
    HashMapWrap::HashMapWrap(int initSize);
    HashMapWrap::HashMapWrap();
    HashMapWrap::~HashMapWrap();

    float getValue(int key1, int key2);
    void setValue(int key1, int key2, float value);

    void resize(int i);
    int size();

    std::string makeStringFromKeys(int key1, int key2);
    std::string test();

private:
    //hash_map&lt;const char*, float, hash&lt;const char*&gt;, eqstr&gt; connections;
};
</code></pre>
<p>und die .cpp datei:</p>
<pre><code>/*
 *  HashMapWrap.cpp
 *  dynObjRec
 *
 *  Created by Tim Kietzmann on 15.06.06.
 *  Copyright 2006 __MyCompanyName__. All rights reserved.
 *
 */

#include &quot;HashMapWrap.h&quot;
#include &lt;sstream&gt;

HashMapWrap::HashMapWrap() {
}

HashMapWrap::HashMapWrap(int initSize) {
   // connections.resize(initSize);
}

HashMapWrap::~HashMapWrap() {
}

float HashMapWrap::getValue(int key1,int key2) {
    return 0;//connections[makeStringFromKeys(key1,key2).c_str()];
}

void HashMapWrap::setValue(int key1,int key2,float value) {
    //connections[makeStringFromKeys(key1,key2).c_str()] = value;
}

void HashMapWrap::resize(int initSize) {
   // connections.resize(initSize);
}

int HashMapWrap::size() {
    return 0;//connections.size();
}

std::string HashMapWrap::makeStringFromKeys(int key1, int key2) {
    std::ostringstream os;
    os &lt;&lt; key1 &lt;&lt; &quot;:&quot; &lt;&lt; key2;
    std::string s = os.str();
    return s;
}

std::string HashMapWrap::test() {
    std::stringstream s1;
    std::cout &lt;&lt;&quot;1\n&quot;;
    int i = 22;
    s1 &lt;&lt; &quot;testMethod &quot; &lt;&lt; i;
    std::cout &lt;&lt;&quot;2\n&quot;;
    std::string s2 = s1.str();
    std::cout &lt;&lt;&quot;3\n&quot;;
    s1.flush();
    s1.clear();

    return &quot;test&quot;;
}
</code></pre>
<p>main.cpp:</p>
<pre><code class="language-cpp">#include &lt;string&gt;
#include &lt;sstream&gt;
#include &quot;HashMapWrap.h&quot;

using namespace Tribots;

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

    HashMapWrap hm(20);

    std::cout &lt;&lt; hm.test();
    std::cout &lt;&lt; hm.makeStringFromKeys(1,2); 
    return 0;
}
</code></pre>
<p>beide Methoden schmeissen mri den Bus error an den Kopf!</p>
<p>Gruesse</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1078655</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1078655</guid><dc:creator><![CDATA[silversurfer2025]]></dc:creator><pubDate>Fri, 16 Jun 2006 07:11:14 GMT</pubDate></item><item><title><![CDATA[Reply to stringstream und bus error.. ich werd noch wahnsinnig ;) on Fri, 16 Jun 2006 08:25:18 GMT]]></title><description><![CDATA[<p>Wie weit kommt denn die test() Methode, bevor sie dich mit einem &quot;Bus Error&quot; überrascht?</p>
<p>(btw, wenn die Methode nichts mit der internen Datenstruktur zu tun hast, reicht dort auch eine statische Memberfunktion (oder eine unabhängige Funktion)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1078688</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1078688</guid><dc:creator><![CDATA[CStoll]]></dc:creator><pubDate>Fri, 16 Jun 2006 08:25:18 GMT</pubDate></item><item><title><![CDATA[Reply to stringstream und bus error.. ich werd noch wahnsinnig ;) on Fri, 16 Jun 2006 21:01:17 GMT]]></title><description><![CDATA[<p>Er gibt alles aus, also 1,2 und 3.</p>
<p>Ihr koennt euch uebrigens die zeilen<br />
s1.flush();<br />
s1.clear();<br />
einfach wegdenken, mit und ohne kommt der Fehler!</p>
<p>Das bescheuerte ist dass wenn die Zeilen vorhanden sind er den Bus error NACH der Ausgabe von 3 macht also wohl beim return. Wenn ich aber die zeilen loesche kommt kein bus error.</p>
<p>Ich finde das ganze sehr merkwuerdig, weil er im ersten Fall den Code anscheinend durchlaeuft ohne Problem und der Fehler beim return kommt und der zweite Fall zeigt dass das return alleine ohne probleme funktioniert..</p>
<p>Es ist zum Haare raufen..</p>
<p>Danke fuer eure Hilfe schon und noch einmal...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1079172</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1079172</guid><dc:creator><![CDATA[silversurfer2025]]></dc:creator><pubDate>Fri, 16 Jun 2006 21:01:17 GMT</pubDate></item><item><title><![CDATA[Reply to stringstream und bus error.. ich werd noch wahnsinnig ;) on Fri, 16 Jun 2006 22:48:43 GMT]]></title><description><![CDATA[<p>Sry das ich frage, aber was ist ein &quot;Bus Error&quot;?</p>
<p>Also bei mir lässt sich das Beispiel wunderbar durchlaufen. Ausgabe:</p>
<pre><code>1
2
3
test1:2
</code></pre>
<p>mfg.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1079218</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1079218</guid><dc:creator><![CDATA[joomoo]]></dc:creator><pubDate>Fri, 16 Jun 2006 22:48:43 GMT</pubDate></item><item><title><![CDATA[Reply to stringstream und bus error.. ich werd noch wahnsinnig ;) on Fri, 16 Jun 2006 23:32:10 GMT]]></title><description><![CDATA[<p>joomoo schrieb:</p>
<blockquote>
<p>Sry das ich frage, aber was ist ein &quot;Bus Error&quot;?</p>
<p>Also bei mir lässt sich das Beispiel wunderbar durchlaufen. Ausgabe:</p>
<pre><code>1
2
3
test1:2
</code></pre>
<p>mfg.</p>
</blockquote>
<p>natürlich. ist ja auch nichts dran. ich würde vermuten, das problem ist in der build-umgebung und nicht im source zu suchen. irgendein treiber, der probleme macht, viren, hardwaredefekte, was auch immer.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1079232</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1079232</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Fri, 16 Jun 2006 23:32:10 GMT</pubDate></item><item><title><![CDATA[Reply to stringstream und bus error.. ich werd noch wahnsinnig ;) on Mon, 19 Jun 2006 19:48:43 GMT]]></title><description><![CDATA[<p>also ich habe den compiler von g++3.3 auf 4.0 umgestellt, nun cshien vorerst das problem nicht zo bestehen.. bis gerade eben. Ich habe nichts am code geaendert und auf einmal funktioniert wieder nichts. So eine sch*****! Ich versuche das ganze morgen unter Linux.. ich sitze nun fast ne woche an dieser scheiss klasse..etwas was ich in 3 anderen sprachen innerhalb von stunden geschrieben haette dauert fuer mich in c++ tage <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f622.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--crying_face"
      title=";("
      alt="😢"
    /></p>
<p>Habe da nun eine Frage zum Code an sich:<br />
ich habe in der makeStringFromKeys-Methode ein string objekt angelegt, welches mit inhalt gefuellt wird. Dieses string-objekt wird dann zurueckgegeben als return der Methode. Kann man das ueberhaupt so machen oder geht das objekt ausserhalb des scopes verloren (weil ja nur lokale variable)? Sollte ich also besser mit new arbeiten und einen string pointer zurueckgeben?</p>
<p>Danke schon einmal...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1080952</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1080952</guid><dc:creator><![CDATA[silversurfer2025]]></dc:creator><pubDate>Mon, 19 Jun 2006 19:48:43 GMT</pubDate></item></channel></rss>