I see code like
char *p = malloc(strlen(s) + 1); strcpy(p, s);Shouldn't that be malloc((strlen(s) + 1) * sizeof(char))?
It's never necessary to multiply by sizeof(char), since sizeof(char) is, by definition, exactly 1. (On the other hand, multiplying by sizeof(char) doesn't hurt, and may help by introducing a size_t into the expression.) See also question 8.9.
References:
ANSI Sec. 3.3.3.4
ISO Sec. 6.3.3.4
H&S Sec. 7.5.2 p. 195
Read sequentially: prev next up top
This page by Steve Summit // Copyright 1995 // mail feedback