no warning for data truncation?

Discussion in 'C Programming' started by david, Oct 23, 2008.

  1. david

    david Guest

    I've noticed that the following compiles (as C) under both VS8 and gcc
    with no warnings, even though there's a possibility of data truncation
    from enum to unsigned char. It does generate a warning under VS6,
    however. Under VS8, I enabled run-time checks, including /RTCc
    (smaller type check), but still no warning. The only way I can get a
    warning under VS8 is if I configure it to compile as C++.

    Two questions:
    (1) why doesn't /RTCc pick up the data truncation (enum to unsigned
    char)?
    (2) is there any way to configure VS8 (aside from compiling as C++)
    that will generate a warning?

    Here's the code:

    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>

    void foo(unsigned char result);
    void bar(void);

    typedef enum {

    OK = 0,
    NOT_OK = 1,
    NO_STATUS = 0xffff

    } STATUS;

    int main(int argc, char* argv[])
    {
    bar();
    printf("%d %s", argc, argv[0]);
    return 0;
    }

    void bar(void)
    {
    STATUS status = OK;
    foo(status);
    }

    void foo(unsigned char result)
    {
    result++;
    return;
    }
    david, Oct 23, 2008
    #1
    1. Advertising

  2. david

    david Guest

    On Oct 23, 4:40 pm, Richard Heathfield <> wrote:
    >
    > Firstly, make sure /W4 and /Za are switched on. Failing that, I suggest you


    Thanks for the tip. Using /W4 and /Za caused the following warning:

    warning C4244: 'function' : conversion from 'STATUS' to 'unsigned
    char', possible loss of data

    Which is what I was hoping to see.

    Cheers.
    david, Oct 24, 2008
    #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. manas

    Text Truncation

    manas, Jul 1, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    507
    Mark Fitzpatrick
    Jul 1, 2005
  2. Marcelo

    MySql Data Truncation

    Marcelo, Dec 19, 2005, in forum: Java
    Replies:
    3
    Views:
    17,132
    Roedy Green
    Dec 21, 2005
  3. Replies:
    3
    Views:
    1,248
    Mike Beaty
    Oct 11, 2006
  4. Magix

    truncation warning

    Magix, Aug 5, 2004, in forum: C Programming
    Replies:
    2
    Views:
    2,895
    Ravi Uday
    Aug 5, 2004
  5. Alfonso Morra

    pointer truncation from 'void *' to 'int'

    Alfonso Morra, Jul 24, 2005, in forum: C Programming
    Replies:
    26
    Views:
    1,303
    Steve Summit
    Jul 25, 2005
Loading...

Share This Page