1751695 Members
5110 Online
108781 Solutions
New Discussion юеВ

Bus Error in assignment.

 
Arun  Grover
New Member

Bus Error in assignment.

I've faced a strange error with aCC C++ compiler.

There is a piece of code in which when i try to declare and define an integer (uint32_t) it gives me a BUS ERROR.
for example:
uint32_t id = structure.int_variable;

But when i do the same thing by declaring and defining separately in the following manner there is no error.
uint32_t id = 0;
id = structure.int_variable;

I just want to how how does the compiler interpret these statements.
7 REPLIES 7
Steven Schweda
Honored Contributor

Re: Bus Error in assignment.

Have you considered the advantages of posting
a real failing test case, instead of
fragments filled with mysteries like
"structure"?

By the time you explain the problem well
enough that some non-psychic person can see
what you're actually doing, the problem may
become clear to you, too.

> [...] a strange error with aCC C++ compiler.

It's a run-time error, not a compile-time
error, right? Showing actual commands with
actual output can be more helpful than vague
descriptions, too.

> I just want [...]

I'm sure that the compiler can emit the
actual machine code it generates in a form
slightly easier to read than a ".o" file, but
I doubt that that would help you.
Dennis Handly
Acclaimed Contributor

Re: Bus Error in assignment

>I just want to how how does the compiler interpret these statements.

The first is an initialization. The second is an assignment. It they are builtin types, they should do the same thing.

Are you in a thread? A thread stack overflow?
I assume that structure is a static, global, parm or local object? If it is a reference, that could have a bad pointer value.
Can you use gdb to print id and structure?

>Steven: I doubt that that would help you.

It would help me. :-)

What version is your aC++ compiler? What OS version and hardware model?

Arun  Grover
New Member

Re: Bus Error in assignment.

Ok first of all, i'm extremely sorry to provide with such limited amount of information.
I actually fured out what the problem was.

it starts like this:
in my header file i've declared 2 structures like this:

#pragma pack 2
struct A
{
uint16_t i;
uint32_t j;
};
struct B
{
uint32_t length;
uint32_t id;
uint8_t buf[16];
};
#pragma pack

Now when my function looks like this
void func()
{
A a;
/***** Do Something *****/
B b;
/****** Do Something *****/
int rec_id = b.id; // Line giving SIGBUS
}

Now as far as i'm able to interpret the problem is beacuse of the pragma.
When i declare both the structures with pragma pack 2 i get sizeof(A) as 6 and sizeof(B) as 24.
Now when make their variables from function stack the memory allocated to A is 6 bytes and while making a variable of B compiler starts using the memory address which is just next to A.
Which means B will get 2 bytes from one word rest 20 bytes in next 5 words and next 2 bytes from another word. This thing happens beacuse i've set a "pragma pack 2" for B as well, which is actually not required because B is never going to have any padding.

Thus i just modified the header file to this
#pragma pack 2
struct A
{
uint16_t i;
uint32_t j;
};
#pragma pack
struct B
{
uint32_t length;
uint32_t id;
uint8_t buf[16];
};

n it works fine now. SIGBUS probably was coming because of misalignment.

But the thing is OLD compilers never gave this kinda problem. So is it a compiler bug or is it that this new compiler has added intelligence in it.

I'm running this code on a PA RISC machine and compiled with aCC compiler version
"aCC: HP C/aC++ B3910B A.06.21 "

Sorry again that i provided with limited amount of info. This is also just a sample code written above i can not provide with actual snippet.

Dennis Handly
Acclaimed Contributor

Re: Bus Error in assignment

>in my header file I've declared 2 structures like this:

There are N things terribly wrong here.
1) This shouldn't abort.
2) Your compiler version doesn't match your architecture.
3) You can't possibly have aCC6's A.06.21, it is still under development.

>while making a variable of B compiler starts using the memory address which is just next to A.

While it could do that, it doesn't have to.

>But the thing is OLD compilers never gave this kinda problem.

What was the old version?

>I'm running this code on a PA-RISC machine and compiled with aCC

The latest aCC3 version is A.03.85.

Dennis Handly
Acclaimed Contributor

Re: Bus Error in assignment

>while making a variable of B compiler starts using the memory address which is just next to A.

Testing A.03.85 indicates it does that. But I can't duplicate the alignment trap.
What compile options were you using?

Arun  Grover
New Member

Re: Bus Error in assignment.

OOps compiler version which i have given is of "ia" i guess. I'll confirm the compiler options and version as soon as i get hold of integration team.
Dennis Handly
Acclaimed Contributor

Re: Bus Error in assignment

>I get hold of integration team.

You might have them or you get a hold of me directly.