- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Structure alignment in 10.20 C (bundled)
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2001 12:15 PM
07-01-2001 12:15 PM
How can I do it?
Is there any online manual for the bundled compiler?
Thanks
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2001 03:10 PM
07-01-2001 03:10 PM
Re: Structure alignment in 10.20 C (bundled)
To my great surprise the 10.20 bundled compiler recogizes the same pragma as the Ansi C compiler.
Use
#pragma HP_ALIGN NOPADDING
Sample code:
int main()
{
struct
{
int x;
char y;
short z;
} zz;
printf("size = %d\n",sizeof(zz));
return(0);
}
produces 8 bytes without the pragma and 7 bytes
with the pragma. Just what you wanted. You know you are being a bad boy producing non-portable code.
Regards, Clay
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2001 04:58 PM
07-01-2001 04:58 PM
Re: Structure alignment in 10.20 C (bundled)
For now, I do not know if you are a king (your icon), but, for sure, you are a rabbit. I was not expecting any reply today (Sunday).
Your last remark "You know you are being a bad boy producing non-portable code" is interesting and bring a new question.
I have to decode a stream of network data that is based on byte-aligned (variable size) structures. Is there a better way rather than using pointers and "non-portable" structures?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-02-2001 06:55 AM
07-02-2001 06:55 AM
Re: Structure alignment in 10.20 C (bundled)
You need to look into xdr (External Data Representation); it's the foundation of rpc (Remote Procedure Calls). It allows one to represent data in a platform independent way and thus allows a procedure and data on Machine A to be executed and accessed on Machine B even if A is a Unix server and B is a PC under Windows. You could man xdr for details if you had a development compiler. You really need to get a devopment compiler for any serious work. If you choose not to use one of the HP compilers, you can download gcc from one of the HP-UX Porting Centers.
Regards, Clay
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2001 02:00 PM
07-03-2001 02:00 PM
Re: Structure alignment in 10.20 C (bundled)
To verify operation of the HP_ALIGN pragma, I did the following program:
------------------------------------------#pragma HP_ALIGN NOPADDING
typedef struct{
unsigned char a;
unsigned char b;
unsigned long c;
unsigned long d;
unsigned short e;
} PS;
main( argc, argv)
int argc;
char *argv[];
{
PS s;
printf ("Offsets in PS: %d, %d, %d, %d, %d, %d\n", &s.a - &s, &s.b - &s.a, &s.c - &s.a, &s.d - &s.a, &s.e - &s.a, sizeof( s));
if (&s.c > &s.a) printf( "a comes before c\n");
memcpy( s, "ABCDEFGHIJKLMN", sizeof(s));
printf( "%x, %x, %x, %x, %x\n", s.a, s.b, s.c, s.d, s.e);
}
-------------------------------------------
Results are:
Offsets in PS: 0, 1, 0, 1, 5, 12
a comes before c
41, 42, 43444546, 4748494a, 4b4c
The pointers in PA-RISC seem unusual: both the "a" and "c" members have the same offset.
According to K&R, subtracting pointers to members of the same structure should be permitted. However they do not state what the result should be. With PA_RISC, it seems that the result is not significant.
I was amazed to see that pointer comparison worked fine (&s.c is greater than &s.a while the difference is zero).
Can you bring some light on this
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2001 10:17 AM
07-05-2001 10:17 AM
Re: Structure alignment in 10.20 C (bundled)
printf ("Addrs in PS: %d, %d, %d, %d, %d, %d\n",
&s, &s.a, &s.b, &s.c, &s.d, &s.e);
printf ("Offsets in PS: %d, %d, %d, %d, %d, %d\n",
(int) (&s.a) - (int) (&s),
(int) (&s.b) - (int) (&s.a),
(int) (&s.c) - (int) (&s.a),
(int) (&s.d) - (int) (&s.a),
(int) (&s.e) - (int) (&s.a),
sizeof(s));
The output is:
Addrs in PS: 2063839952, 2063839952, 2063839953, 2063839954, 2063839958, 2063839962
Offsets in PS: 0, 1, 2, 6, 10, 12
a comes before c
41, 42, 43444546, 4748494a, 4b4c
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2001 10:34 AM
07-05-2001 10:34 AM
SolutionSorry I didn't notice your last posting until Gregory's answer. The results of pointer arithmetic can be quite confusing.
For example, using your struct PS example
let's declare an array struct PS xx[20];
let's also assume that the base address of xx
(i.e &(xx[0]) = 1000. Since these structs are 12 bytes xx[1] = 1012, xx[2] = 1024, ... .
Now let's declare 2 pointers:
struct PS *p0 = &(xx[0]),*p2 = &(xx[2]);
However, if doing pointer arithmetic
p2 - p0 does not equal 24 as you might expect but instead equals 2.
Hope this helps, Clay
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2001 06:33 PM
07-08-2001 06:33 PM
Re: Structure alignment in 10.20 C (bundled)
I'm ashamed. I's always essential to remember the basics.
The case is closed (at least for me) but other questions will follow in separate threads.