473,387 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Alignment of char array

7
Hi,

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:
Expand|Select|Wrap|Line Numbers
  1. unsigned char *ch_arr = new (unsigned char)[20];
  2. unsigned char ptr = ch_arr + 10;
  3. unsigned int number = 100;
  4.  
  5. *((unsigned int *)ptr - 1) = number;
  6.  
The last line gives a segmentation fault.
In fact, I have found that its enough to write the following in order to crash:
Expand|Select|Wrap|Line Numbers
  1. unsigned char *ch_arr = new (unsigned char)[20];
  2. unsigned int number = 100;
  3.  
  4. ch_arr[2] = number;
  5.  
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?

Thanks!

RO
Nov 12 '07 #1
4 6461
RedSon
5,000 Expert 4TB
Hi,

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:
Expand|Select|Wrap|Line Numbers
  1. unsigned char *ch_arr = new (unsigned char)[20];
  2. unsigned char ptr = ch_arr + 10;
  3. unsigned int number = 100;
  4.  
  5. *((unsigned int *)ptr - 1) = number;
  6.  
The last line gives a segmentation fault.
In fact, I have found that its enough to write the following in order to crash:
Expand|Select|Wrap|Line Numbers
  1. unsigned char *ch_arr = new (unsigned char)[20];
  2. unsigned int number = 100;
  3.  
  4. ch_arr[2] = number;
  5.  
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?

Thanks!

RO
Part of the problem is you are trying to stuff an int into a char array. Ints are 4 times larger then chars. You will have to do some data marshaling because one int will take up char[n] through char [n+3].

Alternatively you can do some bit shifting to put both the lo bytes and hi bytes of each word of the int into the char array. You might need to create a IntToCharArray method, or see if there is a library that you can uset hat does it. I don't think there is an ANSI method that does this.
Nov 12 '07 #2
oler1s
671 Expert 512MB
You have already posted your question at http://forums.devshed.com/c-programm...ay-488818.html. The members there have responded, so I won't bother pursuing this thread, and neither should anyone else here.

As you were informed, the code examples you posted (and your expectations of them) are troubling. Your first example involves you trying to take a char pointer, assign it to a char, and then assign an integer in some operations on that char. With the loss of precision, and the mistake in assignment vs. memory copying, the code won't work as you think it will.

And the second example is puzzling. Well, if you think it should copy memory that won't happen. When you do a = b, you don't copy memory. The compiler sets code that tries to assign the value b to a, perhaps resulting in a loss of precision. This was all discussed in the other thread on the other forum you created, so I'll leave it at here. You've got bad code, so it's more than just porting here. You need to fix it.
Nov 12 '07 #3
RedSon
5,000 Expert 4TB
Oh I see, you are sneaky.
Nov 12 '07 #4
rorni
7
Thanks for your replies.

First of all, no sneakiness intended. I simply assumed that different people particiapte in different forums and therefore I might get different insights this way. I'm really sorry if this is improper forum usage.

I also want to apologize for my hasty post, as I did on the other forum you pointed to. I'll copy my apology to here even though it is cross posting, for the completion of the thread:
I was a bit hasty writing the post yesterday from home and from memory without the code in front of me. sizablegrin must have noticed this - first of all I did indeed forget the asterisk in the definition of the array, but it is there in the original code.
Second, and more significantly, it is indeed not a segmentation fault but a bus error!
And finally, as sizablegrin and clifford correctly guessed, the second example does not really crash - I was doing too many tests and got them confused.
I apologize for making you try to understand nonexisting problems.
But the first problem is real.
In any case, thaks for your time and help!

RO
Nov 13 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: J. Campbell | last post by:
I posted a question some time back about accessing a char array as an array of words. In order not to overrun the char array, I padded it with enough 0x00 bytes to ensure that when accessed as...
36
by: Bhalchandra Thatte | last post by:
I am allocating a block of memory using malloc. I want to use it to store a "header" structure followed by structs in my application. How to calculate the alignment without making any assumption...
67
by: S.Tobias | last post by:
I would like to check if I understand the following excerpt correctly: 6.2.5#26 (Types): All pointers to structure types shall have the same representation and alignment requirements as each...
4
by: Romeo Colacitti | last post by:
I have a need to make a custom quasi-memory allocator, and I remembered a simple ons in K&R2. Looking at the code for it now, I think I notice a "fault" in the design, and I was wondering if...
13
by: aegis | last post by:
The following was mentioned by Eric Sosman from http://groups.google.com/group/comp.lang.c/msg/b696b28f59b9dac4?dmode=source "The alignment requirement for any type T must be a divisor of...
3
by: Bill Pursell | last post by:
I have a program that does most of its work traversing a bunch of lists. The lists contain a void *, and I spent some time today replacing the void *'s with a copy of the data at the end of the...
10
by: haomiao | last post by:
I want to implement a common list that can cantain any type of data, so I declare the list as (briefly) --------------------------------------- struct list { int data_size; int node_num;...
10
by: Lionel B | last post by:
Greetings, I have some code that is to read unformatted data from disc and interpret it as blocks of unsigned integers. In an attempt to achieve efficiency (it is pretty essential for my...
6
by: Francois Grieu | last post by:
Hello, I'm asking myself all kind of questions on allocating an array of struct with proper alignment. Is the following code oorrect ? I'm most interested by the statement t =...
8
by: Stephen Horne | last post by:
I understand that the next C++ standard will have features to handle the alignment of data types. This is good, but a bit late for me! I've been using some template trickery to handle alignment...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.