no warning for data truncation?

D

david

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;
}
 
D

david

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top