Home > Archive > VC Language > June 2005 > memory allocation problems
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
memory allocation problems
|
|
| Steve Long 2005-05-31, 9:00 pm |
| Hello,
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)];
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...
Steve
| |
| Victor Bazarov 2005-05-31, 9:00 pm |
| 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
| |
| David Webber 2005-05-31, 9:00 pm |
|
"Steve Long" <Steve_Noneya@NoSpam.com> wrote in message
news:O5gJsMiZFHA.2420@TK2MSFTNGP12.phx.gbl...
> Hello,
> 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)];
>
> 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...
As Victor says it all rather depends on what te values of curtime
and msg are.
Why not replace it with
int nLenCurTime = strlen(curtime);
int nLenMsg = strlen(msg);
int nBytesNeeded = nLenCurTime+nLenMsg+3;
char *buffer = new char [nBytesNeeded];
..
..
..
delete [] buffer;
and then step through with te debugger.
(I take it you *are* remembering the [] in the delete statement?)
Dave
--
David Webber
Author MOZART the music processor for Windows -
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mzusers/mailinglist.htm
| |
| Steve Long 2005-05-31, 9:00 pm |
| 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@comAcast.net> wrote in message
news:%23%23%23TfRiZFHA.580@TK2MSFTNGP15.phx.gbl...
> Steve Long wrote:
just[color=darkred]
3)];[color=darkred]
>
> 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...
>
>
> 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
| |
| Simon Trew 2005-05-31, 9:00 pm |
| "Steve Long" <Steve_Noneya@NoSpam.com> wrote in message
news:OkbtP6iZFHA.612@TK2MSFTNGP12.phx.gbl...
> 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>:
You may need new (std::nothrow) if you haven't got 'using namespace std'
anywhere in scope (and pray God you have not).
S.
| |
| Simon Trew 2005-05-31, 9:00 pm |
| "David Webber" <dave@musical.demon.co.uk> wrote in message
news:ec1vzviZFHA.584@TK2MSFTNGP15.phx.gbl...
> (I take it you *are* remembering the [] in the delete statement?)
On VC6 and 7.0 implementations (haven't tried it on 7.1 or 8b2), it
magically works anyway. It's undefined behavior, of course, but it's
unlikely to cause this particular bug.
| |
| Steve Long 2005-05-31, 9:00 pm |
| I'm passing 25 + 25 + 3. How could that cause an exception? I posted the
code for the entire function body a little earlier.
"David Webber" <dave@musical.demon.co.uk> wrote in message
news:ec1vzviZFHA.584@TK2MSFTNGP15.phx.gbl...
>
> "Steve Long" <Steve_Noneya@NoSpam.com> wrote in message
> news:O5gJsMiZFHA.2420@TK2MSFTNGP12.phx.gbl...
>
>
> As Victor says it all rather depends on what te values of curtime
> and msg are.
>
> Why not replace it with
>
> int nLenCurTime = strlen(curtime);
> int nLenMsg = strlen(msg);
> int nBytesNeeded = nLenCurTime+nLenMsg+3;
>
> char *buffer = new char [nBytesNeeded];
> .
> .
> .
> delete [] buffer;
>
> and then step through with te debugger.
>
> (I take it you *are* remembering the [] in the delete statement?)
>
> Dave
> --
> David Webber
> Author MOZART the music processor for Windows -
> http://www.mozart.co.uk
> For discussion/support see
> http://www.mozart.co.uk/mzusers/mailinglist.htm
>
>
>
>
>
>
| |
| Emil Kvarnhammar 2005-05-31, 9:00 pm |
| 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@NoSpam.com> wrote in message
news:OkbtP6iZFHA.612@TK2MSFTNGP12.phx.gbl...
> 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@comAcast.net> wrote in message
> news:%23%23%23TfRiZFHA.580@TK2MSFTNGP15.phx.gbl...
> just
> 3)];
>
>
| |
| Steve Long 2005-05-31, 9:00 pm |
| Sweet. That was the problem. Thank you so much Emil.
Steve
"Emil Kvarnhammar" <info@ynaxREMOVETHIS.com> wrote in message
news:Og5I6DjZFHA.2756@tk2msftngp13.phx.gbl...
> 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@NoSpam.com> wrote in message
> news:OkbtP6iZFHA.612@TK2MSFTNGP12.phx.gbl...
I'm[color=darkred]
+[color=darkred]
it[color=darkred]
and[color=darkred]
using[color=darkred]
>
>
| |
| David Webber 2005-06-01, 4:00 am |
|
"Simon Trew" <ten.enagro@werts> wrote in message
news:OgcRzDjZFHA.2796@TK2MSFTNGP10.phx.gbl...
>
> On VC6 and 7.0 implementations (haven't tried it on 7.1 or 8b2),
> it magically works anyway. It's undefined behavior, of course, but
> it's unlikely to cause this particular bug.
Interesting. I didn't know that.
I'll now try to forget I do and go on writing correct code :-)
Dave
--
David Webber
Author MOZART the music processor for Windows -
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mzusers/mailinglist.htm
| |
| Alexander Grigoriev 2005-06-01, 4:00 am |
| I can't help but wonder what was wrong with simply:
fprintf(fp, "%s%s\n\n", curtime, msg);
No funky allocations, no nothing...
"Steve Long" <Steve_Noneya@NoSpam.com> wrote in message
news:OkbtP6iZFHA.612@TK2MSFTNGP12.phx.gbl...
> 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@comAcast.net> wrote in message
> news:%23%23%23TfRiZFHA.580@TK2MSFTNGP15.phx.gbl...
> just
> 3)];
>
>
| |
| Steve Long 2005-06-01, 4:03 pm |
| Hmmm, hadn't thought of it. Looks clean. I think I'll try it.
Thanks
Steve
"Alexander Grigoriev" <alegr@earthlink.net> wrote in message
news:%23jFxdxmZFHA.3840@tk2msftngp13.phx.gbl...
> I can't help but wonder what was wrong with simply:
>
> fprintf(fp, "%s%s\n\n", curtime, msg);
>
> No funky allocations, no nothing...
>
> "Steve Long" <Steve_Noneya@NoSpam.com> wrote in message
> news:OkbtP6iZFHA.612@TK2MSFTNGP12.phx.gbl...
I'm[color=darkred]
+[color=darkred]
it[color=darkred]
and[color=darkred]
using[color=darkred]
>
>
|
|
|
|
|