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:
  • 550 Vote(s) - 3.44 Average
  • 1
  • 2
  • 3
  • 4
  • 5
#pragma pack effect

#1
I was wondering if someone could explain to me what the `#pragma pack` preprocessor statement does, and more importantly, why one would want to use it.

I checked out the [MSDN page][1], which offered some insight, but I was hoping to hear more from people with experience. I've seen it in code before, though I can't seem to find where anymore.

[1]:

[To see links please register here]

Reply

#2
You'd likely only want to use this if you were coding to some hardware (e.g. a memory mapped device) which had strict requirements for register ordering and alignment.

However, this looks like a pretty blunt tool to achieve that end. A better approach would be to code a mini-driver in assembler and give it a C calling interface rather than fumbling around with this pragma.
Reply

#3
It tells the compiler the boundary to align objects in a structure to. For example, if I have something like:

struct foo {
char a;
int b;
};

With a typical 32-bit machine, you'd normally "want" to have 3 bytes of padding between `a` and `b` so that `b` will land at a 4-byte boundary to maximize its access speed (and that's what will typically happen by default).

If, however, you have to match an externally defined structure you want to ensure the compiler lays out your structure exactly according to that external definition. In this case, you can give the compiler a `#pragma pack(1)` to tell it *not* to insert any padding between members -- if the definition of the structure includes padding between members, you insert it explicitly (e.g., typically with members named `unusedN` or `ignoreN`, or something on that order).
Reply

#4
Data elements (e.g. members of classes and structs) are typically aligned on WORD or DWORD boundaries for current generation processors in order to improve access times. Retrieving a DWORD at an address which isn't divisible by 4 requires at least one extra CPU cycle on a 32 bit processor. So, if you have e.g. three char members `char a, b, c;`, they actually tend to take 6 or 12 bytes of storage.

`#pragma` allows you to override this to achieve more efficient space usage, at the expense of access speed, or for consistency of stored data between different compiler targets. I had a lot of fun with this transitioning from 16 bit to 32 bit code; I expect porting to 64 bit code will cause the same kinds of headaches for some code.
Reply

#5
A compiler *may* place structure members on particular byte boundaries for reasons of performance on a particular architecture. This may leave unused padding between members. Structure packing forces members to be contiguous.

This may be important for example if you require a structure to conform to a particular file or communications format where the data you need the data to be at specific positions within a sequence. However such usage does not deal with endian-ness issues, so although used, it may not be portable.

It may also to exactly overlay the internal register structure of some I/O device such as a UART or USB controller for example, in order that register access be through a structure rather than direct addresses.
Reply

#6
Compiler could align members in structures to achieve maximum performance on the certain platform. `#pragma pack` directive allows you to control that alignment. Usually you should leave it by default for optimum performance. If you need to pass a structure to the remote machine you generally will use `#pragma pack 1` to exclude any unwanted alignment.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

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