|
From: Joseph K. <joe...@tr...> - 2003-04-18 22:17:10
|
Mike Castle wrote:
> However, I'd like to suggest one change:
>
> --- src/c/wrapper_unix.c 3 Apr 2003 08:10:19 -0000 1.33
> +++ src/c/wrapper_unix.c 15 Apr 2003 22:11:13 -0000
> @@ -213,8 +213,8 @@
> /* number of arguments + 1 for a NULL pointer
> at the end */
> for (i = 0; i <= length; i++) {
> if (i < length) {
> - wrapperData->jvmCommand[i] = (char *)malloc(sizeof(char *) * strlen(strings[i]) + 1);
> - sprintf(wrapperData->jvmCommand[i], strings[i]);
> + wrapperData->jvmCommand[i] = malloc(sizeof(char *) * strlen(strings[i]) + 1);
> + strcpy(wrapperData->jvmCommand[i], strings[i]);
> } else {
> wrapperData->jvmCommand[i] = NULL;
> }
>
> First: in _C_, one should _NEVER_ cast the return value of malloc(). If
> you get warnings without it, your ether your code or compiler is broken.
Hi Mike,
Could you possibly provide a reference for this rule? I have always
cast the results of malloc(), based on the advice contained in
K&R 2nd ed. pg. 142 (K&R2 claims to cover ANSI C), but (obviously)
I haven't done much C programming recently :-) Other sources I've
looked at say that it's not *necessary* to cast malloc's result,
but that it doesn't hurt. So I'm just wondering about your statement
above, which seems to be made with great conviction :-) Does the
ISO C standard recommend against casting?
[Incidentally, I don't want to start a huge thread about this;
but Leif's response seemed to indicate the answer might be of
general interest, which is why I posted to the list rather than
to Mike directly. If anyone wants to argue about the issue, we can
take it to comp.lang.c.]
Thanks,
-- Joe Knapka
|