0

To produce millisecond using clock function after strcat but it simply crash, what could be the problem?

FREObject result = 0;

uint32_t len = -1;
const uint8_t *str = 0;
char *temp = "Millisecond: ";
uint8_t *strAll;

clock_t curtime = clock();

double *asc = (double *) curtime;  //Using double datatype?

if(FREGetObjectAsUTF8(argv[0], &len, &str) == FRE_OK) {

  strAll = (char *)malloc(strlen(temp) + strlen(str) + 1 + strlen(asc) + 1);
  strcpy(strAll,temp);
  strcat(strAll,str);
  strcat(strAll," ");
  strcat(strAll,(char *)asc);  //Is this correct?
}

FRENewObjectFromUTF8(strlen((const char *)strAll)+1, (const uint8_t *)strAll, &result);         

return result;
James ONG
  • 47
  • 1
  • 7

2 Answers2

2

The assignment to asc is extremely dubious:

double *asc = (double *) curtime;  //Using double datatype?

You can measure the difference between two clock() values, to determine the processor time used.

You might get more nearly successful results with:

double asc = curtime;

The strlen() operation is undefined - under either definition of asc:

strAll = (char *)malloc(strlen(temp) + strlen(str) + 1 + strlen(asc) + 1);

Then the strcat() operation is going fail horribly; you have to convert the double to a string before you can concatenate the string to another.

strcat(strAll,(char *)asc);  //Is this correct?

So, you have a lot of work to do. You need to decide whether clock() is the right system call. If it is, you have to fix the sizing and the conversion of asc or curtime to a string before you concatenate it to the result.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Also, I think `clock_t` is a long integer rather than a double. – Timothy Jones Dec 28 '11 at 02:53
  • thx, seem like a lot to work on double to string, I don't find any string function on C? So will have to use Array? – James ONG Dec 28 '11 at 03:07
  • 1
    @JamesONG to convert a `double` (or a `long`) to a string in C, I would recommend [`sprintf()`](http://linux.die.net/man/3/sprintf). – Timothy Jones Dec 28 '11 at 03:18
  • 1
    @JamesONG: You need to have *strings* in C `NUL` terminated. `malloc` is not assured to 0 fill, you can use `calloc` instead. Dont typecast the return value of `malloc` or `calloc` if your code is in C. And as Timothy Jones has suggested you can make use of `sprintf` or `snprintf` instead of `strcpy` & `strcat` calls in your current case. – another.anon.coward Dec 28 '11 at 03:23
2

Jonathan Leffler's answer outlines a number of things you will need to sort out before your code will work. If you haven't already, you should give him an upvote :)


In your current code, the cast to (char*) in your code tells the compiler to treat the memory where asc is stored as a char*, but it won't do the conversion to string for you. In order to convert a double to a string, have a look at sprintf(). I would replace the strcat lines with this:

sprintf(strAll,"%s%s %f",temp,str,asc);

Note that you would first need to make sure that strAll is big enough to contain the resulting string. This could be a little bit tricky for a double. However, I don't really think you have a double - clock_t is probably a long, which means you could do this:

long asc = (long) curtime;

...

strAll = malloc(strlen(temp) + strlen(str) + 1 + (asc/10 +1) + 1);
sprintf(strAll,"%s%s %ld",temp,str,asc);

Warning: asc/10 +1 only tells you how many characters you need to allocate for a non-negative number. Since clock is documented to return negative in error cases, you should first check for that.

Note that I also removed the cast to char* since it can cause problems in C (thanks to an anonymous commenter).

Also: Currently the result of malloc isn't checked for errors - on error, it will return NULL, which will break attempts to use strAll. Consequently, it's a good idea to make sure it isn't NULL before trying to use it.

Community
  • 1
  • 1
Timothy Jones
  • 21,495
  • 6
  • 60
  • 90