Alignment of char array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rorni
    New Member
    • Oct 2007
    • 7

    Alignment of char array

    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:
    Code:
    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:
    Code:
    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?

    Thanks!

    RO
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #2
    Originally posted by rorni
    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:
    Code:
    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:
    Code:
    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?

    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.
    Last edited by RedSon; Nov 12 '07, 10:02 PM.

    Comment

    • oler1s
      Recognized Expert Contributor
      • Aug 2007
      • 671

      #3
      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.

      Comment

      • RedSon
        Recognized Expert Expert
        • Jan 2007
        • 4980

        #4
        Oh I see, you are sneaky.

        Comment

        • rorni
          New Member
          • Oct 2007
          • 7

          #5
          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

          Comment

          Working...