Errors, bugs, questions - page 2503

 
Alexey Viktorov:

So what's the bug?

You declare an array of two elements with two bytes each and get 4 bytes as it should be. Where is the bug? What is it?

It's this.

You align it to 4 and 2 elements will occupy 2*8 bytes.

Sorry, but if you write that, you don't understand the subject.

Analogous to the crosses:

#include <iostream>
struct alignas(4) A
{
  short j;
};

int main()
{
   A q[2];
   std::cout << sizeof(q) << std::endl; // 8
   return 0;
}

The mistake probably isn't here?

 
Vict:

I'm sorry, but if you write that, you don't understand the subject.

No, you don't seem to understand.

//g++  5.4.0

#include <iostream>
#pragma  pack (push, 4)
struct A
{
  short j;
};
#pragma  pack (pop)

int main()
{
   A q[2];
   std::cout << sizeof(q) << std::endl; // 4
   return 0;
}
 
TheXpert:

No, you don't seem to understand.

The #pragma pack from the smallmicrosoft stuff is a very peculiar thing (the way it works now surprises me, to be honest). Not surprisingly, the standard alignas works differently.

Here's more:

#include <iostream>
struct A
{
  short j;
} __attribute__((aligned(4)));

int main()
{
   A q[2];
   std::cout << sizeof(q) << std::endl; // 8
   return 0;
}
 
Vict:

The #pragma pack of the small softwares is a very peculiar thing (the way it works is surprising to me now, to be honest). No wonder that alignas works differently in the standard.

come on )

#include <iostream>
struct A
{
  short j;
} alignas(4) ;

int main()
{
   A q[2];
   std::cout << sizeof(q) << std::endl; // 4
   return 0;
}

#pragma pack works fine on gcc as well, and what you wrote is just an internal gcc attribute that works differently than aligned.

 
TheXpert:

Come on, already.)

#pragma pack works fine on gcc as well, and what you've written is just an internal gcc attribute that works differently than aligned.

So what does this example show? That you haven't figured out the use of alignas? Even the compiler swears at the code:

1.cc:7:3: warning: attribute ignored in declaration of 'struct A' [-Wattributes]
    7 | } alignas(4) ;
      |   ^~~~~~~
1.cc:7:3: note: attribute for 'struct A' must follow the 'struct' keyword

The #pragma pack is an odd little softie creation that gcc only supports for compatibility.

 
Vict:

So what does this example show? That you haven't figured out how to use alignas?

I agree, I messed up.

Here's the right code.

#include <iostream>
struct alignas(4) A
{
  short j;
}  ;

int main()
{
   A q[2];
   std::cout << sizeof(q) << std::endl; // 8
   return 0;
}

so alignas is not analogous to packaging.

because in the same gcc (so that without smallsoft) the default multiple of packing is 8

because:

#include <iostream>
struct A
{
  short j;
};

struct B
{
  short j;
  long long k;
};

int main()
{
   A q[2];
   B r[2];
   std::cout << sizeof(q) << " " << sizeof(r) << std::endl; // 4 32
   return 0;
}

but with alignas(8)

#include <iostream>
struct alignas(8) A
{
  short j;
};

struct alignas(8) B
{
  short j;
  long long k;
};

int main()
{
   A q[2];
   B r[2];
   std::cout << sizeof(q) << " " << sizeof(r) << std::endl; // 16 32
   return 0;
}
 
And note that this is default behaviour, not for compatibility with softwares
 
TheXpert:
And note that this is default behavior, not for smallmicrosoft compatibility

I don't get the idea. Everything is fine in the example, I don't see a problem. Default - structure alignment == take the strictest requirement-alignment among members.

struct {char;} == 1, struct {char;short} == 2, struct {char;int} == 4, etc. It's like that everywhere. And long long == eight, so alignas has no effect.

 
Vict:

I don't get the idea. Everything is fine in the example, I don't see the problem.

Yes, you're right, the example is no indicator.

The bottom line is that mql pack works as #pragma pack and not alignas.

 
People, share some information. What are you using alignment for?