Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with Bus Error when I run my program

Status
Not open for further replies.

ckrieger1

Programmer
Feb 13, 2004
6
US
I need help. I keep getting a Bus Error when I run my program and I've narrowed down where it occurs to the following code fragment.
px0c0 and px0c1 are type double pointers and
count_x0c0[m] and countc0 are integers. Can anyone see why this code would be giving me a Bus Error? Thanks.

// set *px0c0 and *px0c1
for(int m=0;m<data.n;m++)
{
bayes.px0c0[m]=1.0 * (count_x0c0[m]/countc0);
bayes.px0c1[m]=1.0 * (count_x0c1[m]/countc1);
}

bayes.pc0 = 1.0 * countc0 / data.N;
 
Check up size of memory chunks referenced via pointers (must be >0 and <= data.n).
(If these sizes are OK, change your processor;)

Apropos, do you want an integer division in the loop? What for senseless 1.0*integer? If you want a real division, use (double)i/j casting in parentheses.
 
Bus errors are normally caused by alignment problems.

1) What is bayes? Is it a reference within some character array?
2) Do px0c0 and px0c1 contain valid values? Have they been newed or malloced?
 
The way I allocated px0c0 and px0c1:
bayes.px0c0 = (double*) calloc(n, sizeof(double));
bayes.px0c1 = (double*) calloc(n, sizeof(double));

Then I used a for loop to initialize all of them. They all contain valid values.
 
Put this in after your calloc
Code:
printf ("%p %p\n", bayes.px0c0, bayes.px0c1);
Both the values printed out should end in 0, 4, 8 or C. If they end with anything else, you have a word alignment problem. Not sure how you get around this one.

Is this C or can you use C++? If you can use C++, try
Code:
bayes.px0c0 = new double[n];
bayes.px0c1 = new double[n];
That will definitely be word aligned.
 
Are n, data.n and data.N the same variable?
 
Ok I tried that. I guess it's not an alignment problem. Here is the code for the whole function. n data.n and data.N are all different variables.

int allocate_bayes(Bayes &bayes, int n)
{
// bayes.px0c0 = (double*) calloc(n,sizeof(double));
// bayes.px0c1 = (double*) calloc(n,sizeof(double));
bayes.px0c0 = new double[n];
bayes.px0c1 = new double[n];
printf ("%p %p\n", bayes.px0c0, bayes.px0c1);

for (int i=0; i<n; i++)
bayes.px0c0=bayes.px0c1=0.5;
bayes.pc0=0.5;
}
 
You probably need to look further up the stack.
1) Is n 0 or negative
2) How is bayes passed in? Are you doing something like allocate_bayes (*pbayes, 10)? If you are, is pbayes a legal value. It should probably be word aligned.

Also try some print statements - see how far it goes into the loop before it crashes
Code:
  printf ("bayes is %p\n", &bayes); fflush(stdout);
  for (int i=0; i<n; i++)
  {
    print ("i = %d\n", i);  fflush (stdout);
    bayes.px0c0[i]=bayes.px0c1[i]=0.5;
  }
  printf ("out of loop\n");  fflush (stdout);
  bayes.pc0=0.5;
  printf ("not crashing in allocate_bayes\n"); fflush (stdout);

You need the fflush otherwise not all the output will be flushed and you may be chasing something that is perfectly OK.
 
Well I feel stupid lol, I just found my error, all I did was change a 'j' to a 'k' in another part of my program and I got rid of the bus error.
 
Don't worry about it - it happens to everyone.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top