Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 311 Vote(s) - 3.56 Average
  • 1
  • 2
  • 3
  • 4
  • 5
#pragma pack() with push and pop vs unpack

#1
I have this sample program below

#include <stdio.h>
#include <stdlib.h>

#pragma pack(push)
#pragma pack(1)
typedef struct{
char a;
int b;
char c;
}st_a;
#pragma pack(pop)

typedef struct{
char a;
int b;
char c;
}st_b;


int main()
{
printf("size of struct a %zd \n",sizeof(st_a));
printf("size of struct b %zd \n",sizeof(st_b));

return 0;
}

Output of the above program is

size of struct a 6
size of struct b 12


Now if I change the struct declaration as below:


#pragma pack(1)
typedef struct{
char a;
int b;
char c;
}st_a;
#pragma unpack()

Output of the program is

size of struct a 6
size of struct b 6

Why is this difference in the behaviour? My understanding was that both structure declarations are doing the same thing.

I am running this on my MBP.

$gcc --version

Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn)
Target: x86_64-apple-darwin14.4.0
Thread model: posix

Reply

#2
Your compiler knows nothing about `unpack()` pragma, and just ignores it, so the same packing rules are applied to both structures.

`MSVC` compiler will issue a warning about unknown `#pragma` directives on first warning level.

Both `GCC` and `Clang` keep silence by default. You need to use `-Wunknown-pragmas` flag.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through