Problems doing realloc, getting heap runtime error? 
Author Message
 Problems doing realloc, getting heap runtime error?

Hi,

Below is where I'm having problems doing realloc, look at the else
condition. The malloc works great.

What am I doing wrong and how to correct it?

Thanks,

char pData[4096];
char*pBuf;

   if (iPacket==NO){

    iPacket=YES;

    pBuf = (char*)malloc(nNewBytes+1);
    if (pBuf){

     memset(pBuf,0,nNewBytes+1);
     memcpy(pBuf,pData,nNewBytes);
     pBuf[nNewBytes]='\0';

    }else{

     CallBack_Network_Error("error malloc recx buffer memory");
     break;

    }

   }else{

    pBuf = (char *) realloc(pBuf, nNewBytes+1);
    if ( pBuf ){

     pBuf=pData;
     pBuf[nNewBytes]='\0';

    }else{

     CallBack_Network_Error("error reallocating recx buffer memory");
     break;
    }
   }



Thu, 10 Mar 2005 18:06:46 GMT  
 Problems doing realloc, getting heap runtime error?

Quote:

> Hi,

> Below is where I'm having problems doing realloc, look at the else
> condition. The malloc works great.

> What am I doing wrong and how to correct it?

You are probably corrupting memory somewhere or ...

Quote:
> char pData[4096];
> char*pBuf;

If you're intending "p" to mean pointer for your variables,
then you've already failed, as pData is an array of char.

Quote:
>    if (iPacket==NO){

>     iPacket=YES;

>     pBuf = (char*)malloc(nNewBytes+1);

"dangerous" casting of malloc -- did you include stdlib.h?

Quote:
>     if (pBuf){

>      memset(pBuf,0,nNewBytes+1);
>      memcpy(pBuf,pData,nNewBytes);
>      pBuf[nNewBytes]='\0';

At some point, you've got to realize a few of those steps above
are just a plain waste of time.  If the buffers are holding
strings, just use strcpy or strncpy and be done with it.  But,
from what you've posted, pData could hold any old garbage...

Quote:
>     }else{

>      CallBack_Network_Error("error malloc recx buffer memory");
>      break;

What purpose does the "break" statement serve here?  Is this
snippet copied from a loop?

Quote:
>     }

>    }else{

>     pBuf = (char *) realloc(pBuf, nNewBytes+1);

Here you leak memory in addition to the "dangerous" cast.  You need
to handle the NULL return of realloc so a valid pBuf pointer isn't
lost:

  {
    void *tmp = realloc (pBuf, nNewBytes+1);
    if (tmp != NULL) { p = tmp; }
    else { Abort_Transaction_Due_To_Memory_Failure(); }
  }

Quote:
>     if ( pBuf ){

>      pBuf=pData;

Did you forget what you were doing halfway through the function?
You just clobbered your newly reallocated memory with a pointer
to a local array.  Guaranteed to be a problem, especially in any
call to realloc() or free(), or passing the variable as a return
value from the function...
Quote:
>      pBuf[nNewBytes]='\0';

>     }else{

>      CallBack_Network_Error("error reallocating recx buffer memory");
>      break;
>     }
>    }



Thu, 10 Mar 2005 18:52:56 GMT  
 Problems doing realloc, getting heap runtime error?

The code below is extracted from a loop.

pData is already initialized and holding data prior to the loop.

I copy pData into pBuf after I malloc pBuf.

Then I have pData re-initialized with more data the pData just recieved
again.

Then I need to realloc pBuf to copie the data again from pData into pBuf.

That is what I need to do but have having an error on realloc and appending
pData to pBuf.

Problems  in getting pData into pBuf after reallocating pBuf.



Quote:

> > Hi,

> > Below is where I'm having problems doing realloc, look at the else
> > condition. The malloc works great.

> > What am I doing wrong and how to correct it?

> You are probably corrupting memory somewhere or ...

> > char pData[4096];
> > char*pBuf;

> If you're intending "p" to mean pointer for your variables,
> then you've already failed, as pData is an array of char.

> >    if (iPacket==NO){

> >     iPacket=YES;

> >     pBuf = (char*)malloc(nNewBytes+1);

> "dangerous" casting of malloc -- did you include stdlib.h?

> >     if (pBuf){

> >      memset(pBuf,0,nNewBytes+1);
> >      memcpy(pBuf,pData,nNewBytes);
> >      pBuf[nNewBytes]='\0';

> At some point, you've got to realize a few of those steps above
> are just a plain waste of time.  If the buffers are holding
> strings, just use strcpy or strncpy and be done with it.  But,
> from what you've posted, pData could hold any old garbage...

> >     }else{

> >      CallBack_Network_Error("error malloc recx buffer memory");
> >      break;

> What purpose does the "break" statement serve here?  Is this
> snippet copied from a loop?

> >     }

> >    }else{

> >     pBuf = (char *) realloc(pBuf, nNewBytes+1);

> Here you leak memory in addition to the "dangerous" cast.  You need
> to handle the NULL return of realloc so a valid pBuf pointer isn't
> lost:

>   {
>     void *tmp = realloc (pBuf, nNewBytes+1);
>     if (tmp != NULL) { p = tmp; }
>     else { Abort_Transaction_Due_To_Memory_Failure(); }
>   }

> >     if ( pBuf ){

> >      pBuf=pData;

> Did you forget what you were doing halfway through the function?
> You just clobbered your newly reallocated memory with a pointer
> to a local array.  Guaranteed to be a problem, especially in any
> call to realloc() or free(), or passing the variable as a return
> value from the function...

> >      pBuf[nNewBytes]='\0';

> >     }else{

> >      CallBack_Network_Error("error reallocating recx buffer memory");
> >      break;
> >     }
> >    }



Thu, 10 Mar 2005 19:13:32 GMT  
 Problems doing realloc, getting heap runtime error?
Quote:

> Hi,

> Below is where I'm having problems doing realloc, look at the else
> condition. The malloc works great.

> What am I doing wrong and how to correct it?

> Thanks,

> char pData[4096];
> char*pBuf;

char *pBuf;

Quote:

>    if (iPacket==NO){

>     iPacket=YES;

>     pBuf = (char*)malloc(nNewBytes+1);

Include stdlib.h if you haven't and get rid of the cast.

Quote:
>     if (pBuf){

>      memset(pBuf,0,nNewBytes+1);
>      memcpy(pBuf,pData,nNewBytes);
>      pBuf[nNewBytes]='\0';

The memset is immediately followed by a memcpy that writes over all
except the last byte initialized with memset.  This last byte is also
then assigned some value.  Why bother doing the memset then?

Quote:

>     }else{

>      CallBack_Network_Error("error malloc recx buffer memory");
>      break;

I presume the code is extracted out of a loop.  Otherwise, it shouldn't
compile.  Additionally, the nature of your problem seems to be related
to how your loop is set-up.  Please give more context to obtain better
answers.

Quote:

>     }

>    }else{

>     pBuf = (char *) realloc(pBuf, nNewBytes+1);

You should use a temporary to hold the return of realloc to avoid memory
leaks.

If what you want is a buffer that grows inside this loop, you are doing
it the wrong way.  Your call to realloc changes the size of the memory
block pointed to by pBuf to nNewBytes+1.

Assume your loop tries to copy two "packets" into pBuf.  Further assume
that in the first pass, iPacket == NO, so you use malloc to get space
for one "packet" which will be nNewBytes+1 big.  In the second iteration
of this loop, you will get into the realloc part.  However, your call to
realloc would merely resize pBuf to nNewBytes+1, instead of  making pBuf
as big as its *current* size + nNewBytes+1.

If your goal was to grow pBuf to accommodate the second packet, you
should keep track of pBuf's current size in a temporary variable (say
cursz) and pass the new size to realloc as this cursz + <how much more
you want>.  Something like:
       tempPtr = realloc(pBuf, cursz+nNewBytes+1);
       if ( !tempPtr ) {
           /* error handling stuff */
       }
       else pBuf = tempPtr;

Quote:
>     if ( pBuf ){

>      pBuf=pData;

Huh??  You just obtained some memory through realloc, that you have
simply thrown away without freeing.  Assuming that you want to copy new
data over into the space you obtained from realloc:
        memcpy(pBuf+cursz, pData, nNewBytes);
        cursz += nNewBytes;
is what you should use.

Quote:
>      pBuf[nNewBytes]='\0';

Again, with the same assumptions as above, this should be:
        pBuf[cursz] = 0;

Quote:

>     }else{

>      CallBack_Network_Error("error reallocating recx buffer memory");
>      break;
>     }
>    }

-nrk.


Thu, 10 Mar 2005 21:10:43 GMT  
 Problems doing realloc, getting heap runtime error?

Quote:

> The code below is extracted from a loop.

> pData is already initialized and holding data prior to the loop.

> I copy pData into pBuf after I malloc pBuf.

> Then I have pData re-initialized with more data the pData just recieved
> again.

> Then I need to realloc pBuf to copie the data again from pData into pBuf.

> That is what I need to do but have having an error on realloc and appending
> pData to pBuf.

> Problems  in getting pData into pBuf after reallocating pBuf.

You need to keep track of how many significant bytes were read into
pData and how many bytes have been written into pBuf so far.

Perhaps something like:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *
copy_from_stream (FILE *ifp)
{
    size_t nRead    = 0;
    size_t nWritten = 0;
    int    retry    = 0;
    int    errors   = 0;
    char   buffer[4096] = "";
    char   *cptr = NULL;

    if (ifp == NULL)
        return NULL;

    for (;;) {
        nRead = fread (buffer, 1, sizeof buffer, ifp);
        if (nRead > 0) {
            if (cptr == NULL) {
                cptr = malloc (nRead + 1);
                if (cptr == NULL) {
                    fprintf (stderr, "%s:%d: malloc failed!\n",\
                    __FILE__, __LINE__);
                    errors = 1;
                    break;
                }
            }
            else {
                void *tmp = realloc (cptr, nRead + nWritten + 1);
                if (tmp == NULL) {
                    fprintf (stderr, "%s:%d: realloc failed\n",\
                    __FILE__, __LINE__);
                    errors = 1;
                    break;
                }
                cptr = tmp;
            }
            memcpy (cptr + nWritten, buffer, nRead);
            nWritten += nRead;
        }
        if (feof(ifp)) {
            break;
        }
        else if (ferror(ifp)) {
            if (retry < 3) {
                clearerr (ifp);
                retry++;
            }
            else {
                errors = 1;
                break;
            }
        }
    }

    if (errors && cptr != NULL) {
        free (cptr);
        cptr = NULL;
    }
    else if (cptr != NULL) {
        cptr[nWritten] = '\0';
    }
    return cptr;

- Show quoted text -

Quote:
}



Fri, 11 Mar 2005 04:41:39 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. rebuilding c-runtime / error in realloc

2. DAO3.60 - getting error 3260 when doing CDaoRecordset::Update

3. adAsyncExecute - runtime error when getting state property

4. HEAP error: Free heap block xxx mdofied at xxx

5. Heap errors when stressing Automation, _bstr_t, and watching heap blocks

6. assertion errors- heap problem?

7. Heap Error, COM Interface problem?

8. HEAP[dllhost.exe]: HEAP: Free Heap block 1e32c28 modified at 1e32dc4 after it was freed

9. Microsoft Visual C++ Runtime Library Runtime Error When Browsing in IE6

10. Microsoft Visual C++ Runtime Library Runtime Error When Browsing in IE6

11. Problem with message box (causes runtime error)

12. Problem with message box (runtime error after I click OK)

 

 
Powered by phpBB® Forum Software