Array alignment on HPUX

Thread Tools Search this Thread
Top Forums Programming Array alignment on HPUX
# 1  
Old 11-12-2007
Array alignment on HPUX


I'm porting code from Windows to HP-UX 11.23, using gcc (I'm begining to suspect this might not be the best choice, but I thought it would be most compatible since we've already compiled on Linux with gcc).

I have in my code something similar to the folowing:
unsigned char *ch_arr = new (unsigned char)[20];
unsigned char ptr = ch_arr + 10;
unsigned int number = 100;

*((unsigned int *)ptr - 1) = number;

The last line gives a segmentation fault.
In fact, I have found that its enough to write the following in order to crash:
unsigned char *ch_arr = new (unsigned char)[20];
unsigned int number = 100;

ch_arr[2] = number;

And the same is true for any index in the array which does not divide by 4.
I assume this is an alignment problem. If I were writing new code I could work around it, but since I am porting code this is a problem. Apparently the code uses this type of code, treating char arrays as memory buffers for different types, in many places and it would be very difficult to find them all and fix them.

Is there any compilation flag that could change the alignment for me?
Any other ideas for a global fix for this?


# 2  
Old 11-12-2007
Excellent problem!

You will find yourself in a bit of a pickle unless you sort out the misaligned accesses.

If you use structures then the compiler will automatically insert the correct padding to provide the type alignment.

Here are a couple of useful macros

#define offsetof(x,y)	((long)((char *)&(((x *)0)->y)))
#define alignof(x)	(short)(long)(&((struct { char _x; x _d; } *)NULL)->_d)

# 3  
Old 11-13-2007
I am beginning to appreciate my pickled situation...

I'm intrigued about the macros. I understand that in 'alignof' x is a type, correct?
I tried it but my compiler says:
error: types may not be defined in casts

# 4  
Old 11-13-2007
Yes a type. It will work for a basic types and typedef'd types.

Which compiler are you using? I'm a bit concerned... Smilie

I've used HP's PA-RISC compiler, it's Itanium2, gcc and they are all happy with it.

I've found it to be highly portable, and I mean gcc, MIPSpro, HP's cc, Sun's cc, Visual C++, Tru64's cc.

Last edited by porter; 11-13-2007 at 03:20 AM..
# 5  
Old 11-14-2007
First of all the line of code I wrote is:
        fprintf( stderr, "alignof : %d\n", alignof(UINT) );

Where UINT is defined as 'unsigned int'. This gives me the compilation error I mentioned.
I'm using gcc 4.2.1 on HPUX on Itanium: ia64-hp-hpux11.23
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Help with awk alignment

Dear All, I am in the beginning stage of learning shell scripting and preparing shell script on my own now. I would like to get help from fellow mates here. As I am trying to take O/P with space included from I/P table. Kindly guide me to align given I/P table as Expected O/P. ... (5 Replies)
Discussion started by: Raja007
5 Replies

2. Shell Programming and Scripting

Row alignment

*1 flash read test(*do_test1*) PASS *2 xxxxxxxxxxx flash write test(*do_test2) FAIL ------>xxxxx *1 flash read test(*do_test1*) PASS *2 xxxxxxxxxxx flash write test(*do_test2) FAIL ------>xxxxx I want pass and Fail to be aligned if each line uses printf or echo to print, is... (5 Replies)
Discussion started by: yanglei_fage
5 Replies

3. HP-UX

pwage-hpux-T for Trusted HPUX servers

I'm sharing this in case anybody needs it. Modified from the original solaris pwage script. This modified hpux script will check /etc/password file on hpux trusted systems search /tcb and grep the required u_succhg field. Calculate days to expiry and notify users via email. original solaris... (2 Replies)
Discussion started by: sparcguy
2 Replies

4. HP-UX

HP-UX text alignment

HI all, I tried to edit my files using HP-UX but my output tends to not align when I add another character in the files to edit my files i used the command is as follow chmod +w filename vi filename Help, :eek: (1 Reply)
Discussion started by: jasonhpwong
1 Replies

5. Shell Programming and Scripting


Hi, I am having a file with with format. however, for longer xml, the xml code has been truncated like this. F1 |###################### |String1 |<XML><REQ><MSGTYPE>DBDIRECT</MSGTYPE><SYNC>0</SYNC><CLIENT>C11</CLIENT>NAME=MYNAME|JOB=MYJOB| | ... (3 Replies)
Discussion started by: shellwell
3 Replies

6. Shell Programming and Scripting

Need Script to Use CPUs on a HPUX server to simulate Workload Manager on HPUX.

I am running HPUX and using WLM (workload manager). I want to write a script to fork CPUs to basically take CPUs from other servers to show that the communication is working and CPU licensing is working. Basically, I want to build a script that will use up CPU on a server. Any ideas? (2 Replies)
Discussion started by: cpolikowsky
2 Replies

7. UNIX for Dummies Questions & Answers

VI paste out of alignment

We have a guy at work who is trying to copy and paste from one file to another using vi and highlighting the code to copy with a mouse. Source file: xyz 123 abc 999 zyx 321 cba 999 xyz 123 abc 999 But when he pastes it he gets (I put the underlines in to show... (4 Replies)
Discussion started by: dlam
4 Replies

8. Shell Programming and Scripting

alignment of variable

Dear Champs, i have a file let a.txt having value number text 00 123 012 145 456 ...etc i need number and text column vales should right align ??? how can i achive this ??? NOTE number is of max 3 char and text can take max 7 if any records are less than above lengths... (2 Replies)
Discussion started by: manas_ranjan
2 Replies

9. UNIX for Dummies Questions & Answers

Column Alignment

I copied a word file to my Unix directory, How do I line up my columns to the file I copied over? (3 Replies)
Discussion started by: nikncha
3 Replies
Login or Register to Ask a Question