Click to See Complete Forum and Search --> : Using putenv to place a double value in memory


jkepler
June 5th, 2009, 09:50 AM
Hi,

In another Thread , I was kindly given a way to put a string in the environment environment. Now, I'm trying to place a double. I've tryed the following code:


void Setenv(double vx)
{
char* key = "KP_VAL=";
char num[100];
sprintf(num,"%lf",vx);
char* exp = (char*) malloc(strlen(num) + strlen(key) + 1);
strcpy(exp , key );
strcat(exp ,num);
putenv(exp);
free(exp);
free(key);
free(num);
}


but it gives me several errors.

Can someone help me?

Kind regards,

JKepler

olivthill2
June 5th, 2009, 10:10 AM
What are the error messages?

The last two free() are errors because they do not match with malloc(). They are about data defined on the stack which don't need to be freed.

Here is a simplified version:
void Setenv(double vx)
{
char exp[100];
sprintf(exp,"KP_VAL=%lf",vx);
putenv(exp);
}

jkepler
June 5th, 2009, 12:29 PM
Hi,

Thank you very much.

Kind regards,

JKepler

wigga
June 8th, 2009, 11:04 AM
strlen(num) is undefined behavior since num is not initalized only declared.

nvm what i said i was looking over the sprintf