Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Unix C Variable Alignment Question

JohnSJJohnSJ Member Posts: 1
I am trying to process binary data in a char buffer received from a GPIB instrument. The layout of the data is buffer[0]..buffer[1] reserved, buffer[2]..buffer[5] unsigned int, and then unsigned ints after that every 4 bytes.

What I'd like to do is something like:

char buffer[1024]; // Filed as per above description
unsigned int i1;
unsigned int i2;

i1 = *(unsigned int *) &buffer[2];
i2 = *(unsigned int *) &buffer[6];
etc;

but on our Sun Unix system this causes a SIGBUS error. (works fine on a PC though). I chacked further and found that it was the assignment that caused the fault, not the pointer cast.

Because of the SIGBUS I am forced to do:

char buffer[1024]; // Filed as per above description
char uibif[4];
unsigned int i1;
unsigned int i2;

memcpy(uibuf, &buffer[2], 4);
i1 = *(unsigned int *) &uibuf[0];
memcpy(uibuf, &buffer[6], 4);
i2 = *(unsigned int *) &uibuf[0];

Can anyone who knows Unix well explain to me why the first case causes a SIGBUS error? Thanks,

John-SJ

Comments

  • Andre YoungAndre Young USAMember Posts: 0

    ______ ( http://forcoder.org ) free ebooks and video tutorials about < Perl Objective-C R C Ruby Go PL/SQL Java Scratch Visual Basic JavaScript PHP C# C++ MATLAB Swift Delphi Python Assembly Visual Basic .NET D Lisp Prolog Transact-SQL Awk Dart Alice Crystal Ada FoxPro SAS VBScript Hack ABAP Kotlin Logo Lua ML Bash F# Julia Scheme LabVIEW COBOL Rust Clojure Apex Scala Erlang Fortran /> ______

Sign In or Register to comment.