[haiku-development] Re: Reg. C++: Ignoring padding in structs

  • From: "Shubham Bhagat" <dmarc-noreply@xxxxxxxxxxxxx> (Redacted sender "shubhambhagat111" for DMARC)
  • To: <haiku-development@xxxxxxxxxxxxx>
  • Date: Thu, 25 Jun 2020 09:58:10 +0000 (UTC)

 Thank you for pointing the typedef difference in both the languages. Didn't 
know that :)Sorry I failed to mention that the examples don't compile. I've now 
used __attribute__((packed)) and it works.But having multiple __PACKED in the 
same file is causing conflicting declaration error while compiling.

    On Thursday, 25 June, 2020, 2:44:01 pm IST, Adrien Destugues 
<pulkomandy@xxxxxxxxxxxxx> wrote:  
 

Hi,

If I'm not wrong, to tightly pack structures we use "__PACKED"?

If not then what do we use?

I have typedef'ed my structures and I can't add __PACKED to any of them...

Struct looks like

typedef struct abcd{

/* content*/

} ABCD;

The following don't work:

1)
typedef struct abcd __PACKED{

} ABCD;

2)
struct abcd{

} __PACKED;
typedef struct abcd ABCD;

In C++ you don't need typedef.

struct abcd {
    ....
};

abcd someObject;

will work just fine. This is because "struct" and "class" are handled the same 
(and is different from C).

You did not explqin how your exqmple does "not work". Is the struct not packed? 
Does it fail compiling and if so, what's the error?

There is nothing in C++ about how to pack structures. In gcc you can use 
__attribute__((packed)) or if there are a lot of structures to pack defined in 
the same place you can use #pragma pack instead. I think __PACKED is defined 
somewhere in haiku headers.

-- 
Adrien.

  

Other related posts: