Re: Crash by allocationg small blocks
From: Severian (severian_at_chlamydia-is-not-a-flower.com)
Date: 01/15/05
- Next message: Carl Daniel [VC++ MVP]: "Re: STL error"
- Previous message: Mark Randall: "sprintf field param errors"
- In reply to: Sebastien LEGUET: "Crash by allocationg small blocks"
- Next in thread: Doug Harrison [MVP]: "Re: Crash by allocationg small blocks"
- Reply: Doug Harrison [MVP]: "Re: Crash by allocationg small blocks"
- Messages sorted by: [ date ] [ thread ]
Date: Sat, 15 Jan 2005 13:38:58 GMT
On Thu, 13 Jan 2005 02:05:08 -0800, "Sebastien LEGUET" <Sebastien
LEGUET@discussions.microsoft.com> wrote:
>Hello,
>
>I try to exeute this code and I have a crash during the execution. The
>realloc function return NULL.
>#include "stdio.h"
>#include "stdlib.h"
>
>int main()
>{
> char** pData = NULL;
> char *pszFicDescName = NULL;
>
> for( int i=0; i< 7000000; i++ )
> {
> pszFicDescName = (char*)malloc(1041);
> strcpy(pszFicDescName,"Ceci est un test");
> if (pData == NULL)
> pData = (char**)malloc( sizeof(char*) );
/* You know, you might not crash if you CHECK FOR
** failure HERE: */
> pData = (char**)realloc( (void*)pData, (i+1)*sizeof(char*) );
/* Or here!! */
>//access violation in small-block allocator
> pData[i] = pszFicDescName;
> printf("Cpt = %d\n",i);
> }
>
> return(0);
>} //end main
Check for failure when mallocing/reallocing your data, and don't
expect the system to handle every naive memory request you might ever
make.
How much memory do you think you have?
The CRT allocater is probably not designed for what you want; however,
designing a general allocator for your case may not be simple.
>Could you tell me if you know about that problem and how can I resolved that
>because I need to use standard C code.
>I tried this code on VC6 and VC7 and I have the same result. I know that
>there is a resolution by declaring _set_sbh_threshold(0); but it doesn't work
>and I have the SP5 of VC6 so the realloc function has been corrected.
>
>In fact if I comment the line
> pData[i] = pszFicDescName;
>the program is correctly executed to the end.
Well, duh. That's because you're ignoring the return from both
malloc() and realloc() and -- if you comment the line -- never
writing to the NULL pointer (returned when memory is exhausted).
The CRT memory system is fairly limited. You either need to redesign
your process or at least redesign your memory allocations.
>Thanks
>
>Sebastien
>
-- Sev
- Next message: Carl Daniel [VC++ MVP]: "Re: STL error"
- Previous message: Mark Randall: "sprintf field param errors"
- In reply to: Sebastien LEGUET: "Crash by allocationg small blocks"
- Next in thread: Doug Harrison [MVP]: "Re: Crash by allocationg small blocks"
- Reply: Doug Harrison [MVP]: "Re: Crash by allocationg small blocks"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|