<?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[Dynamische Speicherverwaltung]]></title><description><![CDATA[<p>Hallo.</p>
<p>Ich schreibe gerade ein Programm, wo ich ein bisschen mit den Sortieralgorithmen herumexperimentieren will.</p>
<p>Darum will ich mir auf dem Bildschirm 500 Pixel zeichnen lassen mit dem Index als x-Koordinate und einer Zufallszahl als y-Koordinate und will diese Dann sortieren und jeden Schritt neu zeichnen lassen.</p>
<p>Ich bin jetzt soweit, dass ich die Zufallszahlen erzeugt habe und das Anfangsbild zeichnen kann, aber sobald das geschehen ist kommt eine Fehlermeldung:</p>
<p>Die Anweisung in &quot;0x004012fc&quot; verweist auf Speicher in &quot;0x00000004&quot;. Der Vorgang &quot;read&quot; konnte nicht auf dem SPeicher durchgeführt werden.<br />
Klicken Sie auf &quot;OK&quot; um das Programm zu beenden.<br />
Klicken sie auf &quot;Abbrechen&quot;, um das Programm zu debuggen.</p>
<p>Wenn ich auf abbrechen klicke, kommt die Fehlermeldung nochmal, nur ohne Abbrechen-Button.</p>
<p>Ich schicke einfach mal meinen Code und ich bin mir ziemlich sicher, dass das an der Speicherverwaltung liegt, da bin ich mir immer etwas unsicher wenn ich damit arbeite:</p>
<pre><code class="language-cpp"># include &lt;windows.h&gt;

# define MAX_PIXELS 500

TCHAR szAppName[]=TEXT(&quot;mSort&quot;);

LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);
int *CreateRandomPixels(int);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
				   PSTR lpCmdLine, int iCmdShow)
{
	HWND hwnd;
	WNDCLASS wndclass;
	MSG msg;

	wndclass.style = CS_VREDRAW | CS_HREDRAW;
	wndclass.lpfnWndProc = WindowProc;
	wndclass.cbClsExtra = 0;
	wndclass.cbWndExtra = 0;
	wndclass.hInstance = hInstance;
	wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
	wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
	wndclass.lpszMenuName = NULL;
	wndclass.lpszClassName = szAppName;

	if(!RegisterClass(&amp;wndclass))
	{
		MessageBox(NULL, 
			TEXT(&quot;Fehler: Programm arbeitet mit Unicode und setzt Windows NT vorraus&quot;),
			szAppName, MB_ICONERROR);
		return 0;
	};

	hwnd = CreateWindow (szAppName,
						 szAppName,
						 WS_OVERLAPPEDWINDOW,
						 100,
						 100,
						 700,
						 640,
						 NULL,
						 NULL,
						 hInstance,
						 NULL);

	ShowWindow(hwnd, iCmdShow);
	UpdateWindow(hwnd);

	while(GetMessage(&amp;msg, NULL, 0, 0))
	{
		TranslateMessage(&amp;msg);
		DispatchMessage(&amp;msg);
	};

	return msg.wParam;
};

LRESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	HWND hButtonGo;
	HDC hdc;
	int i;
	static int *pixels;

	switch(message)
	{
	case WM_PAINT:
		pixels = CreateRandomPixels(MAX_PIXELS);
		hdc = GetDC(hwnd);
		for(i=0;i&lt;MAX_PIXELS;i++)
		{
			SetPixel(hdc, i+20, pixels[i]+20, 0);
		}
		ReleaseDC(hwnd, hdc);
	case WM_CREATE:
		hButtonGo = CreateWindow (TEXT(&quot;button&quot;),
								  TEXT(&quot;Sort&quot;),
								  WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
								  640, 20, 80, 20,
								  hwnd,
								  (HMENU) 1,
								  ((LPCREATESTRUCT)lParam)-&gt;hInstance,
								  NULL);
		return 0;

	case WM_DESTROY:
		PostQuitMessage(0);
		delete[] pixels;
		return 0;
	};
	return DefWindowProc(hwnd, message, wParam, lParam);
};

int *CreateRandomPixels(int max)
{
	int i;
	int *randpixels = new int[max];

	for(i=0;i&lt;max;i++)
	{
		randpixels[i] = rand()%MAX_PIXELS;
	};
	return randpixels;
};
</code></pre>
<p>Ach ja: Die Pixel werden wunderbar gezeichnet, erst dann kommt die Fehlermeldung.</p>
<p>Bitte helft mir.</p>
<p>Danke schonmal<br />
Gruß<br />
Max</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/182131/dynamische-speicherverwaltung</link><generator>RSS for Node</generator><lastBuildDate>Tue, 07 Jul 2026 22:04:06 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/182131.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 21 May 2007 18:38:53 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Dynamische Speicherverwaltung on Mon, 21 May 2007 18:38:53 GMT]]></title><description><![CDATA[<p>Hallo.</p>
<p>Ich schreibe gerade ein Programm, wo ich ein bisschen mit den Sortieralgorithmen herumexperimentieren will.</p>
<p>Darum will ich mir auf dem Bildschirm 500 Pixel zeichnen lassen mit dem Index als x-Koordinate und einer Zufallszahl als y-Koordinate und will diese Dann sortieren und jeden Schritt neu zeichnen lassen.</p>
<p>Ich bin jetzt soweit, dass ich die Zufallszahlen erzeugt habe und das Anfangsbild zeichnen kann, aber sobald das geschehen ist kommt eine Fehlermeldung:</p>
<p>Die Anweisung in &quot;0x004012fc&quot; verweist auf Speicher in &quot;0x00000004&quot;. Der Vorgang &quot;read&quot; konnte nicht auf dem SPeicher durchgeführt werden.<br />
Klicken Sie auf &quot;OK&quot; um das Programm zu beenden.<br />
Klicken sie auf &quot;Abbrechen&quot;, um das Programm zu debuggen.</p>
<p>Wenn ich auf abbrechen klicke, kommt die Fehlermeldung nochmal, nur ohne Abbrechen-Button.</p>
<p>Ich schicke einfach mal meinen Code und ich bin mir ziemlich sicher, dass das an der Speicherverwaltung liegt, da bin ich mir immer etwas unsicher wenn ich damit arbeite:</p>
<pre><code class="language-cpp"># include &lt;windows.h&gt;

# define MAX_PIXELS 500

TCHAR szAppName[]=TEXT(&quot;mSort&quot;);

LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);
int *CreateRandomPixels(int);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
				   PSTR lpCmdLine, int iCmdShow)
{
	HWND hwnd;
	WNDCLASS wndclass;
	MSG msg;

	wndclass.style = CS_VREDRAW | CS_HREDRAW;
	wndclass.lpfnWndProc = WindowProc;
	wndclass.cbClsExtra = 0;
	wndclass.cbWndExtra = 0;
	wndclass.hInstance = hInstance;
	wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
	wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
	wndclass.lpszMenuName = NULL;
	wndclass.lpszClassName = szAppName;

	if(!RegisterClass(&amp;wndclass))
	{
		MessageBox(NULL, 
			TEXT(&quot;Fehler: Programm arbeitet mit Unicode und setzt Windows NT vorraus&quot;),
			szAppName, MB_ICONERROR);
		return 0;
	};

	hwnd = CreateWindow (szAppName,
						 szAppName,
						 WS_OVERLAPPEDWINDOW,
						 100,
						 100,
						 700,
						 640,
						 NULL,
						 NULL,
						 hInstance,
						 NULL);

	ShowWindow(hwnd, iCmdShow);
	UpdateWindow(hwnd);

	while(GetMessage(&amp;msg, NULL, 0, 0))
	{
		TranslateMessage(&amp;msg);
		DispatchMessage(&amp;msg);
	};

	return msg.wParam;
};

LRESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	HWND hButtonGo;
	HDC hdc;
	int i;
	static int *pixels;

	switch(message)
	{
	case WM_PAINT:
		pixels = CreateRandomPixels(MAX_PIXELS);
		hdc = GetDC(hwnd);
		for(i=0;i&lt;MAX_PIXELS;i++)
		{
			SetPixel(hdc, i+20, pixels[i]+20, 0);
		}
		ReleaseDC(hwnd, hdc);
	case WM_CREATE:
		hButtonGo = CreateWindow (TEXT(&quot;button&quot;),
								  TEXT(&quot;Sort&quot;),
								  WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
								  640, 20, 80, 20,
								  hwnd,
								  (HMENU) 1,
								  ((LPCREATESTRUCT)lParam)-&gt;hInstance,
								  NULL);
		return 0;

	case WM_DESTROY:
		PostQuitMessage(0);
		delete[] pixels;
		return 0;
	};
	return DefWindowProc(hwnd, message, wParam, lParam);
};

int *CreateRandomPixels(int max)
{
	int i;
	int *randpixels = new int[max];

	for(i=0;i&lt;max;i++)
	{
		randpixels[i] = rand()%MAX_PIXELS;
	};
	return randpixels;
};
</code></pre>
<p>Ach ja: Die Pixel werden wunderbar gezeichnet, erst dann kommt die Fehlermeldung.</p>
<p>Bitte helft mir.</p>
<p>Danke schonmal<br />
Gruß<br />
Max</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1289349</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1289349</guid><dc:creator><![CDATA[Max3000]]></dc:creator><pubDate>Mon, 21 May 2007 18:38:53 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamische Speicherverwaltung on Mon, 21 May 2007 18:52:12 GMT]]></title><description><![CDATA[<p>Sorry ich hab was vergessen.</p>
<p>In dem Fall WM_PAINT musste doch noch return 0 hin.</p>
<p>Jetzt ist der Fehler nicht mehr da, aber dafür wird diese WM_PAINT -Nachricht immer wieder aufgerufen, das soll doch aber nur einmal sein.</p>
<p>Ich dachte eigentlich immer WM_PAINT kommt nur beim Erstellen des Fensters (mit UpdateWindow) und wenn man z.B. größe des Fensters ändert oder das Fenster verschiebt.</p>
<p>Hat da jemand eine Lösungsmöglichkeit?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1289367</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1289367</guid><dc:creator><![CDATA[Max3000]]></dc:creator><pubDate>Mon, 21 May 2007 18:52:12 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamische Speicherverwaltung on Mon, 21 May 2007 19:00:49 GMT]]></title><description><![CDATA[<p>BeginPaint und EndPaint?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1289372</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1289372</guid><dc:creator><![CDATA[MFK]]></dc:creator><pubDate>Mon, 21 May 2007 19:00:49 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamische Speicherverwaltung on Mon, 21 May 2007 19:04:06 GMT]]></title><description><![CDATA[<p>Jo.<br />
Damit gehts.</p>
<p>Vielen Dank.</p>
<p>Wer das hier soweit schon gelesen hat, kann mir vielleicht trotzdem sagen, warum das mit GetDC und ReleaseDC nicht geklappt hat und was da genau der Unterschied ist.</p>
<p>Gruß<br />
Max</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1289373</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1289373</guid><dc:creator><![CDATA[Max3000]]></dc:creator><pubDate>Mon, 21 May 2007 19:04:06 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamische Speicherverwaltung on Mon, 21 May 2007 19:04:25 GMT]]></title><description><![CDATA[<p>WM_PAINT wird solange immer wieder gesendet bis der neu zu zeichnende Fensterbereich wieder für gültig erklärt wurde.<br />
Das passiert entweder bei Verwendung von BeginPaint() und EndPaint() oder durch Aufruf von ValidateRect()<br />
Nimm BeginPaint() und EndPaint() !</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1289376</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1289376</guid><dc:creator><![CDATA[geeky]]></dc:creator><pubDate>Mon, 21 May 2007 19:04:25 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamische Speicherverwaltung on Mon, 21 May 2007 20:00:51 GMT]]></title><description><![CDATA[<p>Ich hab zu dem selben Programm gleich nochmal ne Frage.<br />
Nach jedem Sortierschritt soll jetzt dieses Feld pixels neu gezeichnet werden. Wenn ich auf meinen Button klicke geht aber leider nix los.</p>
<p>Wie kann ich WM_Paint nochmal aufrufen?<br />
SendMessage und UpdateWindow funktionieren irgendwie nicht:</p>
<pre><code class="language-cpp">LRESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	HWND hButtonGo;
	HDC hdc;
	PAINTSTRUCT ps;
	int i, j, tmp, smallest;
	static int *pixels;

	switch(message)
	{

	case WM_CREATE:
		pixels = CreateRandomPixels(MAX_PIXELS);
		hButtonGo = CreateWindow (TEXT(&quot;button&quot;),
								  TEXT(&quot;Sort&quot;),
								  WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
								  540, 20, 120, 30,
								  hwnd,
								  (HMENU) 1,
								  ((LPCREATESTRUCT)lParam)-&gt;hInstance,
								  NULL);
		return 0;

	case WM_PAINT:
		hdc = BeginPaint(hwnd, &amp;ps);
		for(i=0;i&lt;MAX_PIXELS;i++)
		{
			SetPixel(hdc, i+20, pixels[i]+20, 0);
		}
		EndPaint(hwnd, &amp;ps);
		return 0;
	case WM_DESTROY:
		PostQuitMessage(0);
		delete[] pixels;
		return 0;
	case WM_COMMAND:
		if(HIWORD(wParam)==BN_CLICKED)
		{
			if(LOWORD(wParam)==1)
			{

				for(i=0;i&lt;(MAX_PIXELS-1); i++)
				{
					smallest=i;
					for(j=i+1;j&lt;MAX_PIXELS; j++)
					{
						if(pixels[j]&gt;pixels[smallest]) smallest=j;
					};
					tmp=pixels[i];
					pixels[i]=pixels[smallest];
					pixels[smallest]=tmp;
					SendMessage(NULL, WM_PAINT, wParam, lParam);
					//UpdateWindow(hwnd);
				};
			};
		};
	};
	return DefWindowProc(hwnd, message, wParam, lParam);
};
</code></pre>
<p>Jemand eine Idee?</p>
<p>Gruß<br />
Max</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1289415</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1289415</guid><dc:creator><![CDATA[Max3000]]></dc:creator><pubDate>Mon, 21 May 2007 20:00:51 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamische Speicherverwaltung on Mon, 21 May 2007 20:10:46 GMT]]></title><description><![CDATA[<p>InvalidateRect() mit UpdateWindow() direkt dahinter <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=";D"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1289423</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1289423</guid><dc:creator><![CDATA[geeky]]></dc:creator><pubDate>Mon, 21 May 2007 20:10:46 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamische Speicherverwaltung on Mon, 21 May 2007 20:16:25 GMT]]></title><description><![CDATA[<p>Wunderbar.<br />
Jetzt klappt alles.<br />
Ich denke jetzt kann ich euch erstmal für eine gewisse Zeit in Ruhe lassen.</p>
<p>Vielen Dank<br />
MFG<br />
Max</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1289427</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1289427</guid><dc:creator><![CDATA[Max3000]]></dc:creator><pubDate>Mon, 21 May 2007 20:16:25 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamische Speicherverwaltung on Mon, 21 May 2007 20:51:35 GMT]]></title><description><![CDATA[<p>Wenn Du &quot;CreateRandomPixels&quot; aber regelmäßig unter WM_PAINT aufrufst (ein paar Posts davor ist das so), erzeugst Du massig Speicherleaks!<br />
Dann schreib das:</p>
<pre><code class="language-cpp">case WM_DESTROY:
        PostQuitMessage(0);
        delete[] pixels;
        return 0;
</code></pre>
<p>besser so:</p>
<pre><code class="language-cpp">case WM_DESTROY:
        delete[] pixels;
        PostQuitMessage(0);
        return 0;
</code></pre>
<p>(Erst Clean-Up, dann beenden <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>PS-1: Dein &quot;hButtonGo&quot; lebt nicht sehr lange <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="🤡"
    /> .<br />
PS-2: Was issen dat <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>
<pre><code class="language-cpp">SendMessage(NULL, WM_PAINT, wParam, lParam);
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1289443</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1289443</guid><dc:creator><![CDATA[CodeFinder]]></dc:creator><pubDate>Mon, 21 May 2007 20:51:35 GMT</pubDate></item><item><title><![CDATA[Reply to Dynamische Speicherverwaltung on Mon, 21 May 2007 22:40:51 GMT]]></title><description><![CDATA[<p>Genaugenommen ist es egal ob PostQuitMessage nun vor dem delete steht oder dahinter, die Message lander ja erstmal in der Queue, und wird erst später verwurstet.<br />
Aber es liest sich natürlich &quot;sauberer&quot; wenn erst delete und dann PostQuitMessage dasteht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1289491</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1289491</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Mon, 21 May 2007 22:40:51 GMT</pubDate></item></channel></rss>