Chad <> wrote:
> Given something like
> #include <stdio.h>
> #include <string.h>
>
> int main(void)
> {
> char name[] = "chad";
> int number = 2;
> int val;
>
> size_t len = strlen(name);
>
> val = len/number ;
>
> return 0;
> }
>
> [cdalten@localhost ~]$ gcc -g -Wall -ansi -pedantic div.c -o div
>
> What if name[] is something really really long. Long enough to fill up
> size_t in len. Wouldn't this value get truncated since val is int?
Possibly, depending on the sizes of size_t and int. If size_t is at
least as large as int, the division is done unsigned. If it's exactly as
large as int, the result of dividing (what is in effect) any unsigned
int value by two is guaranteed to fit in a signed int.
> And if it does possibly get truncated, how come my compiler doesn't say
> anyting?
Who knows the motivations of the Ganuck implementors? Perhaps they do
static analysis on your code, and decide that in this case, strlen(name)
is always 4. Perhaps you need to crank up the optimisation level;
perhaps down.
Richard
|