<?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[Segmentation fault, Problem mit new]]></title><description><![CDATA[<p>Hallo,<br />
habe ein Problem mit folgendem Programm (nur ein Ausshnitt):</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;fstream&gt;
using namespace std;

class GRAPH{
public:
	int degree;
	int *neighbours;
};
int n,i;
GRAPH *graph;

int readgraph(const char *filename, int *n, GRAPH **graph){
	int i, n1, n2;
	ifstream in(filename);
	in &gt;&gt; *n;
	cout &lt;&lt; &quot;Test: &quot; &lt;&lt; *n;
	*graph = new GRAPH[*n];

	graph[0]-&gt;degree=1;
	graph[1]-&gt;degree=1;
    return 0;
}
int main(){
	char dateiname[67];

	cout &lt;&lt; &quot;Dateiname: &quot;;
	cin &gt;&gt; dateiname;
	readgraph(dateiname,&amp;n,&amp;graph);
    return 0;
}
</code></pre>
<p>Durch Zeile 18 wird *n ausgegeben und ist aufgrund meiner Test-Datei, die öffne 5. Dann will ich also durch new ein Array mit 5 Elementen meiner GRAPH-Klasse einrichten. Die Zuweisung graph[0]-&gt;degree=0 geht noch gut, aber graph[1]-&gt;degree erzeugt laut Debugger einen segmentation fault.</p>
<p>Hoffe, mir kann jemand helfen, hab schon so viel rumprobiert und mit Nachdenken komm ich auch zu nichts.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/250281/segmentation-fault-problem-mit-new</link><generator>RSS for Node</generator><lastBuildDate>Wed, 16 Sep 2026 02:23:06 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/250281.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 16 Sep 2009 08:21:23 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Segmentation fault, Problem mit new on Wed, 16 Sep 2009 08:21:23 GMT]]></title><description><![CDATA[<p>Hallo,<br />
habe ein Problem mit folgendem Programm (nur ein Ausshnitt):</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;fstream&gt;
using namespace std;

class GRAPH{
public:
	int degree;
	int *neighbours;
};
int n,i;
GRAPH *graph;

int readgraph(const char *filename, int *n, GRAPH **graph){
	int i, n1, n2;
	ifstream in(filename);
	in &gt;&gt; *n;
	cout &lt;&lt; &quot;Test: &quot; &lt;&lt; *n;
	*graph = new GRAPH[*n];

	graph[0]-&gt;degree=1;
	graph[1]-&gt;degree=1;
    return 0;
}
int main(){
	char dateiname[67];

	cout &lt;&lt; &quot;Dateiname: &quot;;
	cin &gt;&gt; dateiname;
	readgraph(dateiname,&amp;n,&amp;graph);
    return 0;
}
</code></pre>
<p>Durch Zeile 18 wird *n ausgegeben und ist aufgrund meiner Test-Datei, die öffne 5. Dann will ich also durch new ein Array mit 5 Elementen meiner GRAPH-Klasse einrichten. Die Zuweisung graph[0]-&gt;degree=0 geht noch gut, aber graph[1]-&gt;degree erzeugt laut Debugger einen segmentation fault.</p>
<p>Hoffe, mir kann jemand helfen, hab schon so viel rumprobiert und mit Nachdenken komm ich auch zu nichts.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1779615</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1779615</guid><dc:creator><![CDATA[alx3400]]></dc:creator><pubDate>Wed, 16 Sep 2009 08:21:23 GMT</pubDate></item><item><title><![CDATA[Reply to Segmentation fault, Problem mit new on Wed, 16 Sep 2009 08:41:29 GMT]]></title><description><![CDATA[<p>Da stellt sich die Frage wozu du</p>
<pre><code class="language-cpp">GRAPH *graph;
</code></pre>
<p>global deklariert hast, wenn du es sowieso in der Funktion auch noch übergibst.<br />
Und dann haben die auch noch die gleichen Variablennamen....</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1779626</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1779626</guid><dc:creator><![CDATA[It0101]]></dc:creator><pubDate>Wed, 16 Sep 2009 08:41:29 GMT</pubDate></item><item><title><![CDATA[Reply to Segmentation fault, Problem mit new on Wed, 16 Sep 2009 08:48:40 GMT]]></title><description><![CDATA[<p>Ich rufe doch in Zeile 30 die Funktion readgraph auf, dafür muss graph doch definiert sein, oder nicht? Der Grund, warum ich das gemacht habe, ist auch eigentlich, dass ich das Programm nur aus einem in C geschriebenen umschreibe und das da so gemacht wurde.</p>
<p>Ich weiss auch anhand deiner Antwort nicht genau, wie ich es anders machen sollte.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1779630</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1779630</guid><dc:creator><![CDATA[alx3400]]></dc:creator><pubDate>Wed, 16 Sep 2009 08:48:40 GMT</pubDate></item><item><title><![CDATA[Reply to Segmentation fault, Problem mit new on Wed, 16 Sep 2009 09:02:20 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">graph[0]-&gt;degree=1; //sollte (*graph)[0].degree=1; sein
graph[1]-&gt;degree=1; //sollte (*graph)[1].degree=1; sein
</code></pre>
<p>Vielleicht solltest Du Dir mal Container (z.B. <code>std::vector</code> ) ansehen. Außerdem <code>std::string</code> und <code>std::getline</code> .<br />
Die glabelen Variablen können auch lokal in der main-Funktion deklariert werden. Referenzen ersparen einem das Gehampel mit den Doppelpointern.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1779636</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1779636</guid><dc:creator><![CDATA[Tachyon]]></dc:creator><pubDate>Wed, 16 Sep 2009 09:02:20 GMT</pubDate></item><item><title><![CDATA[Reply to Segmentation fault, Problem mit new on Wed, 16 Sep 2009 09:02:23 GMT]]></title><description><![CDATA[<p>Schau dir mal Referenzen an, das erleichtert die ganze Pointerfrickelei um einiges. Deine call-by-reference über Pointer zu machen ist C-Stil und in C++ nicht nötig.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1779640</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1779640</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Wed, 16 Sep 2009 09:02:23 GMT</pubDate></item><item><title><![CDATA[Reply to Segmentation fault, Problem mit new on Wed, 16 Sep 2009 09:02:25 GMT]]></title><description><![CDATA[<p>Da wir hier in C++ sind, ist der Hinweis auf std::vector obligatorisch: Nutze std::vector (in Kombination mit Referenzen beim Funktionsparameter)!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1779641</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1779641</guid><dc:creator><![CDATA[knivil]]></dc:creator><pubDate>Wed, 16 Sep 2009 09:02:25 GMT</pubDate></item><item><title><![CDATA[Reply to Segmentation fault, Problem mit new on Wed, 16 Sep 2009 09:12:23 GMT]]></title><description><![CDATA[<p>alx3400 schrieb:</p>
<blockquote>
<p>Ich rufe doch in Zeile 30 die Funktion readgraph auf, dafür muss graph doch definiert sein, oder nicht? ...</p>
</blockquote>
<p>Schon, aber das ist kein Grund, die Variable global zu machen. Außerdem: string-as-string-can... <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="😉"
    /><br />
Zusammen mit den Hinweisen auf vector und Referenzen:</p>
<pre><code class="language-cpp">#include &lt;string&gt;
#include &lt;vector&gt;

class GRAPH{
public:
    int degree;
    int *neighbours;
};

int readgraph(const char *filename, int &amp;n, std::vector&lt;GRAPH&gt;&amp; graph){
    ifstream in(filename);
    in &gt;&gt; n;
    cout &lt;&lt; &quot;Test: &quot; &lt;&lt; *n;
    graph.resize(n); // braucht's eigentlich nicht unbedingt
    graph[0].degree=1; // Achtung: Hier fehlen Größenüberprüfungen!!
    graph[1].degree=1;
    // was ist mit den Anderen graph-Objekten?
    return 0;
} 

int main(){
    std::string dateiname;
    cout &lt;&lt; &quot;Dateiname: &quot;;
    cin &gt;&gt; dateiname;

    int n;                   // reicht vollkommen aus, die hier zu deklarieren
    std::vector&lt;GRAPH&gt; graph; // reicht vollkommen aus, die hier zu deklarieren
    readgraph(dateiname.c_str(), n, graph);
    return 0;
}
</code></pre>
<p>Gruß,</p>
<p>Simon2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1779643</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1779643</guid><dc:creator><![CDATA[Simon2]]></dc:creator><pubDate>Wed, 16 Sep 2009 09:12:23 GMT</pubDate></item><item><title><![CDATA[Reply to Segmentation fault, Problem mit new on Wed, 16 Sep 2009 09:07:47 GMT]]></title><description><![CDATA[<p>Tachyon schrieb:</p>
<blockquote>
<pre><code class="language-cpp">graph[0]-&gt;degree=1; //sollte (*graph)[0].degree=1; sein
graph[1]-&gt;degree=1; //sollte (*graph)[1].degree=1; sein
</code></pre>
</blockquote>
<p>Das ist doch nur eine andere Schreibweise oder? Jedenfalls macht das Programm genau den gleichen Quatsch in beiden Schreibweisen.</p>
<p>Container sollte ich mir mal anschauen, und es steht auch schon auf der To-Do-Liste, aber im Moment ist es mir wichtig, das mit einem Array zu machen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1779645</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1779645</guid><dc:creator><![CDATA[alx3400]]></dc:creator><pubDate>Wed, 16 Sep 2009 09:07:47 GMT</pubDate></item><item><title><![CDATA[Reply to Segmentation fault, Problem mit new on Wed, 16 Sep 2009 09:08:21 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;string&gt;
using namespace std;

class GRAPH{
public:
    int degree;
    int *neighbours;
};
// globale Variablen entfernt

bool readgraph(const char *filename, int &amp;anzahl, GRAPH *&amp;graph){ // Referenzen sind doch schicker ;)
    ifstream in(filename);
	if(!in)	// wurde die Datei überhaupt gefunden?
		return false;
    in &gt;&gt; anzahl;
    cout &lt;&lt; &quot;Test: &quot; &lt;&lt; anzahl &lt;&lt; endl;
    graph = new GRAPH[anzahl];
	for(int i(0); i != anzahl; ++i) // alle Elemente initialisieren
		graph[i].degree = 1;
    return true;
}

int main(){
	std::string dateiname; // std::string
	int n;
	GRAPH *graph;
    cout &lt;&lt; &quot;Dateiname: &quot;;
	getline(cin, dateiname); // ganze Zeile lesen, falls Dateiname leerzeichen enthält
	if(readgraph(dateiname.c_str(), n, graph)) { // dank bool'schen Rückgabewert überprüfen ob es geklappt hat
		cout &lt;&lt; &quot;Es wurden &quot; &lt;&lt; n &lt;&lt; &quot; Graphen erstellt.&quot; &lt;&lt; endl;
		for(int i(0); i != n; ++i) {
			cout &lt;&lt; graph[i].degree &lt;&lt; &quot; &quot;;
		}
		delete [] graph; // nicht vergessen den Speicher auch wieder freizugeben
	} else {
		cerr &lt;&lt; &quot;Fehler beim lesen der Graphen.&quot; &lt;&lt; endl;
	}

    return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1779646</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1779646</guid><dc:creator><![CDATA[Mizar]]></dc:creator><pubDate>Wed, 16 Sep 2009 09:08:21 GMT</pubDate></item><item><title><![CDATA[Reply to Segmentation fault, Problem mit new on Wed, 16 Sep 2009 09:15:59 GMT]]></title><description><![CDATA[<p>alx3400 schrieb:</p>
<blockquote>
<p>Tachyon schrieb:</p>
<blockquote>
<pre><code class="language-cpp">graph[0]-&gt;degree=1; //sollte (*graph)[0].degree=1; sein
graph[1]-&gt;degree=1; //sollte (*graph)[1].degree=1; sein
</code></pre>
</blockquote>
<p>Das ist doch nur eine andere Schreibweise oder? ...</p>
</blockquote>
<p>Überleg' Dir mal, worauf Du jeweils den &quot;[]&quot; loslässt...</p>
<p>alx3400 schrieb:</p>
<blockquote>
<p>Jedenfalls macht das Programm genau den gleichen Quatsch in beiden Schreibweisen....</p>
</blockquote>
<p>Das liegt ziemlich sicher daran, dass Du &quot;new[*n]&quot; ausführst, ohne dass sichergestellt ist, dass n auf den richtigen Wert weist und/oder dass Du auf &quot;graph[0]&quot; und &quot;graph[1]&quot; zugreifst, ohne zu wissen, wie viele Elemente da wirklich liegen.</p>
<p>kleiner Tipp: Ich habe mein obiges Post nochmal überarbeitet ...</p>
<p>Gruß,</p>
<p>Simon2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1779651</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1779651</guid><dc:creator><![CDATA[Simon2]]></dc:creator><pubDate>Wed, 16 Sep 2009 09:15:59 GMT</pubDate></item><item><title><![CDATA[Reply to Segmentation fault, Problem mit new on Wed, 16 Sep 2009 09:17:47 GMT]]></title><description><![CDATA[<p>Vielen Dank dafür. Aber kannst du mir auch sagen, warum das bei deinem Code geht und bei meinem nicht. Die entscheidende Stelle ist doch diese oder?</p>
<pre><code class="language-cpp">in &gt;&gt; anzahl; 
    cout &lt;&lt; &quot;Test: &quot; &lt;&lt; anzahl &lt;&lt; endl; 
    graph = new GRAPH[anzahl]; 
    for(int i(0); i != anzahl; ++i) // alle Elemente initialisieren 
        graph[i].degree = 1;
</code></pre>
<p>Und die sah bei mir ja kaum anders aus. Mich wundert immer noch, wo der Fehler herkam.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1779655</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1779655</guid><dc:creator><![CDATA[alx3400]]></dc:creator><pubDate>Wed, 16 Sep 2009 09:17:47 GMT</pubDate></item><item><title><![CDATA[Reply to Segmentation fault, Problem mit new on Wed, 16 Sep 2009 09:21:29 GMT]]></title><description><![CDATA[<p>alx3400 schrieb:</p>
<blockquote>
<p>Das ist doch nur eine andere Schreibweise oder? Jedenfalls macht das Programm genau den gleichen Quatsch in beiden Schreibweisen.</p>
</blockquote>
<p>Nein. <code>graph[1]...</code> liefert Dir <code>graph** + sizeof(graph*)</code> . Das Element gibt es nicht. Hinter <code>graph</code> steckt nur genau ein Element.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1779656</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1779656</guid><dc:creator><![CDATA[Tachyon]]></dc:creator><pubDate>Wed, 16 Sep 2009 09:21:29 GMT</pubDate></item><item><title><![CDATA[Reply to Segmentation fault, Problem mit new on Wed, 16 Sep 2009 09:19:45 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">int *n
</code></pre>
<p>Das hier ist wohl auch nicht nötig. Er brauch die Länge des Arrays nicht als Pointer übergeben... Also selbst für ein &quot;früheres&quot; Ansi-C-Programm is das nich doll <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/1779658</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1779658</guid><dc:creator><![CDATA[It0101]]></dc:creator><pubDate>Wed, 16 Sep 2009 09:19:45 GMT</pubDate></item><item><title><![CDATA[Reply to Segmentation fault, Problem mit new on Wed, 16 Sep 2009 09:27:26 GMT]]></title><description><![CDATA[<p>alx3400 schrieb:</p>
<blockquote>
<p>Vielen Dank dafür. Aber kannst du mir auch sagen, warum das bei deinem Code geht und bei meinem nicht. Die entscheidende Stelle ist doch diese oder?</p>
</blockquote>
<pre><code class="language-cpp">bool readgraph(const char *filename, int &amp;anzahl, GRAPH *&amp;graph)
</code></pre>
<p>Diese Funktion erwartet nun eine Referenz auf einen Zeiger, statt wie bisher:</p>
<pre><code class="language-cpp">int readgraph(const char *filename, int *n, GRAPH **graph)
</code></pre>
<p>Wie pumuckl auch schon sagte:</p>
<p>pumuckl schrieb:</p>
<blockquote>
<p>Schau dir mal Referenzen an, das erleichtert die ganze Pointerfrickelei um einiges. Deine call-by-reference über Pointer zu machen ist C-Stil und in C++ nicht nötig.</p>
</blockquote>
<p>wird es mit Referenzen leichter.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1779662</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1779662</guid><dc:creator><![CDATA[Mizar]]></dc:creator><pubDate>Wed, 16 Sep 2009 09:27:26 GMT</pubDate></item><item><title><![CDATA[Reply to Segmentation fault, Problem mit new on Wed, 16 Sep 2009 10:10:59 GMT]]></title><description><![CDATA[<p>Tachyon schrieb:</p>
<blockquote>
<p>Nein. <code>graph[1]...</code> liefert Dir <code>graph** + sizeof(graph*)</code> .Das Element gibt es nicht. Hinter <code>graph</code> steckt nur genau ein Element.</p>
</blockquote>
<p>Ich komme zum gleichen Schluß wie du =). Aber dein Weg ist anderst und sehr interessant. Weißt du wo die [] operatoren definiert sind in c++ also z.b. iostrem oder so. Ich würde sie mir gerne anschauen.<br />
Gruß</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1779686</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1779686</guid><dc:creator><![CDATA[AlexXXx]]></dc:creator><pubDate>Wed, 16 Sep 2009 10:10:59 GMT</pubDate></item><item><title><![CDATA[Reply to Segmentation fault, Problem mit new on Wed, 16 Sep 2009 11:50:33 GMT]]></title><description><![CDATA[<p>Erstmal vielen Dank für die vielen Antworten. Werd mir das jetzt erstmal in Ruhe anschauen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1779744</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1779744</guid><dc:creator><![CDATA[alx3400]]></dc:creator><pubDate>Wed, 16 Sep 2009 11:50:33 GMT</pubDate></item><item><title><![CDATA[Reply to Segmentation fault, Problem mit new on Wed, 16 Sep 2009 13:59:42 GMT]]></title><description><![CDATA[<p>Habe nun einiges berücksichtigt, was mir hier empfohlen wurde und bin mit dem Einlesen des Graphen nun fertig. Wen es interessiert, hier der Code:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt; 
#include &lt;fstream&gt; 
#include &lt;string&gt; 
#include &lt;vector&gt;
using namespace std; 

class GRAPH{ 
public: 
    int degree; 
    std::vector&lt;int&gt; neighbours; 
}; 
int i,n;

bool readgraph(const char *filename, int &amp;n, std::vector&lt;GRAPH&gt;&amp; graph){  
	int n1,n2;
    ifstream in(filename); 
	if(!in)
		return false;
    in &gt;&gt; n; 
	graph.resize(n);
    cout &lt;&lt; &quot;Anzahl Knoten: &quot; &lt;&lt; n &lt;&lt; endl; 

    for(i=0; i &lt; n; ++i) 		//degrees mit 0 initialisieren
        graph[i].degree = 0;		

	while(!in.eof()){				//degrees der Knoten ermitteln
		in &gt;&gt; n1 &gt;&gt; n2;	
		graph[n1].degree++;			
		graph[n2].degree++;
		cout &lt;&lt; &quot;\nn1: &quot; &lt;&lt; n1 &lt;&lt; &quot;\tn2: &quot; &lt;&lt; n2;
	}

	for (i = 0; i &lt; n; i++){			//neighbour arrays initialisieren
		graph[i].neighbours.resize(graph[i].degree);
		graph[i].degree = 0;		
	}
	in.clear();						 //zurück auf Datei Anfang
	in.seekg (0, ios::beg);       
	in &gt;&gt; n;

	while (!in.eof())				//neighbour-vector füllen
      {
         in &gt;&gt; n1 &gt;&gt; n2;			
         graph[n1].neighbours[graph[n1].degree++] = n2;
         graph[n2].neighbours[graph[n2].degree++] = n1;
      }

    return true; 
} 

int print(std::vector&lt;GRAPH&gt;&amp; graph){		//Testausgabe
	for (i = 0; i &lt; n; i++){
		  int j;										
		  cout &lt;&lt; &quot;\nNeighbours of node &quot; &lt;&lt; i &lt;&lt; &quot;: &quot;;
		  for (j = 0; j &lt; graph[i].degree; j++){
			  cout &lt;&lt; &quot; &quot; &lt;&lt; graph[i].neighbours[j];
		  }
	}
	return 0;
}

int main(){ 
	std::vector&lt;GRAPH&gt; graph;
    std::string dateiname; 
    cout &lt;&lt; &quot;Dateiname: &quot;; 
    getline(cin, dateiname); // ganze Zeile lesen, falls Dateiname leerzeichen enthält 
	if(readgraph(dateiname.c_str(), n, graph)){
		print(graph);
	}
		else{ 
			cerr &lt;&lt; &quot;Datei konnte nicht geoeffnet werden&quot;;
	}
    return 0; 
}
</code></pre>
<p>Bin immer offen für Verbesserungen, aber Fragen habe ich soweit keine mehr.<br />
Danke nochmal</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1779835</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1779835</guid><dc:creator><![CDATA[alx3400]]></dc:creator><pubDate>Wed, 16 Sep 2009 13:59:42 GMT</pubDate></item><item><title><![CDATA[Reply to Segmentation fault, Problem mit new on Wed, 16 Sep 2009 14:13:15 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">#include &lt;iostream&gt; 
#include &lt;fstream&gt; 
#include &lt;string&gt; 
#include &lt;vector&gt; 
using namespace std; 

class GRAPH
{
public:
	explicit GRAPH(int _degree = 0)
	:	degree(_degree)
	{}

	int degree; 

	std::vector&lt;int&gt; neighbours;
};

bool readgraph(const char *filename, std::vector&lt;GRAPH&gt; &amp;graph)
{
    ifstream in(filename);
    if(!in) 
        return false;
	std::vector&lt;GRAPH&gt;::size_type len;
    in &gt;&gt; len;
    graph.resize(len);
    cout &lt;&lt; &quot;Anzahl Knoten: &quot; &lt;&lt; n &lt;&lt; endl; 

	for(int n1, n2; in &gt;&gt; n1 &gt;&gt; n2; )
	{                //degrees der Knoten ermitteln 
        graph.at(n1).degree++;
        graph.at(n2).degree++;
    } 

    for (i = 0; i &lt; n; i++)
	{            //neighbour arrays initialisieren 
        graph[i].neighbours.resize(graph[i].degree); 
        graph[i].degree = 0;        
    } 
    in.clear();                         //zurück auf Datei Anfang 
    in.seekg (0, ios::beg);
    in &gt;&gt; n;

	for(int n1, n2; in &gt;&gt; n1 &gt;&gt; n2; )
	{			//neighbour-vector füllen
		graph[n1].neighbours[graph[n1].degree++] = n2;
		graph[n2].neighbours[graph[n2].degree++] = n1;
	}

    return true; 
} 

void print(const std::vector&lt;GRAPH&gt;&amp; graph)
{        //Testausgabe 
    for (int i = 0; i &lt; n; i++)
	{
          cout &lt;&lt; &quot;\nNeighbours of node &quot; &lt;&lt; i &lt;&lt; &quot;: &quot;;
          for (int j = 0; j &lt; graph[i].degree; j++)
		  {
              cout &lt;&lt; &quot; &quot; &lt;&lt; graph[i].neighbours[j]; 
          } 
    } 
} 

int main()
{
    std::string dateiname; 
    cout &lt;&lt; &quot;Dateiname: &quot;; 
    getline(cin, dateiname); // ganze Zeile lesen, falls Dateiname leerzeichen enthält 
    std::vector&lt;GRAPH&gt; graph; 
    if(readgraph(dateiname.c_str(), n, graph))
        print(graph); 
	else
		cerr &lt;&lt; &quot;Datei konnte nicht geoeffnet werden&quot;; 
}
</code></pre>
<p>ab und an solltest du int noch durch den size_type ersetzen...<br />
allerdings hab ich jz nur blind iwas geändert, weil ich nicht gelesen habe, was du eigtl machen möchtest - und es mir zu kompliziert erschien, den code so lang zu untersuchen, bis ich das verstanden hab^^<br />
werd wohl auch paar fehler reingemacht haben^^</p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1779848</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1779848</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Wed, 16 Sep 2009 14:13:15 GMT</pubDate></item></channel></rss>