🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: pl.comp.lang.c
Article #309890

Re: W której wersji standardu C lub C++ pracujecie?

#309890
From: Maciek Godek
Date: Tue, 13 Aug 2019 16:26
96 lines
4970 bytes
W dniu wtorek, 13 sierpnia 2019 18:08:20 UTC+2 użytkownik PawelS pawel(at)wbcd(dot)pl napisał:
> Maciek Godek pisze:
> > W dniu niedziela, 11 sierpnia 2019 20:12:34 UTC+2 użytkownik PawelS pawel(at)wbcd(dot)pl napisał:
> >> Maciek Godek pisze:
> >>> W dniu wtorek, 6 sierpnia 2019 14:02:33 UTC+2 użytkownik Queequeg napisał:
> >>> To ważne, bo ani -Wall ani nawet -Wextra w GCC nie wyłapie czegoś takiego:
> >>>
> >>> uint32_t f() {
> >>>   return JAKAŚ_DUŻA_WARTOŚĆ;
> >>> }
> >>>
> >>> uint16_t x = f();
> >>>
> >>> (co uważam że jest skandaliczne i byłem w szoku jak to odkryłem. Zresztą nadal jestem w szoku)
> >> A to ciekawe, bo bez żadnych opcji gcc zgłasza ostrzeżenie:
> >>
> >> gcc -v
> >> ------
> >> gcc version 4.1.2 20080704 (Red Hat 4.1.2-44)
> >>
> >> test.c
> >> ------
> >> #include <stdint.h>
> >> uint32_t f() {
> >>          return 0xffffffffffff;
> >> }
> >> int main() {
> >>          uint16_t x = f();
> >>          return 0;
> >> }
> >>
> >> gcc -o test test.c
> >> ------------------
> >> test.c: In function 'f':
> >> test.c:3: warning: integer constant is too large for 'long' type
> >> test.c:3: warning: large integer implicitly truncated to unsigned type
> > 
> > W tym przykładzie zgłasza błąd konwersji stałej. Zamiast tego spróbuj np.
> > 
> > #include <stdint.h>
> > uint32_t f(uint32_t x) {
> >   return x*x;
> > }
> > int main(int argc, char *argv[]) {
> >          uint16_t x = f(argc);
> >          return 0;
> > }
> > 
> > U mnie na telefonie (gcc 6) jest milczenie, choć typ zmiennej ewidentnie się nie zgadza z typem funkcji.
> > (Dopiero -Wconversion pomaga)
> 
> gcc -o test test.c
> ------------------
> 
> gcc -o test test.c -Wconversion
> -------------------------------
> test.c: In function ‘main’:
> test.c:6: warning: passing argument 1 of ‘f’ as unsigned due to prototype
> 
> > choć typ zmiennej ewidentnie się nie zgadza z typem funkcji.
> /usr/include/stdint.h
> typedef unsigned int        uint32_t;

Nie o ten fragment mi chodzi, tylko o przypisanie wartości funkcji do zmiennej.

Może wyraźniej to będzie widoczne tutaj:

#include <stdint.h>
uint32_t f(int32_t x) {
    return x*x;
}

int main(int32_t argc, char *argv[]) {
    (void) argv;
    uint16_t x = f(argc);
    return x;
}

Jak kompilujesz z -Wall -Wextra to cisza. Jak dodasz -Wconversion, to masz

x.c: In function ‘f’:
x.c:3:13: warning: conversion to ‘uint32_t {aka unsigned int}’ from ‘int32_t {aka int}’ may change the sign of the result [-Wsign-conversion]
     return x*x;
            ~^~
x.c: In function ‘main’:
x.c:8:18: warning: conversion to ‘uint16_t {aka short unsigned int}’ from ‘uint32_t {aka unsigned int}’ may alter its value [-Wconversion]
     uint16_t x = f(argc);

Chodzi mi o tego drugiego warninga. Brak takich ostrzeżeń powoduje, że bardzo poważne błędy mogą bardzo łatwo umknąć.

> Być może powinien tutaj zabrać głos ktoś bardziej kompetentny,
> ale "int" od "unsigned int" a różni się tylko znakiem,
> także moim zdaniem ewidentna niezgodność typów danych to byłoby:
> "double" vs "int", "char *" vs "int", "struct ..." vs "int",

Mi chodzi raczej o błędy w rodzaju przypisania wartości uint32_t do zmiennej typu uint16_t. Kwestia znaku też bywa istotna, ale kwestia precyzji jest ważniejsza.
Technical Details
Message-ID: <dc11d608-88c5-4511-bd8d-9e1922cd5e30@googlegroups.com>
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.icm.edu.pl!newsfeed.pionier.net.pl!goblin2!goblin3!goblin.stu.neva.ru!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!b26no5563275qtq.0!news-out.google.com!d29ni1350qtg.1!nntp.google.com!b26no5563272qtq.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
References: <a1fbe21b-aed9-4b32-9691-a824c4e31a21@trust.no1> <3f1215b5-3c16-4248-a74f-1b57d21d4c98@googlegroups.com> <5d505a6e$0$17364$65785112@news.neostrada.pl> <d897b352-477c-42c0-ae44-f2df4bda7361@googlegroups.com> <5d52e072$0$521$65785112@news.neostrada.pl>

Show Full Headers
Complete Article Headers
X-Received: by 2002:a0c:db88:: with SMTP id m8mr15155685qvk.143.1565713596921;        Tue, 13 Aug 2019 09:26:36 -0700 (PDT)
X-Received: by 2002:a81:8706:: with SMTP id x6mr27200943ywf.512.1565713596685; Tue, 13 Aug 2019 09:26:36 -0700 (PDT)
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.icm.edu.pl!newsfeed.pionier.net.pl!goblin2!goblin3!goblin.stu.neva.ru!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!b26no5563275qtq.0!news-out.google.com!d29ni1350qtg.1!nntp.google.com!b26no5563272qtq.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups: pl.comp.lang.c
Date: Tue, 13 Aug 2019 09:26:36 -0700 (PDT)
In-Reply-To: <5d52e072$0$521$65785112@news.neostrada.pl>
Complaints-To: groups-abuse@google.com
Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=213.192.68.153; posting-account=f7iIKQoAAAAkDKpUafc-4IXhmRAzdB5r
NNTP-Posting-Host: 213.192.68.153
References: <a1fbe21b-aed9-4b32-9691-a824c4e31a21@trust.no1> <3f1215b5-3c16-4248-a74f-1b57d21d4c98@googlegroups.com> <5d505a6e$0$17364$65785112@news.neostrada.pl> <d897b352-477c-42c0-ae44-f2df4bda7361@googlegroups.com> <5d52e072$0$521$65785112@news.neostrada.pl>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <dc11d608-88c5-4511-bd8d-9e1922cd5e30@googlegroups.com>
Subject: Re: W której wersji standardu C lub C++ pracujecie?
From: Maciek Godek <godek.maciek@gmail.com>
Injection-Date: Tue, 13 Aug 2019 16:26:36 +0000
Lines: 115
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit