<?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[Speicher fuer dim x dim  Matrix anfordern]]></title><description><![CDATA[<p>Hallo zusammen,</p>
<p>ich habe mal wieder eine Frage zum Thema Speicher und zwar habe ich folgendes Programm:</p>
<pre><code>/*
#include &lt;cstdlib&gt; 
#include &lt;ctime&gt;
#include &lt;iostream&gt;
#include &lt;conio.h&gt;

using namespace std;

int main() {
  double **matrix; //Zeiger auf Zeiger auf Matrix
  int dim;

  cout &lt;&lt; &quot;Dimension eingeben: &quot;;
  cin &gt;&gt; dim;

  //Speicher fuer dim x dim  Matrix anfordern
  matrix = new (sizeof (double*)*dim);
  for(int i=0 ; i&lt;dim ; i++) {
    matrix[i] =new (sizeof (double)*dim);
  }

  //Mit Zufallswerten fuellen
  srand(time(NULL));
  for(int i=0 ; i&lt;dim ; i++) 
    for(int j=0 ; j&lt;dim ; j++) 
      matrix[i][j] = (double)rand(); 

  //Ausgeben
  cout &lt;&lt; &quot;Erzeugte Matrix: &quot; &lt;&lt; endl;
  for(int i=0 ; i&lt;dim ; i++) {
    for(int j=0 ; j&lt;dim ; j++) { 
      cout &lt;&lt; matrix[i][j] &lt;&lt; &quot; &quot;;
    }
    cout &lt;&lt; endl;
  }

  //Speicher wieder freigeben
  for(int i=0 ; i&lt;dim ; i++) {
    delete [] (matrix[i]);
  }

  delete [] (matrix);

  _getch();
  return 0;
}
</code></pre>
<p>Die Speicherzuweisung funktioniert nicht, da meine Syntax falsch ist. Könnte mir jemand mal genau erklären wie man den Speicher zuweist und was genau dabei geschieht. Das ist mir absolut unklar.</p>
<p>Vielen Dank.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/318136/speicher-fuer-dim-x-dim-matrix-anfordern</link><generator>RSS for Node</generator><lastBuildDate>Mon, 27 Jul 2026 11:52:37 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/318136.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 02 Jul 2013 15:21:32 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Speicher fuer dim x dim  Matrix anfordern on Tue, 02 Jul 2013 15:21:32 GMT]]></title><description><![CDATA[<p>Hallo zusammen,</p>
<p>ich habe mal wieder eine Frage zum Thema Speicher und zwar habe ich folgendes Programm:</p>
<pre><code>/*
#include &lt;cstdlib&gt; 
#include &lt;ctime&gt;
#include &lt;iostream&gt;
#include &lt;conio.h&gt;

using namespace std;

int main() {
  double **matrix; //Zeiger auf Zeiger auf Matrix
  int dim;

  cout &lt;&lt; &quot;Dimension eingeben: &quot;;
  cin &gt;&gt; dim;

  //Speicher fuer dim x dim  Matrix anfordern
  matrix = new (sizeof (double*)*dim);
  for(int i=0 ; i&lt;dim ; i++) {
    matrix[i] =new (sizeof (double)*dim);
  }

  //Mit Zufallswerten fuellen
  srand(time(NULL));
  for(int i=0 ; i&lt;dim ; i++) 
    for(int j=0 ; j&lt;dim ; j++) 
      matrix[i][j] = (double)rand(); 

  //Ausgeben
  cout &lt;&lt; &quot;Erzeugte Matrix: &quot; &lt;&lt; endl;
  for(int i=0 ; i&lt;dim ; i++) {
    for(int j=0 ; j&lt;dim ; j++) { 
      cout &lt;&lt; matrix[i][j] &lt;&lt; &quot; &quot;;
    }
    cout &lt;&lt; endl;
  }

  //Speicher wieder freigeben
  for(int i=0 ; i&lt;dim ; i++) {
    delete [] (matrix[i]);
  }

  delete [] (matrix);

  _getch();
  return 0;
}
</code></pre>
<p>Die Speicherzuweisung funktioniert nicht, da meine Syntax falsch ist. Könnte mir jemand mal genau erklären wie man den Speicher zuweist und was genau dabei geschieht. Das ist mir absolut unklar.</p>
<p>Vielen Dank.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2335991</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2335991</guid><dc:creator><![CDATA[Tyle]]></dc:creator><pubDate>Tue, 02 Jul 2013 15:21:32 GMT</pubDate></item><item><title><![CDATA[Reply to Speicher fuer dim x dim  Matrix anfordern on Tue, 02 Jul 2013 15:27:14 GMT]]></title><description><![CDATA[<p>Weißt du es nicht besser oder willst du aus Übungszwecken keinen std::vector verwenden?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2335996</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2335996</guid><dc:creator><![CDATA[daddy_felix]]></dc:creator><pubDate>Tue, 02 Jul 2013 15:27:14 GMT</pubDate></item><item><title><![CDATA[Reply to Speicher fuer dim x dim  Matrix anfordern on Tue, 02 Jul 2013 15:31:29 GMT]]></title><description><![CDATA[<p>Ich muss aus Übungszwecken new und delete verwenden, ansonsten würde ich das auch nicht nutzen.</p>
<p>Ich muss nur wissen wie das genau funktioniert, da ich eine Folgeaufgabe habe wo ich den Speicher wieder reservieren muss.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2335997</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2335997</guid><dc:creator><![CDATA[Tyle]]></dc:creator><pubDate>Tue, 02 Jul 2013 15:31:29 GMT</pubDate></item><item><title><![CDATA[Reply to Speicher fuer dim x dim  Matrix anfordern on Tue, 02 Jul 2013 15:46:02 GMT]]></title><description><![CDATA[<p>Fang doch mal mit einer Dimension an.</p>
<p>Z.B. 42 ints mit zufälligem Inhalt. Bekommst Du das hin?</p>
<p>Schau noch mal in Deine Unterlagen. Diese Syntax ist komplett für die Füße</p>
<pre><code class="language-cpp">matrix = new (sizeof (double*)*dim);
</code></pre>
<p>Und damit meine ich Deine Kursunterlagen zu C++ - mir scheint Du spickst bei C Tutorials o.ä..</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2336000</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2336000</guid><dc:creator><![CDATA[Furble Wurble]]></dc:creator><pubDate>Tue, 02 Jul 2013 15:46:02 GMT</pubDate></item><item><title><![CDATA[Reply to Speicher fuer dim x dim  Matrix anfordern on Tue, 02 Jul 2013 15:47:33 GMT]]></title><description><![CDATA[<pre><code>int main()
{
	unsigned x;
	cin &gt;&gt; x;
	double** matrix = new double*[x];

	unsigned y;
	cin &gt;&gt; y;
	for(unsigned i=0; i!=x; ++i)
	{
		matrix[i] = new double[y];
	}

	for(unsigned i=0; i!=x; ++i)
	{
		delete matrix[i];
	}
	delete matrix;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2336001</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2336001</guid><dc:creator><![CDATA[out]]></dc:creator><pubDate>Tue, 02 Jul 2013 15:47:33 GMT</pubDate></item><item><title><![CDATA[Reply to Speicher fuer dim x dim  Matrix anfordern on Tue, 02 Jul 2013 15:50:55 GMT]]></title><description><![CDATA[<p>Warum nicht x*y an einem Stück?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2336003</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2336003</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Tue, 02 Jul 2013 15:50:55 GMT</pubDate></item><item><title><![CDATA[Reply to Speicher fuer dim x dim  Matrix anfordern on Tue, 02 Jul 2013 16:02:57 GMT]]></title><description><![CDATA[<p>Meinst du sowas:</p>
<pre><code>int groesse = 42;
int *pArray;
pArray = new int[groesse];

delete [] pArray;
</code></pre>
<p>Und gespickt habe ich da nicht. Das Problem ist das new und delet bei uns auch nur in ca. 10 Zeilen Code erklärt wird.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2336005</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2336005</guid><dc:creator><![CDATA[Tyle]]></dc:creator><pubDate>Tue, 02 Jul 2013 16:02:57 GMT</pubDate></item><item><title><![CDATA[Reply to Speicher fuer dim x dim  Matrix anfordern on Tue, 02 Jul 2013 17:24:55 GMT]]></title><description><![CDATA[<p>Entsprechenden Include-Pfad für <a href="http://eigen.tuxfamily.org/index.php?title=Main_Page" rel="nofollow">Eigen</a> vorausgesetzt:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;Eigen/Dense&gt;

using std::cout;
using Eigen::MatrixXd;

int main()
{
  MatrixXd mat = MatrixXd::Identity(9,9)*23;
  cout &lt;&lt; mat(0,0) &lt;&lt; '\n';
  cout &lt;&lt; mat(1,0) &lt;&lt; '\n';
}
</code></pre>
<p>(ungetestet)</p>
<p>Man kann sich aber natürlich auch das Leben schwer machen und dieses Rad schlecht nachbauen. <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>
]]></description><link>https://www.c-plusplus.net/forum/post/2336018</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2336018</guid><dc:creator><![CDATA[krümelkacker]]></dc:creator><pubDate>Tue, 02 Jul 2013 17:24:55 GMT</pubDate></item><item><title><![CDATA[Reply to Speicher fuer dim x dim  Matrix anfordern on Tue, 02 Jul 2013 18:05:43 GMT]]></title><description><![CDATA[<p>Unnötige, schlechte Eigennachbauten von Standardklassen? Ich höre den Ruf und eile herbei!</p>
<pre><code>template&lt;typename ElementType&gt; class MyUnnecessaryAndTediousAndBadCloneOfStdVectorThatOnlyWorksForDefaultConstructibleTypesBecauseIAmLazy
{
private:
  ElementType *data;  
  unsigned long size;
public:
  MyUnnecessaryAndTediousAndBadCloneOfStdVectorThatOnlyWorksForDefaultConstructibleTypesBecauseIAmLazy(unsigned long size): data(new ElementType[size]), size(size) { }
  ~MyUnnecessaryAndTediousAndBadCloneOfStdVectorThatOnlyWorksForDefaultConstructibleTypesBecauseIAmLazy() { delete[] data; }
  MyUnnecessaryAndTediousAndBadCloneOfStdVectorThatOnlyWorksForDefaultConstructibleTypesBecauseIAmLazy
  (const MyUnnecessaryAndTediousAndBadCloneOfStdVectorThatOnlyWorksForDefaultConstructibleTypesBecauseIAmLazy&amp; other):
    data(new ElementType[other.size]), size(other.size)
  {
    // My unnecessary, bad and tedious clone of std::copy
    // which is not even the correct way of doing this 
    // (should get uninitialized memory and use something 
    // like uninitalized_copy)
    for (unsigned long i = 0; i &lt; size; ++i)
      data[i] = other.data[i];
  }
  MyUnnecessaryAndTediousAndBadCloneOfStdVectorThatOnlyWorksForDefaultConstructibleTypesBecauseIAmLazy&amp; operator=
(MyUnnecessaryAndTediousAndBadCloneOfStdVectorThatOnlyWorksForDefaultConstructibleTypesBecauseIAmLazy other)
  {
    // My unnecessary, bad and tedious clone of std::swap
    ElementType* temp = data;
    data = other.data;
    other.data = temp;
    unsigned long temp2 = size;
    size = other.size;
    other.size = temp2;

    return *this;
  }

  ElementType&amp; operator[](unsigned long i) { return data[i]; }
  const ElementType&amp; operator[] (unsigned long i) const { return data[i]; }
};

template &lt;typename ElementType&gt; class Array2D
{
private:
  MyUnnecessaryAndTediousAndBadCloneOfStdVectorThatOnlyWorksForDefaultConstructibleTypesBecauseIAmLazy&lt;ElementType&gt; data;
  unsigned long size_x;

 public:
  Array2D(unsigned long size_x, unsigned long size_y) : data(size_x * size_y), size_x(size_x) {}
  ElementType&amp; operator()(unsigned long x, unsigned long y) { return data[x + y * size_x]; }
  const ElementType&amp; operator() (unsigned long x, unsigned long y) const { return data[x + y * size_x]; }  
};

#include &lt;iostream&gt;
int main()
{
  using std::cout;
  Array2D&lt;int&gt; foo(4,5);

  int i = 0;
  for (int x = 0; x &lt; 4; ++x)
    for (int y = 0; y &lt; 5; ++y)
      foo(x,y) = ++i;

  // Ein bisschen RAII testen:
  Array2D&lt;int&gt; bar(foo);
  Array2D&lt;int&gt; foobar(7,3);
  foobar = bar;

  for (int x = 0; x &lt; 4; ++x)
    {
      for (int y = 0; y &lt; 5; ++y)
        cout &lt;&lt; foobar(x,y) &lt;&lt; &quot; &quot;;
      cout &lt;&lt; '\n';
    }
}
</code></pre>
<p>cout habe ich jetzt mal nicht nachgebaut, weil das irgendwann auf systemabhängiger Ebene geendet hätte.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2336027</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2336027</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Tue, 02 Jul 2013 18:05:43 GMT</pubDate></item><item><title><![CDATA[Reply to Speicher fuer dim x dim  Matrix anfordern on Tue, 02 Jul 2013 18:17:30 GMT]]></title><description><![CDATA[<p>Du hast <code>operator new</code> nicht redefiniert.<br />
Klares Fail.<br />
<img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f921.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--clown_face"
      title=":clown:"
      alt="🤡"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2336032</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2336032</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Tue, 02 Jul 2013 18:17:30 GMT</pubDate></item><item><title><![CDATA[Reply to Speicher fuer dim x dim  Matrix anfordern on Tue, 02 Jul 2013 18:54:28 GMT]]></title><description><![CDATA[<p>Tyle schrieb:</p>
<blockquote>
<pre><code>int groesse = 42;
int *pArray;
pArray = new int[groesse];

delete [] pArray;
</code></pre>
<p>Und gespickt habe ich da nicht. Das Problem ist das new und delet bei uns auch nur in ca. 10 Zeilen Code erklärt wird.</p>
</blockquote>
<p>Das sieht doch gut aus.</p>
<p>Wieso lässt Du Dich dann so verwirren, dass Du den ersten Code schreibst?</p>
<p>Was Du jetzt hast ist ein Array von 42 ints.</p>
<p>Sagen wir ich wollte davon fünfzigtausend Stück haben.<br />
Ergo brauche ich fünfzigtausend Zeiger wie <code>pArray</code> , die jeweils auf 42 ints zeigen.<br />
Da <code>pArray</code> ein <code>int*</code> (<em>&quot;Zeiger auf int&quot;</em>) ist hat ein Zeiger auf ein solches Array den Typ <code>int**</code> (<em>&quot;Zeiger auf Zeiger auf int&quot;</em>).<br />
In einem ersten Schritt alloziere ich die 50k Zeiger-auf-Zeiger und in einem weiteren für jeden von diesen 42 ints.</p>
<pre><code class="language-cpp">int main(){
  const int dim1=50000, dim0=42;
  int** ppArray = new int*[dim1]; // Schritt 1: Speicher fuer dim1 mal int*
  for(int i=0; i&lt;dim1; ++i)       // Schritt 2: dim1 mal Speicher fuer dim0 ints
    ppArray[i]=new int[dim0];
  //....
  for(int i=0; i&lt;dim1; ++i)
    delete[] ppArray[i];
  delete[] ppArray;
}
</code></pre>
<p>Beachte, dass hinter <code>new</code> ein Typ steht: <code>new int*[size];</code> bedeutet ein Array von int*.<br />
<code>new double**[size];</code> ist dementsprechend ein Array von <code>double**</code><br />
Deine fehlerhafte Syntax sah so aus, als ob Du irgendwas mit einer Größe multiplizieren wolltest um den Speicherbedarf anzugeben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2336043</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2336043</guid><dc:creator><![CDATA[Furble Wurble]]></dc:creator><pubDate>Tue, 02 Jul 2013 18:54:28 GMT</pubDate></item><item><title><![CDATA[Reply to Speicher fuer dim x dim  Matrix anfordern on Tue, 02 Jul 2013 18:57:02 GMT]]></title><description><![CDATA[<p>SeppJ schrieb:</p>
<blockquote>
<p>Warum nicht <s>x*y</s> dim0*dim1 an einem Stück?</p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/post/2336046</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2336046</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Tue, 02 Jul 2013 18:57:02 GMT</pubDate></item><item><title><![CDATA[Reply to Speicher fuer dim x dim  Matrix anfordern on Tue, 02 Jul 2013 19:12:34 GMT]]></title><description><![CDATA[<p>SeppJ schrieb:</p>
<blockquote>
<p>SeppJ schrieb:</p>
<blockquote>
<p>Warum nicht <s>x*y</s> dim0*dim1 an einem Stück?</p>
</blockquote>
</blockquote>
<p>Kannst Du dafür nicht einen eigenen Thread aufmachen, wenn Du das diskutieren willst?<br />
Ich schätze dem OP wäre damit nicht bei seinem akuten Problem geholfen.</p>
<p>Aber schreib doch die paar Zeilen. Besser als zweimal den gleichen Halbsatz zu posten wird's allemal.</p>
<p>Go for it!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2336054</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2336054</guid><dc:creator><![CDATA[Furble Wurble]]></dc:creator><pubDate>Tue, 02 Jul 2013 19:12:34 GMT</pubDate></item><item><title><![CDATA[Reply to Speicher fuer dim x dim  Matrix anfordern on Tue, 02 Jul 2013 19:20:28 GMT]]></title><description><![CDATA[<p>Habe ich oben ja schon. Ob man da nun MyUnnecessaryAndTediousAndBadCloneOfStdVectorThatOnlyWorksForDefaultConstructibleTypesBecauseIAmLazy oder einen anderen Container einsetzt ist bewusst anpassbar gelassen.</p>
<p>Es ist aber halt ziemliche Verschwendung, das nicht alles an einem Stück anzufordern. Das würde man niemals so machen, weil es lauter dicke Nachteile hat und man hier keinen der Vorteile benutzt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2336057</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2336057</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Tue, 02 Jul 2013 19:20:28 GMT</pubDate></item><item><title><![CDATA[Reply to Speicher fuer dim x dim  Matrix anfordern on Tue, 02 Jul 2013 19:46:09 GMT]]></title><description><![CDATA[<blockquote>
<p>Wieso lässt Du Dich dann so verwirren, dass Du den ersten Code schreibst?</p>
</blockquote>
<p>Das war vorher ein C-Programm mit malloc und free. Darin habe ich dann stümperhaft rumgewerkelt.</p>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/19375">@SeppJ</a> Kannst ja mal ne Beispiel für ne Code am Stück liefern.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2336068</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2336068</guid><dc:creator><![CDATA[Tyle]]></dc:creator><pubDate>Tue, 02 Jul 2013 19:46:09 GMT</pubDate></item><item><title><![CDATA[Reply to Speicher fuer dim x dim  Matrix anfordern on Tue, 02 Jul 2013 20:00:47 GMT]]></title><description><![CDATA[<p>Ok ich glaube ich habe es nun. Scheint auch soweit zu funktionieren bekomme nur eine Warnug zu der time_t Funktion.</p>
<p>Warnung:<br />
quelle.cpp(35): warning C4244: 'Argument': Konvertierung von 'time_t' in 'unsigned int', möglicher Datenverlust</p>
<pre><code>#include &lt;cstdlib&gt; 
#include &lt;ctime&gt;
#include &lt;iostream&gt;
#include &lt;conio.h&gt;

using namespace std;

int main() {
  double **matrix; //Zeiger auf Zeiger auf Matrix
  int dim;

  cout &lt;&lt; &quot;Dimension eingeben: &quot;;
  cin &gt;&gt; dim;

  //Speicher fuer dim x dim  Matrix anfordern
  matrix = new double*[dim];		//Speicher fuer dim mal int*
  for(int i=0; i&lt;dim; ++i)			//dim1 mal Speicher fuer dim ints
    matrix[i]=new double[dim];

  //Mit Zufallswerten fuellen
  srand(time(NULL));
  for(int i=0 ; i&lt;dim ; i++) 
    for(int j=0 ; j&lt;dim ; j++) 
      matrix[i][j] = (double)rand(); 

  //Ausgeben
  cout &lt;&lt; &quot;Erzeugte Matrix: &quot; &lt;&lt; endl;
  for(int i=0 ; i&lt;dim ; i++) {
    for(int j=0 ; j&lt;dim ; j++) { 
      cout &lt;&lt; matrix[i][j] &lt;&lt; &quot; &quot;;
    }
    cout &lt;&lt; endl;
  }

  //Speicher wieder freigeben
   for(int i=0; i&lt;dim; ++i)
    delete[] matrix[i];
  delete[] matrix;

  _getch();
  return 0;
}
</code></pre>
<p>Jetzt kann ich mich an was komplizierteres wagen um zu sehen ob ich das verstanden habe. Danke nochmals für eure Hilfe.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2336078</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2336078</guid><dc:creator><![CDATA[Tyle]]></dc:creator><pubDate>Tue, 02 Jul 2013 20:00:47 GMT</pubDate></item><item><title><![CDATA[Reply to Speicher fuer dim x dim  Matrix anfordern on Tue, 02 Jul 2013 20:10:50 GMT]]></title><description><![CDATA[<p>Tyle schrieb:</p>
<blockquote>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/19375">@SeppJ</a> Kannst ja mal ne Beispiel für ne Code am Stück liefern.</p>
</blockquote>
<p>SeppJ schrieb:</p>
<blockquote>
<p>Habe ich oben ja schon. Ob man da nun MyUnnecessaryAndTediousAndBadCloneOfStdVectorThatOnlyWorksForDefaultConstructibleTypesBecauseIAmLazy oder einen anderen Container einsetzt ist bewusst anpassbar gelassen.</p>
</blockquote>
<p>SeppJ schrieb:</p>
<blockquote>
<pre><code>template &lt;typename ElementType&gt; class Array2D
{
private:
  HierirgendetwasdasElementTypespeichernkannzurnotaucheinenpointermitnew data;
  unsigned long size_x;
 
 public:
  Array2D(unsigned long size_x, unsigned long size_y) : data(size_x * size_y), size_x(size_x) {}
  ElementType&amp; operator()(unsigned long x, unsigned long y) { return data[x + y * size_x]; }
  const ElementType&amp; operator() (unsigned long x, unsigned long y) const { return data[x + y * size_x]; }  
};
</code></pre>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/post/2336084</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2336084</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Tue, 02 Jul 2013 20:10:50 GMT</pubDate></item><item><title><![CDATA[Reply to Speicher fuer dim x dim  Matrix anfordern on Tue, 02 Jul 2013 21:26:14 GMT]]></title><description><![CDATA[<p>Ok ich bin schon ein ganzes Stück weitergekommen aber es werden nach beenden der .exe noch Fehler angezeigt, bzw. da Programm stürzt dann ab.</p>
<p>Auch kann ich keine Dimension größer als 9 benutzen, warum weiss ich nicht.</p>
<p>Code ist noch ein wenig holprig, aber bin ja noch am lernen <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f644.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_rolling_eyes"
      title=":rolling_eyes:"
      alt="🙄"
    /></p>
<p>Hier mal das Programm:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;iomanip&gt;
#include &lt;cmath&gt;
#include &lt;conio.h&gt;
#define M_PI 3.14159265358979323846	//M_PI wirk komischerweise vom Compiler nicht definiert

using namespace std;

// Template Prototypefunktion //
template &lt;typename T&gt; T dot(T *x, T *y, int n);

int main() {
  //Vektoren
  int *xi, *yi;
  float *xf, *yf;
  double *xd, *yd;
  bool *xb, *yb;

  //Matrizen
  int **I;
  float **F;
  double **D1;
  double **D2;

  int n;
  cout &lt;&lt; &quot;Bitte Dimension n eingeben: &quot;;
  cin &gt;&gt; n;
  //const float M_PI = 3.141;

  //Speicher reservieren
  D1 = new double *[n];
  for(int i=0; i&lt;n; ++i)
		D1[i]=new double[n];

  D2 = new double *[n];
  for(int i=0; i&lt;n; ++i)
		D2[i]=new double[n];

  I = new int *[n];
  for(int i=0; i&lt;n; ++i)
		I[i]=new int[n];

  F = new float *[n];
  for(int i=0; i&lt;n; ++i)
		F[i]=new float[n];

  xi = new int;
  yi = new int;
  xf = new float;
  yf = new float;
  xd = new double;
  yd = new double;
  xb = new bool;
  yb = new bool;

  //Vektoren und Matrizen mit Werten fuellen  
  for(int i=0 ; i&lt;n ; i++) {
    xi[i] = i+1;    yi[i] = i-1;
    xf[i] = 0.1f*i; yf[i] = 0.25f*i;
    xd[i] = M_PI*i; yd[i] = 1e-10*i;
    xb[i] = (i%2 == 0); yb[i] = (i%3 == 0);

    for(int j=0 ; j&lt;n ; j++) {
      if(i==j) I[i][j] = 1; else I[i][j] = 0;
      if(i&gt;j)  F[i][j] = 0.2*i; else F[i][j] = 0.2*j;
      D1[i][j] = M_PI*(i+j);
    }

  }

  cout &lt;&lt; setprecision(2);
  cout &lt;&lt; &quot;Skalarprodukt von int Vektoren: &quot;    &lt;&lt; dot(xi,yi,n) &lt;&lt; endl;
  cout &lt;&lt; &quot;Skalarprodukt von float Vektoren: &quot;  &lt;&lt; dot(xf,yf,n) &lt;&lt; endl;
  cout &lt;&lt; &quot;Skalarprodukt von double Vektoren: &quot; &lt;&lt; dot(xd,yd,n) &lt;&lt; endl;

  //Speicher wieder freigeben
  for(int i=0; i&lt;n; ++i)
    delete[] D1[i];
  delete[] D1;

  for(int i=0; i&lt;n; ++i)
    delete[] D2[i];
  delete[] D2;

  for(int i=0; i&lt;n; ++i)
	delete[] I[i];
  delete[] I;

  for(int i=0; i&lt;n; ++i)
	delete[] F[i];
  delete[] F;

  delete xi;
  delete yi;
  delete xf;
  delete yf;
  delete xd;
  delete yd;
  delete xb;
  delete yb;

  _getch();

  return 0;

}

// Template Funktion Definition //
template &lt;typename T&gt; T dot(T *x, T *y, int n) {
	T res = 0;
	for (int i=0; i&lt;n; i++)
		res += x[i]*y[i];
	return  res;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2336117</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2336117</guid><dc:creator><![CDATA[Tyle]]></dc:creator><pubDate>Tue, 02 Jul 2013 21:26:14 GMT</pubDate></item><item><title><![CDATA[Reply to Speicher fuer dim x dim  Matrix anfordern on Tue, 02 Jul 2013 21:28:27 GMT]]></title><description><![CDATA[<p>Erklär mal, was du dir bei Zeile 47 ff. gedacht hast.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2336118</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2336118</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Tue, 02 Jul 2013 21:28:27 GMT</pubDate></item><item><title><![CDATA[Reply to Speicher fuer dim x dim  Matrix anfordern on Tue, 02 Jul 2013 21:34:56 GMT]]></title><description><![CDATA[<p>Nun ja ich habe am anfang nur die Matritzen mit new belegt und der Kompiler hat immer die Variablen angemeckert.</p>
<p>Habe daher ne bissel mit trail-and-error rumexperimentiert.</p>
<p>Nachdem ich Speicher für die Variablen allokiert habe hat das Programm soweit (bis das Ende) funktioniert.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2336122</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2336122</guid><dc:creator><![CDATA[Tyle]]></dc:creator><pubDate>Tue, 02 Jul 2013 21:34:56 GMT</pubDate></item><item><title><![CDATA[Reply to Speicher fuer dim x dim  Matrix anfordern on Tue, 02 Jul 2013 21:40:42 GMT]]></title><description><![CDATA[<p>Tyle schrieb:</p>
<blockquote>
<p>Habe daher ne bissel mit trail-and-error rumexperimentiert.</p>
</blockquote>
<p>Uralter, aber absolut wahrer Spruch hier aus dem Forum:</p>
<p>c.rackwitz schrieb:</p>
<blockquote>
<p>Wenn du selber Code schreibst, musst du ihn auch verstehen. Code ist kein Haufen von wahllos zusammengeschmissenen Buchstaben und Zeichen, Code ist Logik pur. <em>Du</em> musst <strong>genau</strong> wissen, <em>warum</em> du <em>wo</em> und <em>welches</em> Zeichen setzt.</p>
</blockquote>
<p>Studiere diese Aussage genau und lerne daraus.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2336124</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2336124</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Tue, 02 Jul 2013 21:40:42 GMT</pubDate></item><item><title><![CDATA[Reply to Speicher fuer dim x dim  Matrix anfordern on Tue, 02 Jul 2013 21:47:03 GMT]]></title><description><![CDATA[<p>Mit ist schon klar das ich das Programm noch nicht verstanden habe, daher versuche ich das ganze ja zu verstehen.</p>
<p>Meine Materialien sind leider in Bezug auf new und delet sehr sehr dürftig. Muss daher leider viel ausprobieren.</p>
<p>Wäre super wenn mir jemand erklären könnte was ich genau falsch gemacht habe und die Logik dahinter klar macht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2336126</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2336126</guid><dc:creator><![CDATA[Tyle]]></dc:creator><pubDate>Tue, 02 Jul 2013 21:47:03 GMT</pubDate></item><item><title><![CDATA[Reply to Speicher fuer dim x dim  Matrix anfordern on Wed, 03 Jul 2013 06:54:10 GMT]]></title><description><![CDATA[<p>Tyle schrieb:</p>
<blockquote>
<p>Meine Materialien sind leider in Bezug auf new und delet sehr sehr dürftig. Muss daher leider viel ausprobieren.</p>
</blockquote>
<p><code>new</code> und <code>delete</code> sind ja auch keine Raketentechnik, da muss man gar nicht soviel schreiben.<br />
Subtil sind allerdings die Anforderungen, die man erfüllen muss, damit man keine Speicherlecks hat. Aber auch dafür haben sich mittlerweile Lösungen gefunden - meistens in der Form, dass man <strong>kein</strong> <code>new</code> und <code>delete</code> benutzt.<br />
State of the Art sind dagegen Handles: <code>std::string</code> , <code>std::vector</code> usw. usf..</p>
<p>Auch auf die Gefahr hin mich zu wiederholen:<br />
<code>new</code> gibt einen Zeiger auf allozierten Speicher zurück.<br />
Der zurückgebene Zeiger hat einen Typ abhängig vom allozierten Objekt.</p>
<p>Für eine einzige Instanz ist die Form<br />
<code>auto a = new int; // a ist int*</code><br />
für ein Array ist die Form:<br />
<code>auto aa = new double[12]; // aa ist double*, aa zeigt auf's erste Element</code></p>
<p>So allozierten Speicher gibt man nach Gebrauch mit <code>delete</code> wieder frei.<br />
<code>delete a; // a von oben</code><br />
, bzw.<br />
<code>delete[] aa; // aa von oben</code></p>
<p>So und jetzt kommst Du und erklärst uns: was Du Dir bei Zeile 47ff gedacht hast.<br />
Bzw. was Du jetzt darüber denkst.<br />
Zur Erinnerung: Du weisst, wie Du Arrays (aka. Vektoren) mit <code>new</code> anlegst.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2336168</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2336168</guid><dc:creator><![CDATA[Furble Wurble]]></dc:creator><pubDate>Wed, 03 Jul 2013 06:54:10 GMT</pubDate></item><item><title><![CDATA[Reply to Speicher fuer dim x dim  Matrix anfordern on Wed, 03 Jul 2013 14:48:13 GMT]]></title><description><![CDATA[<p>Also ich glaube ich habe den Fehler gefunden:</p>
<p>Vorher habe ich mit</p>
<pre><code>xi = new int;
</code></pre>
<p>nur Speicher für einen Int Wert reserviert. Was natürlich totaler Quatsch ist, das xi ein Zeiger vom Typ int ist und somit ein Array da ein Array ja auch nur ein Zeiger auf das erste Element des Arrays ist.</p>
<p>An der Stelle hätte ich es eigentlich schon sehen sollen, das die Werte als Array befüllt werden:</p>
<pre><code>xi[i] = i+1;    yi[i] = i-1;
    xf[i] = 0.1f*i; yf[i] = 0.25f*i;
    xd[i] = M_PI*i; yd[i] = 1e-10*i;
    xb[i] = (i%2 == 0); yb[i] = (i%3 == 0);
</code></pre>
<p>Klar das meine vorherige Zuweisung das Programm zum Absturz gebracht hat, da ja kein Speicher, bzw. viel zu wenig Speicher zugewiesen wurde.</p>
<p>Somit andert sich das Programm wie folgt:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;iomanip&gt;
#include &lt;cmath&gt;
#include &lt;conio.h&gt;
#define M_PI 3.14159265358979323846	

using namespace std;

// Template Funktion Prototype //
template &lt;typename T&gt; T dot(T *x, T *y, int n);

int main() {
  //Vektoren
  int *xi, *yi;
  float *xf, *yf;
  double *xd, *yd;
  bool *xb, *yb;

  //Matrizen
  int **I;
  float **F;
  double **D1;
  double **D2;

  //Teilaufgabe a)
  int n;
  cout &lt;&lt; &quot;Bitte Dimension n eingeben: &quot;;
  cin &gt;&gt; n;
  //const float M_PI = 3.141;

  //Speicher reservieren
  D1 = new double *[n];
  for(int i=0; i&lt;n; ++i)
		D1[i]=new double[n];

  D2 = new double *[n];
  for(int i=0; i&lt;n; ++i)
		D2[i]=new double[n];

  I = new int *[n];
  for(int i=0; i&lt;n; ++i)
		I[i]=new int[n];

  F = new float *[n];
  for(int i=0; i&lt;n; ++i)
		F[i]=new float[n];

  xi = new int[n];
  yi = new int[n];
  xf = new float[n];
  yf = new float[n];
  xd = new double[n];
  yd = new double[n];
  xb = new bool[n];
  yb = new bool[n];

  //Vektoren und Matrizen mit Werten fuellen  
  for(int i=0 ; i&lt;n ; i++) {
    xi[i] = i+1;    yi[i] = i-1;
    xf[i] = 0.1f*i; yf[i] = 0.25f*i;
    xd[i] = M_PI*i; yd[i] = 1e-10*i;
    xb[i] = (i%2 == 0); yb[i] = (i%3 == 0);

    for(int j=0 ; j&lt;n ; j++) {
      if(i==j) I[i][j] = 1; else I[i][j] = 0;
      if(i&gt;j)  F[i][j] = 0.2*i; else F[i][j] = 0.2*j;
      D1[i][j] = M_PI*(i+j);
    }

  }

  cout &lt;&lt; setprecision(2);

  cout &lt;&lt; &quot;Skalarprodukt von int Vektoren: &quot;    &lt;&lt; dot(xi,yi,n) &lt;&lt; endl;
  cout &lt;&lt; &quot;Skalarprodukt von float Vektoren: &quot;  &lt;&lt; dot(xf,yf,n) &lt;&lt; endl;
  cout &lt;&lt; &quot;Skalarprodukt von double Vektoren: &quot; &lt;&lt; dot(xd,yd,n) &lt;&lt; endl;

  //Speicher wieder freigeben
  for(int i=0; i&lt;n; ++i)
    delete[] D1[i];
  delete[] D1;

  for(int i=0; i&lt;n; ++i)
    delete[] D2[i];
  delete[] D2;

  for(int i=0; i&lt;n; ++i)
	delete[] I[i];
  delete[] I;

  for(int i=0; i&lt;n; ++i)
	delete[] F[i];
  delete[] F;

  delete [] xi;
  delete [] yi;
  delete [] xf;
  delete [] yf;
  delete [] xd;
  delete [] yd;
  delete [] xb;
  delete [] yb;

  _getch();

  return 0;

}

// Template Funktion Definition //
template &lt;typename T&gt; T dot(T *x, T *y, int n) {
	T res = 0;
	for (int i=0; i&lt;n; i++)
		res += x[i]*y[i];
	return  res;
}
</code></pre>
<p>Sehe ich das so richtig?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2336313</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2336313</guid><dc:creator><![CDATA[Tyle]]></dc:creator><pubDate>Wed, 03 Jul 2013 14:48:13 GMT</pubDate></item><item><title><![CDATA[Reply to Speicher fuer dim x dim  Matrix anfordern on Wed, 03 Jul 2013 15:08:42 GMT]]></title><description><![CDATA[<p>Ja, aber ein Array ist kein Zeiger!<br />
Ein Array kann in einen Zeiger auf des erste Element konvertiert werden, aber es ist kein Zeiger!<br />
Und was spricht gegen std::vector?<br />
Denn überlege mal, was passiert, wenn eine Speicheranforderung fehlschlägt...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2336316</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2336316</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Wed, 03 Jul 2013 15:08:42 GMT</pubDate></item><item><title><![CDATA[Reply to Speicher fuer dim x dim  Matrix anfordern on Wed, 03 Jul 2013 15:15:45 GMT]]></title><description><![CDATA[<p>Nathan schrieb:</p>
<blockquote>
<p>Und was spricht gegen std::vector?</p>
</blockquote>
<p>darf er nicht benutzen, hatte ich auf der ersten Seite schon gefragt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2336320</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2336320</guid><dc:creator><![CDATA[daddy_felix]]></dc:creator><pubDate>Wed, 03 Jul 2013 15:15:45 GMT</pubDate></item></channel></rss>