LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-23-2011, 05:27 AM   #1
PinoyAko
LQ Newbie
 
Registered: May 2011
Posts: 28

Rep: Reputation: 0
When should I use pragma pack?


When should I use pragma pack?

I found this code on some source files while other sources do not have the pragma pack()
Code:
#pragma pack (1)
typedef struct _net_ip_header_t {
	uint8 hdr_len:4;		// header length
	uint8 version:4;		// version
	uint8 tos;				// type of service
	uint16 tot_len;			// total length
	uint16 id;				// identification
	uint16 flags_offset;	// fragment offset field
	uint8 ttl;				// time to live
	uint8 proto;			// protocol
	uint16 cksum;			// checksum
	ip_addr src_ip;			// source ip address
	ip_addr dst_ip;			// destination ip address
} net_ip_header_t;
#pragma pack ()
 
Old 05-23-2011, 05:31 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
The use of pragma pack is to ensure that the data structure is byte-aligned versus word-aligned. For example, the following struct takes up 8 bytes (perhaps not on all systems), but on typical Linux machines:
Code:
struct Foo
{
   int  val;   /* takes up 4 bytes */
   char ch;    /* takes up 1 byte  */
};

/* Yet struct Foo takes up 8 bytes */
With the pragma pack directive, the struct above will only take up 5-bytes.

Last edited by dwhitney67; 05-23-2011 at 05:32 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to use #pragma twwwater Programming 6 01-03-2010 04:58 AM
How to pack data into a 7z recursive + having autounpack EXE pack for windows? frenchn00b Linux - General 5 05-02-2009 01:59 PM
#pragma vedanu Programming 2 02-21-2007 04:15 AM
LXer: Sun Announces the Latest in Open Source Tools with the Availability of NetBeans Visual Web Pack and NetBeans C/C++ Development Pack LXer Syndicated Linux News 1 12-31-2006 07:37 AM
#pragma pack directive rajesh_b Programming 2 09-25-2006 04:05 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration