I am getting an error that I don't understand. Compiler is VC7 on Windows XP
Pro. I am not a c/c++ guru so any help would be appreciated. I have already
scanned the newsgroups and understand that this error comes from writing
past an allocated memory block, but how am I doing this?

void Parameter::ValueDup(double val)
{
if (value != NULL) delete value;
sprintf(temp,"%lf",val);
value = strdup(temp);
}

This is a function in a class named Parameter... The error comes on the last
line of the function (strdup). I have checked the variables and everything
seems fine. Variable values just before the crash are the following...

val = 0.00000000000000000
temp = 0.000000
value = NULL

Both "value" and "temp" are class variables originally declared as
follows...

static char temp[256];
char *value; --> set to NULL in constructor

Any ideas?

Re: DAMAGE: after Normal block by Phil

Phil
Wed Aug 13 17:09:30 CDT 2003

Scott wrote:

> I am getting an error that I don't understand. Compiler is VC7 on Windows XP
> Pro. I am not a c/c++ guru so any help would be appreciated. I have already
> scanned the newsgroups and understand that this error comes from writing
> past an allocated memory block, but how am I doing this?
>
> void Parameter::ValueDup(double val)
> {
> if (value != NULL) delete value;
> sprintf(temp,"%lf",val);
> value = strdup(temp);
> }

The only thing unusual I see is your type. You are using type 'f' for a double
which is correct, but you have the type modifier 'l' which is not valid for type
'f'.

> This is a function in a class named Parameter... The error comes on the last
> line of the function (strdup). I have checked the variables and everything
> seems fine. Variable values just before the crash are the following...
>
> val = 0.00000000000000000
> temp = 0.000000
> value = NULL
>
> Both "value" and "temp" are class variables originally declared as
> follows...
>
> static char temp[256];
> char *value; --> set to NULL in constructor
>
> Any ideas?

--
Phil Frisbie, Jr.
Hawk Software
http://www.hawksoft.com


Re: DAMAGE: after Normal block by D

D
Wed Aug 13 19:06:49 CDT 2003

You should use the free function (not delete) for strings returned by
strdup (see the documentation). Other than that, perhaps code elsewhere
is doing the damage and it's just being noticed here.

DJ


Re: DAMAGE: after Normal block by Scott

Scott
Thu Aug 14 10:49:21 CDT 2003

Thanks to both of you for your answers. I eventually tracked the error to
another section of code by using _CrtCheckMemory() statements at diferent
locations to determine when the memory error actually occurred.


"D.J. Heap" <djheap@dhiprovo.com> wrote in message
news:%235vn2ffYDHA.212@TK2MSFTNGP12.phx.gbl...
> You should use the free function (not delete) for strings returned by
> strdup (see the documentation). Other than that, perhaps code elsewhere
> is doing the damage and it's just being noticed here.
>
> DJ
>