public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/106873] New: unsigned short skips byte when used for memory mapping depending on position in structure
@ 2022-09-07 16:18 info at kemalakcam dot com
  2022-09-07 16:26 ` [Bug c++/106873] " pinskia at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: info at kemalakcam dot com @ 2022-09-07 16:18 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 106873
           Summary: unsigned short skips byte when used for memory mapping
                    depending on position in structure
           Product: gcc
           Version: 12.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: info at kemalakcam dot com
  Target Milestone: ---

Please move to the correct component in reporting menu as this problem is not
necessarily c++

// ============================================================================
// File: ka_example_pointer.cpp
// Desc: Software programming example of pointers
// Date: 2020/12/25 Ali Kemal Akçam - ka_example_pointer.cpp created.
// ============================================================================
// source code shortened to report bug.
// memory mapping with unsigned short does not show intented result.
// both 'unsigned short' and 'unsigned short int' skips one byte depending on
position
// tested with
// gcc --version   
// (Debian 4.9.2-10) 4.9.2
// (Debian 8.3.0-6) 8.3.0
// 12.1.0
// gcc version 4.9.2 compiled as
// gcc -lstdc++ -std=c++11 ka_example_pointer_very_brief.cpp -o
ka_example_pointer_very_brief.o
// gcc version 12.1.0 compiled as
// gcc -lstdc++ ka_example_pointer_very_brief.cpp -o
ka_example_pointer_very_brief.o

#include <stdio.h>

typedef struct 
{ 
 unsigned char  a;     //  1 byte
 unsigned char  b;     //  1 byte
 unsigned char  c;     //  1 byte
 unsigned char  d;     //  1 byte
 unsigned char  e;     //  1 byte
 unsigned char  f;     //  1 byte
 unsigned char  g;     //  1 byte
 unsigned char  h;     //  1 byte
} tsMMP_bytes;         //  8 bytes

typedef struct 
{ 
 unsigned char  a;     //  1 byte
 unsigned short b;     //  2 bytes - skips one byte here
 unsigned char  c;     //  1 byte
 unsigned char  d;     //  1 byte
 unsigned short e;     //  2 bytes - does not skip one byte here
 unsigned char  f;     //  1 byte
} tsMMP_mixed;         //  8 bytes 

typedef struct 
{ 
 unsigned char  a;     //  1 byte
 unsigned char  b;     //  1 byte
 unsigned short c;     //  2 bytes - does not skip one byte here
 unsigned char  d;     //  1 byte
 unsigned short e;     //  2 bytes - skips one byte here
 unsigned char  f;     //  1 byte
} tsMMP_mixed2;        //  8 bytes 

typedef struct 
{ 
 unsigned char  a;     //  1 byte
 unsigned short b;     //  2 bytes - skips one byte here
 unsigned char  c;     //  1 byte
 unsigned short d;     //  2 bytes - skips one byte here
 unsigned char  e;     //  1 byte
 unsigned char  f;     //  1 byte
} tsMMP_mixed3;        //  8 bytes 

typedef struct
{
 unsigned char l;
 char h; 
} tsWord;  // signed word DOES NOT WORK!

typedef struct
{
 unsigned char  a;          //  1 byte
 tsWord         b;          //  2 bytes - works properly
 unsigned char  c;          //  1 byte
 unsigned char  d;          //  1 byte
 tsWord         e;          //  2 bytes - works properly
 unsigned char  f;          //  1 byte
} tsMMP_mixed_struct_word;  //  8 bytes - correctly maps to another linear
memory 

// 20201225T0311 : Kemal Akcam : Mixed typedef struct works perfectly while
using as memory stencil for memory mapping.
typedef struct
{
 unsigned char a;
 unsigned char b; 
} tsShort;

typedef struct
{
 unsigned char  a;     //  1 byte
 tsShort        b;     //  2 bytes - works properly
 unsigned char  c;     //  1 byte
 unsigned char  d;     //  1 byte
 tsShort        e;     //  2 bytes - works properly
 unsigned char  f;     //  1 byte
} tsMMP_mixed_struct;  //  8 bytes - correctly maps to another linear memory 

class c
{
public:
 c(){};
 ~c(){};

 unsigned char m[16] = { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
                         0x18, 0x19, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25 };

 void printContent(void);
};

void c::printContent(void)
{
 int i;
 printf("Memory content             ");
 for(i=0; i<16; i++) printf("%X ", m[i]);
 printf("\n"); 

 printf("tsMMP_bytes                ");
 tsMMP_bytes *pB;
 pB = (tsMMP_bytes *)&m;
 printf("%X %X %X %X %X %X %X %X ", 
         pB->a,
         pB->b,
         pB->c,
         pB->d,
         pB->e,
         pB->f,
         pB->g,
         pB->h);
 printf("\n"); 

 {
 printf("tsMMP_mixed                ");
 tsMMP_mixed *pM;
 pM = (tsMMP_mixed *)&m;
 printf("%X %X %X %X %X %X   <--- 11 is NOT there", 
         pM->a,
         pM->b,
         pM->c,
         pM->d,
         pM->e,
         pM->f);
 printf("\n"); 
 }
 {
 printf("tsMMP_mixed2               ");
 tsMMP_mixed2 *pM;
 pM = (tsMMP_mixed2 *)&m;
 printf("%X %X %X %X %X %X   <--- 15 is NOT there", 
         pM->a,
         pM->b,
         pM->c,
         pM->d,
         pM->e,
         pM->f);
 printf("\n"); 
 }
 { 
 printf("tsMMP_mixed3               ");
 tsMMP_mixed3 *pM;
 pM = (tsMMP_mixed3 *)&m;
 printf("%X %X %X %X %X %X   <--- 11 and 15 is NOT there", 
         pM->a,
         pM->b,
         pM->c,
         pM->d,
         pM->e,
         pM->f);
 printf("\n"); 
 }

 printf("tsMMP_mixed_struct         ");
 tsMMP_mixed_struct *pMS;
 pMS = (tsMMP_mixed_struct *)&m;

 printf("%X %X %X %X %X %X   <--- 11 and 15 is there", 
         pMS->a,
         pMS->b,
         pMS->c,
         pMS->d,
         pMS->e,
         pMS->f);
 printf("\n"); 

 printf("tsMMP_mixed_struct_word    ");
 tsMMP_mixed_struct_word *pMWord;
 pMWord = (tsMMP_mixed_struct_word *)&m;
 printf("%X %X %X %X %X %X   <--- 11 and 15 is there",  
         pMWord->a,
         pMWord->b,
         pMWord->c,
         pMWord->d,
         pMWord->e,
         pMWord->f);
 printf("\n"); 

 printf("tsMMP_mixed_struct_word    ");
  short int *b = (short int *)&(pMWord->b);
  short int *e = (short int *)&(pMWord->e);
  printf(" corr b e %04X %04X ", *b, *e);
 printf("\n"); 

 printf("tsMMP_mixed_struct_word    ");
  printf("%X %X %X %X %X %X %X %X ", 
         pMWord->a,
         pMWord->b.h,
         pMWord->b.l,
         pMWord->c,
         pMWord->d,
         pMWord->e.h,
         pMWord->e.l,
         pMWord->f);
  printf("\n");
}

int main(int argc, char **argv) 
{
 c *n = new c();
 n->printContent();
 n->~c();

 return 0;
}

/*
 Output:
ka@localhost:~/projects/ka_projects/pointer_test$ gcc -lstdc++
ka_example_pointer_very_brief.cpp  -o ka_example_pointer_very_brief.o
ka@localhost:~/projects/ka_projects/pointer_test$
./ka_example_pointer_very_brief.o 
Memory content             10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 
tsMMP_bytes                10 11 12 13 14 15 16 17 
tsMMP_mixed                10 1312 14 15 1716 18   <--- 11 is NOT there
tsMMP_mixed2               10 11 1312 14 1716 18   <--- 15 is NOT there
tsMMP_mixed3               10 1312 14 1716 18 19   <--- 11 and 15 is NOT there
tsMMP_mixed_struct         10 1211 13 14 1615 17   <--- 11 and 15 is there
tsMMP_mixed_struct_word    10 1211 13 14 1615 17   <--- 11 and 15 is there
tsMMP_mixed_struct_word     corr b e 1211 1615 
tsMMP_mixed_struct_word    10 12 11 13 14 16 15 17 
*/

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

* [Bug c++/106873] unsigned short skips byte when used for memory mapping depending on position in structure
  2022-09-07 16:18 [Bug libstdc++/106873] New: unsigned short skips byte when used for memory mapping depending on position in structure info at kemalakcam dot com
@ 2022-09-07 16:26 ` pinskia at gcc dot gnu.org
  2022-09-07 16:28 ` pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-09-07 16:26 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |INVALID
             Status|UNCONFIRMED                 |RESOLVED

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I think you need to use bitfields to get this exact layout or use the attribute
packed.

With -Wpadded I get the following warnings:
<source>:19:17: warning: padding struct to align '<unnamed struct>::b'
[-Wpadded]
   19 |  unsigned short b;     //  2 bytes - skips one byte here
      |                 ^
<source>:17:1: warning: padding struct size to alignment boundary [-Wpadded]
   17 | {
      | ^
<source>:32:17: warning: padding struct to align '<unnamed struct>::e'
[-Wpadded]
   32 |  unsigned short e;     //  2 bytes - skips one byte here
      |                 ^
<source>:27:1: warning: padding struct size to alignment boundary [-Wpadded]
   27 | {
      | ^
<source>:39:17: warning: padding struct to align '<unnamed struct>::b'
[-Wpadded]
   39 |  unsigned short b;     //  2 bytes - skips one byte here
      |                 ^
<source>:41:17: warning: padding struct to align '<unnamed struct>::d'
[-Wpadded]
   41 |  unsigned short d;     //  2 bytes - skips one byte here
      |                 ^

Which I think is what you are missing is that the ABI is layed out this way.

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

* [Bug c++/106873] unsigned short skips byte when used for memory mapping depending on position in structure
  2022-09-07 16:18 [Bug libstdc++/106873] New: unsigned short skips byte when used for memory mapping depending on position in structure info at kemalakcam dot com
  2022-09-07 16:26 ` [Bug c++/106873] " pinskia at gcc dot gnu.org
@ 2022-09-07 16:28 ` pinskia at gcc dot gnu.org
  2022-09-07 16:35 ` info at kemalakcam dot com
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-09-07 16:28 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #1)
> Which I think is what you are missing is that the ABI is layed out this way.

The reason why it is layout this way is for alignment reasons. In many ABIs,
the alignment of short is 2 bytes; the natural alignment.

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

* [Bug c++/106873] unsigned short skips byte when used for memory mapping depending on position in structure
  2022-09-07 16:18 [Bug libstdc++/106873] New: unsigned short skips byte when used for memory mapping depending on position in structure info at kemalakcam dot com
  2022-09-07 16:26 ` [Bug c++/106873] " pinskia at gcc dot gnu.org
  2022-09-07 16:28 ` pinskia at gcc dot gnu.org
@ 2022-09-07 16:35 ` info at kemalakcam dot com
  2022-09-07 16:45 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: info at kemalakcam dot com @ 2022-09-07 16:35 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Kemal Akcam <info at kemalakcam dot com> ---
I did not report as if unsigned short was different than 2 bytes.

Please check the output and explain 
1. why output values changes?
2. why it reads/advances 10 bytes instead of 8 bytes?

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

* [Bug c++/106873] unsigned short skips byte when used for memory mapping depending on position in structure
  2022-09-07 16:18 [Bug libstdc++/106873] New: unsigned short skips byte when used for memory mapping depending on position in structure info at kemalakcam dot com
                   ` (2 preceding siblings ...)
  2022-09-07 16:35 ` info at kemalakcam dot com
@ 2022-09-07 16:45 ` pinskia at gcc dot gnu.org
  2022-09-07 17:01 ` redi at gcc dot gnu.org
  2022-09-12  3:16 ` info at kemalakcam dot com
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-09-07 16:45 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Kemal Akcam from comment #3)
> I did not report as if unsigned short was different than 2 bytes.
> 
> Please check the output and explain 
> 1. why output values changes?
> 2. why it reads/advances 10 bytes instead of 8 bytes?

Again you misunderstand everything. The ABI has a requirement for alignment of
some fields and that is what is called padding.
This is why I said you get a warning with -Wpadding .
If you want to remove this padding, you either use a bitfield or you use the
attribute packed.

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

* [Bug c++/106873] unsigned short skips byte when used for memory mapping depending on position in structure
  2022-09-07 16:18 [Bug libstdc++/106873] New: unsigned short skips byte when used for memory mapping depending on position in structure info at kemalakcam dot com
                   ` (3 preceding siblings ...)
  2022-09-07 16:45 ` pinskia at gcc dot gnu.org
@ 2022-09-07 17:01 ` redi at gcc dot gnu.org
  2022-09-12  3:16 ` info at kemalakcam dot com
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2022-09-07 17:01 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Read https://en.cppreference.com/w/c/language/object#Alignment (for C) and
https://en.cppreference.com/w/cpp/language/object#Alignment (for C++).

Instead of filing bugs like this and PR 106869, please find somewhere more
suitable to learn how C++ works. Both your "bugs" would be appropriate as
questions on StackOverflow (in fact they have probably already been answered
there, and you could find the answers by searching). "I find GCC's behaviour
surprising" is not a bug.

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

* [Bug c++/106873] unsigned short skips byte when used for memory mapping depending on position in structure
  2022-09-07 16:18 [Bug libstdc++/106873] New: unsigned short skips byte when used for memory mapping depending on position in structure info at kemalakcam dot com
                   ` (4 preceding siblings ...)
  2022-09-07 17:01 ` redi at gcc dot gnu.org
@ 2022-09-12  3:16 ` info at kemalakcam dot com
  5 siblings, 0 replies; 7+ messages in thread
From: info at kemalakcam dot com @ 2022-09-12  3:16 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Kemal Akcam <info at kemalakcam dot com> ---
(In reply to Jonathan Wakely from comment #5)
> Read https://en.cppreference.com/w/c/language/object#Alignment (for C) and
> https://en.cppreference.com/w/cpp/language/object#Alignment (for C++).
> 
> Instead of filing bugs like this and PR 106869, please find somewhere more
> suitable to learn how C++ works. Both your "bugs" would be appropriate as
> questions on StackOverflow (in fact they have probably already been answered
> there, and you could find the answers by searching). "I find GCC's behaviour
> surprising" is not a bug.

I like writing a piece of code to analyse software behaviour and sometimes it
is not the software that I'm analysing.
I thank both of you giving quick insight with your responses.
I still like GCC thou.
Best regards.

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

end of thread, other threads:[~2022-09-12  3:16 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-07 16:18 [Bug libstdc++/106873] New: unsigned short skips byte when used for memory mapping depending on position in structure info at kemalakcam dot com
2022-09-07 16:26 ` [Bug c++/106873] " pinskia at gcc dot gnu.org
2022-09-07 16:28 ` pinskia at gcc dot gnu.org
2022-09-07 16:35 ` info at kemalakcam dot com
2022-09-07 16:45 ` pinskia at gcc dot gnu.org
2022-09-07 17:01 ` redi at gcc dot gnu.org
2022-09-12  3:16 ` info at kemalakcam 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).