Is there possible funky division going on here?

Discussion in 'C Programming' started by Chad, Jun 19, 2007.

  1. Chad

    Chad Guest

    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? And
    if it does possibly get truncated, how come my compiler doesn't say
    anyting?

    Chad
    Chad, Jun 19, 2007
    #1
    1. Advertising

  2. Chad

    Richard Bos Guest

    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
    Richard Bos, Jun 19, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Diogenes Dilone

    Funky error Situation

    Diogenes Dilone, Jan 6, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    516
    Diogenes Dilone
    Jan 7, 2004
  2. =?Utf-8?B?VHJhY3k=?=

    Funky address change.

    =?Utf-8?B?VHJhY3k=?=, Mar 16, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    362
    Juan T. Llibre
    Mar 16, 2006
  3. iksrazal

    funky but simple regex

    iksrazal, Jul 19, 2004, in forum: Java
    Replies:
    3
    Views:
    370
    iksrazal
    Jul 20, 2004
  4. Replies:
    94
    Views:
    4,404
    ┬Ča\\/b
    Feb 9, 2007
  5. grocery_stocker

    How to resolve funky sync issues with fork here.

    grocery_stocker, Apr 4, 2007, in forum: Perl Misc
    Replies:
    12
    Views:
    413
    Peter J. Holzer
    Apr 5, 2007
Loading...

Share This Page