The majority of forums are now only available as archives, which means posting/editing is disabled.
The Anything and Everything forum is still open.
The Anything and Everything forum is still open.
malloc problem
|
|||
|
Rank: ? (6)
Member #: 28706 |
Hi,
a small malloc problem . Below is part of the code of a function for converting float values into floating point: Code: char* floating_point(float flt) { char *formula; formula = malloc(21); return formula; } The program exits without warning when it reaches the malloc line in the code above. To be complete, here is how I call the function: Code: fa_string = floating_point(formula_a); fprintf(fpout,"%s\n", fa_string); free(fa_string); What is wrong here? Thanks for any help. |
||
|
|||
|
|||
|
Rank: ? (119)
Member #: 28292 |
The entire code please
|
||
|
|||
|
|||
|
Rank: ? (6)
Member #: 28706 |
Hi igorok,
Here is the full code... /******************************************************************************/ char* floating_point(float flt) { char hex[9], hex_2[9], temp[3], *p; char *formula; int decimal[4], i=0, ii=0; short cnt=0, cnt2=0; union { unsigned long i; float f; }v; if(!(formula = malloc(21))) { free(formula);//free up resources if(!(formula = malloc(21))) exit(-1); } v.f = flt; sprintf(hex_2,"%08lx",v.i); puts(hex_2); cnt=7; while(cnt2<=3) { sprintf(temp,"%c%c",hex_2[cnt-1],hex_2[cnt]); printf("%s\t",temp); decimal[cnt2] = strtol(temp,&p,16); cnt-=2; cnt2++; } printf("\n" sprintf(formula,"%d\t%d\t%d\t%d",decimal[0],decimal[1],decimal[2],decimal[3]); puts(formula); return formula; } /******************************************************************************/ Thanks |
||
|
|||
|
|||
|
Rank: ? (4)
Member #: 28871 |
Replace all `malloc(21);` calls with `(char*)malloc(21);` and you're set.
Cheers, T-Metal » Post edited 2006-10-20, 07:49pm by T-Metal.
|
||
|
Please login or register to post a reply.
