public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/110798] New: The reusult of sizeof operator followed with an 'unsigned typedef-ed generic integer' type is alway 4 bytes(except char)
@ 2023-07-25  6:49 13958014620 at 139 dot com
  2023-07-25  6:59 ` [Bug c++/110798] [12/13 Regression] " pinskia at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: 13958014620 at 139 dot com @ 2023-07-25  6:49 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110798

            Bug ID: 110798
           Summary: The reusult of sizeof operator followed with an
                    'unsigned typedef-ed generic integer' type is alway 4
                    bytes(except char)
           Product: gcc
           Version: og12 (devel/omp/gcc-12)
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: 13958014620 at 139 dot com
  Target Milestone: ---

Some issues arised when doing some tests.
The reusult of sizeof operator followed with an 'unsigned typedef-ed generic
integer' type is always 4 bytes(except char).

The following example had run in fedora 37 OS with gcc_12.3.1 and
gcc_13.1.0(compiled myself), both versions of gcc show the same issue.


#include <cassert>
int main(int argc, char ** argv){
        int ret=0;

    typedef char ch_t;
        assert( sizeof(unsigned char) == 1); //right
        assert( sizeof(ch_t) == 1 ); //right
        assert( sizeof(unsigned ch_t) == 1); //the only one right

    typedef signed char sch;
        assert( sizeof(unsigned char) == 1); //right
        assert( sizeof(sch) == 1 ); //right
        assert( sizeof(unsigned sch) == 4); //wrong, which should be 1

    typedef short sint;
        assert( sizeof(unsigned short) == 2 ); //right
        assert( sizeof(sint) == 2 ); //right
        assert( sizeof(unsigned sint) == 4); //wrong, which should be 8

    typedef long lint;
        assert( sizeof(unsigned long) == 8); //right
        assert( sizeof(lint) == 8 ); //right
        assert( sizeof(unsigned lint) == 4); //wrong, which should be 8

    typedef long long llint;
        assert( sizeof(unsigned long long) == 8); //right
        assert( sizeof(llint) == 8 ); //right
        assert( sizeof(unsigned llint) ==4); //wrong, which should be 8

    /* The same errors as: 
       'typedef signed short ssint', 
       'typedef signed long slint', 
       'typedef signed long long sllint', 
       'typedef unsigned short usint', 
       'typedef unsigned long ulint', 
       'typedef unsigned long long ullint', 
        so that seems all sizeof(unsigned <typedef-ed char/short/int/long
type>) == 4, 
        except typedef char ch_t;
    */
        return ret;
}
----------- or a version outputting the result on the terminal --------------

#include <iostream>
#include <cassert>
using namespace std;
int main(int argc, char ** argv){
    int ret=0;

    typedef char ch_t;
    cout << "typedef char ch_t;"<<endl;
        assert( sizeof(unsigned char) == 1); //right
        cout << "\tsizeof(unsigned char)=" << sizeof(unsigned char) <<endl;
        assert( sizeof(ch_t) == 1 ); //right
        cout << "\tsizeof(ch_t)=" << sizeof(ch_t) <<endl;
        assert( sizeof(unsigned ch_t) == 1); //the only one right
        cout << "\tsizeof(unsigned ch_t)=" << sizeof(unsigned ch_t)  << " --
the only one right"<<endl;

    typedef signed char sch;
    cout << endl << "typedef signed char sch;"<<endl;
        assert( sizeof(unsigned char) == 1); //right
        cout << "\tsizeof(unsigned char)=" << sizeof(unsigned char) <<endl;
        assert( sizeof(sch) == 1 ); //right
        cout << "\tsizeof(sch)=" << sizeof(sch) <<endl;
        assert( sizeof(unsigned sch) == 4); //wrong, which should be 1
        cout << "\tsizeof(unsigned sch)=" << sizeof(unsigned sch)  << " --
wrong"<<endl;

    typedef short sint;
    cout << endl << "typedef short sint;" <<endl;
        assert( sizeof(unsigned short) == 2 ); //right
        cout << "\tsizeof(unsigned short)=" << sizeof(unsigned short int)
<<endl;
        assert( sizeof(sint) == 2 ); //right
        cout << "\tsizeof(sint)=" << sizeof(sint) <<endl;
        assert( sizeof(unsigned sint) == 4); //wrong, which should be 8
        cout << "\tsizeof(unsigned sint)=" << sizeof(unsigned sint)  << " --
wrong"<<endl;

    typedef long lint;
    cout << endl << "typedef long lint;" <<endl;
        assert( sizeof(unsigned long) == 8); //right
        cout << "\tsizeof(unsigned long)=" << sizeof(unsigned long int) <<endl;
        assert( sizeof(lint) == 8 ); //right
        cout << "\tsizeof(lint)=" << sizeof(lint) <<endl;
        assert( sizeof(unsigned lint) == 4); //wrong, which should be 8
        cout << "\tsizeof(unsigned lint)=" << sizeof(unsigned lint)  << " --
wrong"<<endl;

    typedef long long llint;
    cout << endl << "typedef long long llint;" <<endl; 
        assert( sizeof(unsigned long long) == 8); //right
        cout << "\tsizeof(unsigned long long)=" << sizeof(unsigned long long
int) <<endl;

        assert( sizeof(llint) == 8 ); //right
        cout << "\tsizeof(llint)=" << sizeof(llint) <<endl;

        assert( sizeof(unsigned llint) ==4); //wrong, which should be 8
        cout << "\tsizeof(unsigned llint)=" << sizeof(unsigned llint) << " --
wrong"<<endl;

    cout << endl << "The same errors as: \n\
       'typedef signed short ssint', \n\
       'typedef signed long slint', \n\
       'typedef signed long long sllint', \n\
       'typedef unsigned short usint', \n\
       'typedef unsigned long ulint', \n\
       'typedef unsigned long long ullint', \n\
so that seems all sizeof(unsigned <typedef-ed char/short/int/long type>) == 4,
\n\
except typedef char ch_t;" << endl;
        return ret;
}

These issues can't reproduce in pure C language with GCC 12 and 13.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/110798] [12/13 Regression] The reusult of sizeof operator followed with an 'unsigned typedef-ed generic integer' type is alway 4 bytes(except char)
  2023-07-25  6:49 [Bug c++/110798] New: The reusult of sizeof operator followed with an 'unsigned typedef-ed generic integer' type is alway 4 bytes(except char) 13958014620 at 139 dot com
@ 2023-07-25  6:59 ` pinskia at gcc dot gnu.org
  2023-07-25  7:02 ` [Bug c++/110798] [12 " pinskia at gcc dot gnu.org
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-07-25  6:59 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110798

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |12.4
           Keywords|                            |needs-bisection
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=108099

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/110798] [12 Regression] The reusult of sizeof operator followed with an 'unsigned typedef-ed generic integer' type is alway 4 bytes(except char)
  2023-07-25  6:49 [Bug c++/110798] New: The reusult of sizeof operator followed with an 'unsigned typedef-ed generic integer' type is alway 4 bytes(except char) 13958014620 at 139 dot com
  2023-07-25  6:59 ` [Bug c++/110798] [12/13 Regression] " pinskia at gcc dot gnu.org
@ 2023-07-25  7:02 ` pinskia at gcc dot gnu.org
  2023-07-25  7:14 ` 13958014620 at 139 dot com
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-07-25  7:02 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110798

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|[12/13 Regression] The      |[12 Regression] The reusult
                   |reusult of sizeof operator  |of sizeof operator followed
                   |followed with an 'unsigned  |with an 'unsigned
                   |typedef-ed generic integer' |typedef-ed generic integer'
                   |type is alway 4             |type is alway 4
                   |bytes(except char)          |bytes(except char)
      Known to work|                            |13.1.1

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I almost positive this was fixed by r14-159-g03cebd304955a6 which was
backported to GCC 13 branch r13-7277-ga713aa4f47ac1e (for 13.2.0) .

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/110798] [12 Regression] The reusult of sizeof operator followed with an 'unsigned typedef-ed generic integer' type is alway 4 bytes(except char)
  2023-07-25  6:49 [Bug c++/110798] New: The reusult of sizeof operator followed with an 'unsigned typedef-ed generic integer' type is alway 4 bytes(except char) 13958014620 at 139 dot com
  2023-07-25  6:59 ` [Bug c++/110798] [12/13 Regression] " pinskia at gcc dot gnu.org
  2023-07-25  7:02 ` [Bug c++/110798] [12 " pinskia at gcc dot gnu.org
@ 2023-07-25  7:14 ` 13958014620 at 139 dot com
  2023-07-25  9:59 ` rguenth at gcc dot gnu.org
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: 13958014620 at 139 dot com @ 2023-07-25  7:14 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110798

--- Comment #2 from miles <13958014620 at 139 dot com> ---
(In reply to Andrew Pinski from comment #1)
> I almost positive this was fixed by r14-159-g03cebd304955a6 which was
> backported to GCC 13 branch r13-7277-ga713aa4f47ac1e (for 13.2.0) .

OK,Andrew
Thx for reply

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/110798] [12 Regression] The reusult of sizeof operator followed with an 'unsigned typedef-ed generic integer' type is alway 4 bytes(except char)
  2023-07-25  6:49 [Bug c++/110798] New: The reusult of sizeof operator followed with an 'unsigned typedef-ed generic integer' type is alway 4 bytes(except char) 13958014620 at 139 dot com
                   ` (2 preceding siblings ...)
  2023-07-25  7:14 ` 13958014620 at 139 dot com
@ 2023-07-25  9:59 ` rguenth at gcc dot gnu.org
  2023-07-25 10:39 ` redi at gcc dot gnu.org
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-07-25  9:59 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110798

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P3                          |P2
      Known to work|                            |13.1.1, 14.0

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/110798] [12 Regression] The reusult of sizeof operator followed with an 'unsigned typedef-ed generic integer' type is alway 4 bytes(except char)
  2023-07-25  6:49 [Bug c++/110798] New: The reusult of sizeof operator followed with an 'unsigned typedef-ed generic integer' type is alway 4 bytes(except char) 13958014620 at 139 dot com
                   ` (3 preceding siblings ...)
  2023-07-25  9:59 ` rguenth at gcc dot gnu.org
@ 2023-07-25 10:39 ` redi at gcc dot gnu.org
  2023-07-25 10:50 ` redi at gcc dot gnu.org
  2023-07-26  5:11 ` 13958014620 at 139 dot com
  6 siblings, 0 replies; 8+ messages in thread
From: redi at gcc dot gnu.org @ 2023-07-25 10:39 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110798

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to miles from comment #0)
>     typedef char ch_t;
>         assert( sizeof(unsigned char) == 1); //right
>         assert( sizeof(ch_t) == 1 ); //right
>         assert( sizeof(unsigned ch_t) == 1); //the only one right

This is not valid C++, you can't use 'signed' and 'unsigned' with typedefs.

Use std::make_unsigned_t<ch_t>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/110798] [12 Regression] The reusult of sizeof operator followed with an 'unsigned typedef-ed generic integer' type is alway 4 bytes(except char)
  2023-07-25  6:49 [Bug c++/110798] New: The reusult of sizeof operator followed with an 'unsigned typedef-ed generic integer' type is alway 4 bytes(except char) 13958014620 at 139 dot com
                   ` (4 preceding siblings ...)
  2023-07-25 10:39 ` redi at gcc dot gnu.org
@ 2023-07-25 10:50 ` redi at gcc dot gnu.org
  2023-07-26  5:11 ` 13958014620 at 139 dot com
  6 siblings, 0 replies; 8+ messages in thread
From: redi at gcc dot gnu.org @ 2023-07-25 10:50 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110798

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |DUPLICATE
             Status|UNCONFIRMED                 |RESOLVED
           Keywords|needs-bisection             |

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #1)
> I almost positive this was fixed by r14-159-g03cebd304955a6 which was
> backported to GCC 13 branch r13-7277-ga713aa4f47ac1e (for 13.2.0) .

Yes, bisection confirms it. So this is a dup.

Aside: I'm not sure I'd call this a wrong-code bug. The testcase would be
simpler if it used static_assert instead of assert, which would make it
accepts-invalid / rejects-valid instead.

*** This bug has been marked as a duplicate of bug 108099 ***

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c++/110798] [12 Regression] The reusult of sizeof operator followed with an 'unsigned typedef-ed generic integer' type is alway 4 bytes(except char)
  2023-07-25  6:49 [Bug c++/110798] New: The reusult of sizeof operator followed with an 'unsigned typedef-ed generic integer' type is alway 4 bytes(except char) 13958014620 at 139 dot com
                   ` (5 preceding siblings ...)
  2023-07-25 10:50 ` redi at gcc dot gnu.org
@ 2023-07-26  5:11 ` 13958014620 at 139 dot com
  6 siblings, 0 replies; 8+ messages in thread
From: 13958014620 at 139 dot com @ 2023-07-26  5:11 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110798

--- Comment #5 from miles <13958014620 at 139 dot com> ---
(In reply to Jonathan Wakely from comment #4)
> (In reply to Andrew Pinski from comment #1)
> > I almost positive this was fixed by r14-159-g03cebd304955a6 which was
> > backported to GCC 13 branch r13-7277-ga713aa4f47ac1e (for 13.2.0) .
> 
> Yes, bisection confirms it. So this is a dup.
> 
> Aside: I'm not sure I'd call this a wrong-code bug. The testcase would be
> simpler if it used static_assert instead of assert, which would make it
> accepts-invalid / rejects-valid instead.
> 
> *** This bug has been marked as a duplicate of bug 108099 ***


> Aside: I'm not sure I'd call this a wrong-code bug.
Yep, the "unsigned" keyword qualifies an typedef-ed type is illegal according
to ISO_14882. It's acceptable that the compiler reports a failure, at least a
warning.

>The testcase would be simpler if it used static_assert instead of assert, which would make itaccepts-invalid / rejects-valid instead.
Thanks a lot for your suggestion!

The experience of discovering this issue is very interesting.
I wrote a macro function to show the attributes of fundamental type for my son,
he's currently learning c++ language.

#define PRINT_TYPE_ATTRIBUTES(T)  \
    cout << "typeid(" << O_YELLOW(#T) << ").name(): " <<
O_RED(typeid(T).name()) <<endl \
    << "    demangling name: " << O_GREEN(abi::__cxa_demangle(typeid(T).name(),
0, 0, &status))<< endl \
    << "    sizeof(" << #T << "): " << O_BLUE(sizeof(T) << " bytes")<< endl \
    << "    sizeof( unsigned " << #T << "): " << O_BLUE(sizeof(unsigned T) << "
bytes")<< endl;

PRINT_TYPE_ATTRIBUTES(char);
PRINT_TYPE_ATTRIBUTES(int);
PRINT_TYPE_ATTRIBUTES(long);

these statements worked correctly;

my son puts uint8_t and uint64_t into the macro function. At first I thought he
would make a mistake ,but the compiler passed and the results are both 4. It's
confusing to us. I don't know how to explain to him at that time.

That's the whole story :)

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2023-07-26  5:11 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-25  6:49 [Bug c++/110798] New: The reusult of sizeof operator followed with an 'unsigned typedef-ed generic integer' type is alway 4 bytes(except char) 13958014620 at 139 dot com
2023-07-25  6:59 ` [Bug c++/110798] [12/13 Regression] " pinskia at gcc dot gnu.org
2023-07-25  7:02 ` [Bug c++/110798] [12 " pinskia at gcc dot gnu.org
2023-07-25  7:14 ` 13958014620 at 139 dot com
2023-07-25  9:59 ` rguenth at gcc dot gnu.org
2023-07-25 10:39 ` redi at gcc dot gnu.org
2023-07-25 10:50 ` redi at gcc dot gnu.org
2023-07-26  5:11 ` 13958014620 at 139 dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).