<?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[Problem mit malloc]]></title><description><![CDATA[<p>Ich habe ein Problem, ich bin dabei C++ zu lernen und habe mir deshalb einige Tutorials besorgt. Das Problem: im Code ist scheinbar ein Fehler, das macht sich natürlich nicht gut, wen man davon lernen will...<br />
Dummerweise ist das darin gezeigte sehr interessant, ich würde daher gerne wissen, wie es richtig heißt. Es geht eigentlich um die WinAPI, aber da da nicht das problem liegt, glaube ich hier schon richtig zu sein <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 />
Das Problem sind scheinbar die Dateifunktioen. Nicht nur das Visual Studio 2005 die Funktion fopen als veraltet kritisiert (dabei hat sie mir doch so gefallen, weil ich sie schon von php kannte :(), nein, es meckert auch bei buffer = malloc(iFileSize);, wobei char *buffer=NULL; Sollte das nicht eigentlich den pointer des reservierten Speichers auf buffer legen, sosass man dan einen String von der Länge iFileSize hat? Und warum ist das nicht einfach mit char buffer[iFileSize] möglich? Was ist die &quot;neue&quot; Version von fopen? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /><br />
Achja, hier der code:</p>
<pre><code class="language-cpp">#define _CRT_SECURE_NO_DEPRECATE //VC++ 2005 mag scheinbar kein fopen :(

#include &lt;windows.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{
    HWND     hWnd;
    MSG      msg;
    WNDCLASS wc;

    const char szAppName[] = &quot;Editcontrol Tutorial&quot;;

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

    RegisterClass(&amp;wc);

    hWnd = CreateWindow( szAppName,
                         &quot;Editcontrol Tutorial&quot;,
                         WS_OVERLAPPEDWINDOW,
                         CW_USEDEFAULT,
                         CW_USEDEFAULT,
                         CW_USEDEFAULT,
                         CW_USEDEFAULT,
                         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 WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    static HWND hEdit;

    switch(message)
    {
    case WM_CREATE:
        {            
			FILE *fz;
            char *buffer = NULL;
            int iFileSize;

            fz = fopen(&quot;text.txt&quot;, &quot;rb&quot;);
            if(fz != NULL)
            {
                fseek(fz, 0, SEEK_END);
                iFileSize = ftell(fz);
                buffer = malloc(iFileSize);//'void *' kann nicht in 'char *' konvertiert werden

                fseek(fz, 0, SEEK_SET);
                fread(buffer, 1, iFileSize, fz);
                fclose(fz);
            }
			            hEdit = CreateWindowEx(WS_EX_CLIENTEDGE,
                                   &quot;edit&quot;,
                                   buffer, 
                                   WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_MULTILINE |
                                              ES_AUTOVSCROLL,
                                   0, 0, 0, 0,
                                   hWnd,
                                   NULL,
                                   ((LPCREATESTRUCT) lParam) -&gt; hInstance,
                                   NULL);

            free(buffer);

            return 0;
        }
    case WM_SIZE:
        {
			MoveWindow(hEdit, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
            return 0;
        }
    case WM_CLOSE:
        {
			FILE *fz;

			char *buffer = NULL;
            int iLength;

            iLength = GetWindowTextLength(hEdit);

            buffer = malloc(iLength);//'void *' kann nicht in 'char *' konvertiert werden

            GetWindowText(hEdit, buffer, iLength+1);

            fz = fopen(&quot;text.txt&quot;, &quot;wb&quot;);
            fwrite(buffer, 1, iLength, fz);
            fclose(fz);

            free(buffer);

            DestroyWindow(hWnd);
            return 0;
        }
    case WM_DESTROY:
        {
            PostQuitMessage(0);
            return 0;
        }
    }
    return DefWindowProc(hWnd, message, wParam, lParam);
}
</code></pre>
<p>Danke schonmal für die Hilfe.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/141925/problem-mit-malloc</link><generator>RSS for Node</generator><lastBuildDate>Mon, 31 Aug 2026 15:12:17 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/141925.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 26 Mar 2006 14:21:02 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Problem mit malloc on Sun, 26 Mar 2006 14:21:02 GMT]]></title><description><![CDATA[<p>Ich habe ein Problem, ich bin dabei C++ zu lernen und habe mir deshalb einige Tutorials besorgt. Das Problem: im Code ist scheinbar ein Fehler, das macht sich natürlich nicht gut, wen man davon lernen will...<br />
Dummerweise ist das darin gezeigte sehr interessant, ich würde daher gerne wissen, wie es richtig heißt. Es geht eigentlich um die WinAPI, aber da da nicht das problem liegt, glaube ich hier schon richtig zu sein <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 />
Das Problem sind scheinbar die Dateifunktioen. Nicht nur das Visual Studio 2005 die Funktion fopen als veraltet kritisiert (dabei hat sie mir doch so gefallen, weil ich sie schon von php kannte :(), nein, es meckert auch bei buffer = malloc(iFileSize);, wobei char *buffer=NULL; Sollte das nicht eigentlich den pointer des reservierten Speichers auf buffer legen, sosass man dan einen String von der Länge iFileSize hat? Und warum ist das nicht einfach mit char buffer[iFileSize] möglich? Was ist die &quot;neue&quot; Version von fopen? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /><br />
Achja, hier der code:</p>
<pre><code class="language-cpp">#define _CRT_SECURE_NO_DEPRECATE //VC++ 2005 mag scheinbar kein fopen :(

#include &lt;windows.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{
    HWND     hWnd;
    MSG      msg;
    WNDCLASS wc;

    const char szAppName[] = &quot;Editcontrol Tutorial&quot;;

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

    RegisterClass(&amp;wc);

    hWnd = CreateWindow( szAppName,
                         &quot;Editcontrol Tutorial&quot;,
                         WS_OVERLAPPEDWINDOW,
                         CW_USEDEFAULT,
                         CW_USEDEFAULT,
                         CW_USEDEFAULT,
                         CW_USEDEFAULT,
                         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 WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    static HWND hEdit;

    switch(message)
    {
    case WM_CREATE:
        {            
			FILE *fz;
            char *buffer = NULL;
            int iFileSize;

            fz = fopen(&quot;text.txt&quot;, &quot;rb&quot;);
            if(fz != NULL)
            {
                fseek(fz, 0, SEEK_END);
                iFileSize = ftell(fz);
                buffer = malloc(iFileSize);//'void *' kann nicht in 'char *' konvertiert werden

                fseek(fz, 0, SEEK_SET);
                fread(buffer, 1, iFileSize, fz);
                fclose(fz);
            }
			            hEdit = CreateWindowEx(WS_EX_CLIENTEDGE,
                                   &quot;edit&quot;,
                                   buffer, 
                                   WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_MULTILINE |
                                              ES_AUTOVSCROLL,
                                   0, 0, 0, 0,
                                   hWnd,
                                   NULL,
                                   ((LPCREATESTRUCT) lParam) -&gt; hInstance,
                                   NULL);

            free(buffer);

            return 0;
        }
    case WM_SIZE:
        {
			MoveWindow(hEdit, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
            return 0;
        }
    case WM_CLOSE:
        {
			FILE *fz;

			char *buffer = NULL;
            int iLength;

            iLength = GetWindowTextLength(hEdit);

            buffer = malloc(iLength);//'void *' kann nicht in 'char *' konvertiert werden

            GetWindowText(hEdit, buffer, iLength+1);

            fz = fopen(&quot;text.txt&quot;, &quot;wb&quot;);
            fwrite(buffer, 1, iLength, fz);
            fclose(fz);

            free(buffer);

            DestroyWindow(hWnd);
            return 0;
        }
    case WM_DESTROY:
        {
            PostQuitMessage(0);
            return 0;
        }
    }
    return DefWindowProc(hWnd, message, wParam, lParam);
}
</code></pre>
<p>Danke schonmal für die Hilfe.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1024193</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1024193</guid><dc:creator><![CDATA[benpicco]]></dc:creator><pubDate>Sun, 26 Mar 2006 14:21:02 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit malloc on Sun, 26 Mar 2006 14:42:09 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">buffer = malloc(iFileSize);//'void *' kann nicht in 'char *' konvertiert werden
</code></pre>
<p>sollte so aussehen</p>
<pre><code class="language-cpp">buffer = (char*)malloc(iFileSize);//'void *' kann nicht in 'char *' konvertiert werden
</code></pre>
<p>da die funktion <strong>malloc</strong> ein <strong>void</strong>* zurückgibt must du es nach <strong>char</strong>* casten..</p>
<p>außerdem ist das was du hier codest <strong>C</strong> und kein <strong>C++</strong> - schau dir mal String Streams an -&gt; <a href="http://www.cppreference.com/cppio/index.html" rel="nofollow">http://www.cppreference.com/cppio/index.html</a></p>
<p>mfg blan</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1024213</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1024213</guid><dc:creator><![CDATA[blan]]></dc:creator><pubDate>Sun, 26 Mar 2006 14:42:09 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit malloc on Sun, 26 Mar 2006 14:47:41 GMT]]></title><description><![CDATA[<p>Danke, es funktioniert :).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1024222</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1024222</guid><dc:creator><![CDATA[benpicco]]></dc:creator><pubDate>Sun, 26 Mar 2006 14:47:41 GMT</pubDate></item></channel></rss>