Re: memory allocation problems



Hi Steve,

You should not do any cleanup on values returned by ctime.

My suggestion is to remove the line "free(curtime)". The data
is not dynamically allocated, and should therefore not be free'd

This is probably why the function performs well the first time you
call it.

kind regards
Emil Kvarnhammar
http://www.ynax.com

"Steve Long" <Steve_Noneya@xxxxxxxxxx> wrote in message
news:OkbtP6iZFHA.612@xxxxxxxxxxxxxxxxxxxxxxx
> Victor,
> Thanks for your reply. Here's the entire body of the function. I don't
> seem
> to be able to get (nothrow) to work. I've include <new>:
>
> void WriteMessage(FILE* fp, const char* msg)
> {
> time_t curr = time(0);
> char* curtime = ctime((const time_t*) &curr);
> char* buffer = new char[(strlen(curtime) + strlen(msg) + 3)];
> strcpy(buffer, curtime);
> strcat(buffer, msg);
> strcat(buffer, "\n\n");
> if (fp != NULL)
> fwrite(buffer, 1, strlen(buffer), fp);
> free(curtime);
> delete[] buffer;
> return;
> }
>
>
> "Victor Bazarov" <v.Abazarov@xxxxxxxxxxxx> wrote in message
> news:%23%23%23TfRiZFHA.580@xxxxxxxxxxxxxxxxxxxxxxx
>> Steve Long wrote:
>> > this is probably most fundamental but I don't code in C++ often and I'm
> just
>> > not getting why the code is crashing with a:
>> > Unhandled exception at 0x77f75a58 in FileIOTest.exe: User breakpoint
>> >
>> > I have a function that calls this line of code:
>> >
>> > char* buffer = new char[sizeof(char) * (strlen(curtime) + strlen(msg) +
> 3)];
>>
>> OK, sizeof(char) is always 1, you can easily drop it. Now, what is
>> 'curtime'? Is it something that can be passed to 'strlen'? What is
>> 'msg'? Same question, can it be passed to 'strlen'? Remember that
>> 'strlen' is very fragile, it has undefined behaviour if you pass NULL
>> to it...
>>
>> > The first time I call the function, all is well but subsequent times it
>> > crashes with the above exception. All I'm doing is a strcpy, strcat and
>> > writing to a file in the funciton and then I delete buffer memory using
>> > delete.
>> >
>> > What could the problem be. Sorry is this is just really stupid...
>>
>> Could be that you're just running out of memory... Try
>>
>> char* buffer = new (nothrow) char[strlen(curtime)
>> + strlen(msg) + 3];
>>
>> and see if NULL is returned.
>>
>> V
>
>


.



Relevant Pages

  • Re: memory allocation problems
    ... void WriteMessage(FILE* fp, const char* msg) ... >> Unhandled exception at 0x77f75a58 in FileIOTest.exe: ... >> The first time I call the function, all is well but subsequent times it ... >> writing to a file in the funciton and then I delete buffer memory using ...
    (microsoft.public.vc.language)
  • Re: memory allocation problems
    ... > void WriteMessage(FILE* fp, const char* msg) ... > strcpy(buffer, curtime); ... >>> The first time I call the function, all is well but subsequent times it ... >>> writing to a file in the funciton and then I delete buffer memory using ...
    (microsoft.public.vc.language)
  • Re: memory allocation problems
    ... > Unhandled exception at 0x77f75a58 in FileIOTest.exe: ... > The first time I call the function, ... > writing to a file in the funciton and then I delete buffer memory ... int nLenCurTime = strlen; ...
    (microsoft.public.vc.language)
  • Have you seen this error msg before?
    ... I use my laptop alone and have done for 2 years. ... Today I got this msg for ... the first time: ... Your system policy has denied access to the requested URL. ...
    (microsoft.public.windowsxp.general)
  • Can anyone help with systems policy, please?
    ... I use my laptop alone and have done for 2 years. ... Today I got this msg for ... the first time: ... Your system policy has denied access to the requested URL. ...
    (microsoft.public.windowsxp.general)