Converting to 'size_t from' int can change the sign of the result - GCC, C

In my project, I included warning handling as errors and compilation using the -pedantic and -ansi tags. I am using the GCC compiler. In this project, I have to use third-party source code that contains a lot of warnings. Since I treat warnings as errors, I have a hard time fixing their code.

Most warnings concern invalid conversion from int to size_t or vice versa. In some cases, I will not be able to make both variables the same, I mean that I will not be able to change something to size_t . In such cases, I do an explicit cast. Sort of,

 size_t a = (size_t) atoi(val); 

I wonder is this the right approach? Are there any problems when doing this?

If these warnings are minor, can I suppress them only in my files? How do I do the same on MSVC?

+4
source share
3 answers

Edit:

Casting is the only approach if you want to lock the compiler on one instance in a portable way. This is great if you know what you are doing, for example. that you can guarantee that the result of atoi will never be negative.

In GCC, you can disable all sign conversion warnings with the -Wno-sign-conversion flag. There is also -Wno-sign-compare (for things like 2u > 1 ), but this will not be relevant unless you use -Wextra .

You can also use diagnostic pragmas , for example

 #pragma GCC diagnostic ignored "-Wsign-conversion" 

There are several warnings in MSVC regarding signed / unsigned character mismatch, for example:

To disable the warning in MSVC, you can add #pragma warning , for example.

 #pragma warning (disable : 4267) 

or add the /wd4267 flag to the compiler options.


Perhaps you should use strtoul instead of atoi .

 size_t a = strtoul(val, NULL, 0); 

(There is no warning only if size_t is sized as unsigned long . On most platforms this is true, but it is not guaranteed.)

The advantage is that you can perform error checking with this function, for example

 #include <stdlib.h> #include <stdio.h> int main () { char val[256]; fgets(val, 256, stdin); char* endptr; size_t a = strtoul(val, &endptr, 0); if (val == endptr) { printf("Not a number\n"); } else { printf("The value is %zu\n", a); } return 0; } 
+7
source

Take a look at the OpenOffice wiki for best practices for error-free code: http://wiki.services.openoffice.org/wiki/Writing_warning-free_code

They offer static casts for these conversions, and then provide the pragma to disable warnings for a specific section of code.

+4
source

I personally consider this type of warning idiotic and turn it off, but the fact that you are asking about it suggests that you may not be familiar with the conversions between whole types and differences in signed and unsigned behavior, that a warning may be for you useful.

On the other hand, I really despise the [explicit] throws. The suggestion to use strtoul instead of atoi is probably very good. I see that you noted that atoi was just an example, but in principle the same principle applies: use functions that return the type you need, and not force the other type to the type you need. If the function is the one you wrote and not the library function, this can only mean fixing your functions to return size_t for sizes, not int .

-2
source

All Articles