malloc Fails with ERROR_INVALID_HANDLE - Programmer

This is a discussion on malloc Fails with ERROR_INVALID_HANDLE - Programmer ; I am using MS Visual C++ on Windows XP 5.1 The following sometimes returns a NULL pointer (from malloc() to ppStereoDocuments). void **ppStereoDocuments; if (!(ppStereoDocuments=(void **)malloc(2*sizeof(void *)))) { /* Error handling */ DWORD dwError=GetLastError(); return; } dwError receives the value ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: malloc Fails with ERROR_INVALID_HANDLE

  1. malloc Fails with ERROR_INVALID_HANDLE

    I am using MS Visual C++ on Windows XP 5.1

    The following sometimes returns a NULL pointer (from malloc() to
    ppStereoDocuments).

    void **ppStereoDocuments;
    if (!(ppStereoDocuments=(void **)malloc(2*sizeof(void *))))
    {
    /* Error handling */
    DWORD dwError=GetLastError();
    return;
    }

    dwError receives the value 6 (ERROR_INVALID_HANDLE)

    Sometimes the call allocates memory fine.

    I would be most grateful if someone can suggest why I sometimes get
    this error.

    Thanks,
    Peter.


  2. Re: malloc Fails with ERROR_INVALID_HANDLE

    MajorSetback@excite.com wrote:
    > I am using MS Visual C++ on Windows XP 5.1
    >
    > The following sometimes returns a NULL pointer (from malloc() to
    > ppStereoDocuments).


    Is your application using a lot of memory (or another one that is
    running)? 'malloc' will return 0 if there is insufficient memory
    available.

    > void **ppStereoDocuments;
    > if (!(ppStereoDocuments=(void **)malloc(2*sizeof(void *))))
    > {
    > /* Error handling */
    > DWORD dwError=GetLastError();


    You are confusing libraries here. 'malloc' is part of the C runtime
    library. GetLastError is part of the Windows API and lets you determine
    the last error that resulted from a call to a Windows API function. The
    C runtime and the Windows API are two distinct things.

    > return;
    > }
    >
    > dwError receives the value 6 (ERROR_INVALID_HANDLE)


    Check your previous Windows API calls for errors. This error
    information has nothing to do with 'malloc'.

    > Sometimes the call allocates memory fine.
    >
    > I would be most grateful if someone can suggest why I sometimes get
    > this error.


    See above, it's the limits of your memory that make 'malloc' return
    0 now and then.

    hth
    --
    jb

    (reply address in rot13, unscramble first)



  3. Re: malloc Fails with ERROR_INVALID_HANDLE

    Hi Jakob,

    You are absolutely right. Thanks very much for your help.

    Peter.


+ Reply to Thread