Fixed memory leak crash

pull/76/head
parent 94db8460cc
commit 00baa7faf7

@ -258,37 +258,30 @@ char *yon_char_from_int(int int_to_convert)
char *yon_char_from_float(float int_to_convert) char *yon_char_from_float(float int_to_convert)
{ {
int i = 1; int i = snprintf( NULL, 0, "%f", int_to_convert)+1;
float convert_check = (float)int_to_convert;
for (i = 1; convert_check >= 10; i++) char *ch = malloc(i * sizeof(char));
{ memset(ch,0,i * sizeof(char));
convert_check = convert_check / 10;
}
char *ch = malloc((i + 9)* sizeof(char)+ int_to_convert<0?1:0);
memset(ch,0,(i + 9)* sizeof(char));
sprintf(ch, "%.2f", int_to_convert); sprintf(ch, "%.2f", int_to_convert);
return ch; return ch;
} }
char *yon_char_from_double(double int_to_convert) char *yon_char_from_double(double int_to_convert)
{ {
int i = 1; int i = snprintf( NULL, 0, "%f", int_to_convert)+1;
double convert_check = (double)int_to_convert;
for (i = 1; convert_check >= 10; i++) char *ch = malloc(i * sizeof(char));
{ memset(ch,0,i * sizeof(char));
convert_check = convert_check / 10;
}
char *ch = malloc((i + 9)* sizeof(char)+ int_to_convert<0?1:0);
memset(ch,0,(i + 9)* sizeof(char));
sprintf(ch, "%.2f", int_to_convert); sprintf(ch, "%.2f", int_to_convert);
return ch; return ch;
} }
char *yon_char_from_long(long int_to_convert) char *yon_char_from_long(long int_to_convert)
{ {
size_t size = 20 * sizeof(char) + 1 + int_to_convert<0?1:0; int i = snprintf( NULL, 0, "%ld", int_to_convert)+1;
char *ch = malloc(size);
memset(ch,0,size); char *ch = malloc(i * sizeof(char));
memset(ch,0,i * sizeof(char));
sprintf(ch, "%ld", int_to_convert); sprintf(ch, "%ld", int_to_convert);
return ch; return ch;
} }

Loading…
Cancel
Save