realloc problem

realloc problem

Post by CBFalcone » Sat, 08 Dec 2001 18:50:01




>  I am having a problem with realloc() in the code below. I am running this on
> NNT4. I do not think I have messed up but a second opinion would be good.

>  This code runs error free with VC5 so it may be a bug in realloc().

>   do
>   {
>     memset ( read_buffer, 0, sizeof ( read_buffer ));
>     read_bytes = recv ( sd, read_buffer, BUFFER_SIZE, 0 );
>     if ( read_bytes == SOCKET_ERROR || read_bytes == 0 )
>     {
>       if ( work_buffer != NULL )
>         free ( work_buffer );
>       return 1;
>     }
>     work_buffer_size += read_bytes;
>     if ( work_buffer == NULL )
>     {
>       work_buffer = malloc ( work_buffer_size );
>       if ( work_buffer == NULL )
>         return 1;
>       else
>         work_buffer[0] = 0;
>     }
>     else
>     {
>       if ( realloc ( work_buffer, work_buffer_size ) == NULL )
>       {
>         free ( work_buffer );
>         work_buffer = NULL;
>         return 1;
>       }
>     }
>     strcat ( work_buffer, read_buffer );
>   }
>   while ( strcmp ( read_buffer + read_bytes - 3, "\r\n" ));

Yes, you messed up.  realloc returns a pointer to the revised
buffer.  If that is non-NULL it succeeded, and you can copy that
result into work_buffer.  If it is NULL, realloc failed and
work_buffer is unchanged.  So you need something like:

    void *tmp;
    ....

    if (NULL == (tmp = realloc(wkbuff, whatever))) {
       /* take corrective action, wkbuff is unaltered */
    }
    else {
       /* wkbuff is now valueless, do NOT free/dereference it */
       wkbuff = tmp;
       /* use wkbuff, it has grown/shrunk etc. */
    }

--

   Available for consulting/temporary embedded and systems.
   (Remove "XXXX" from reply address. yahoo works unmodified)

 
 
 

1. A problem with realloc

        I'm having a problem with realloc, or one of the functions
that it calls. I'm using qnx 4.22 and my process is using a flat
memory model. A number of dynamic arrays are used and realloc works
fine up to a point.  I end up getting a Segmentation Violation error.
For parameters I am passing it an adress that it has used before in
the same session.

        Using the watcom debugger wvideo and the show calls command,
the function calls go like this:
MyFunction
        |
        V
_nrealloc
        |
        V
_nexpand
        |
        V
_ExpandDGROUP   -> access Violation

If anyone knows what is happening to cause this error please let me
know. Thanks,

                Jason Howard

2. using atm for limiting ethernets bandwidth consumpition

3. about realloc memo in TornadoII

4. Windows API

5. Memory allocation and realloc

6. S/MIME message encryption support in Outlook

7. realloc freed memory

8. Modem disconnects upon connection.

9. Memory fragmentation with realloc()

10. realloc down

11. realloc bug in MSL?

12. Epox (Aralion) ATA100 RAID card problems, problems, problems

13. mach-o problems, problems and guess what ? problems !