Need help with Bus Error, Core Dumped

My code successfully compiles when I run the makefile. When I go to run the program, it outputs 4: {(2,0), (0,1), (3,2), (1,3)}, but then says Bus Error (core dumped). I understand this has something to do with allocation of memory, but I can't seem to figure out what is causing this. The NQueensProblemSingleSolution.h and testNQueens.cc were given and I wrote NQueensProblemSingleSolution.cc. I would appreciate any help you can give me. Thank you.

NQueensProblemSingleSolution.h:

#ifndef NQUEENSPROBLEMSINGLESOLUTION_H // Avoid double includes
#define NQUEENSPROBLEMSINGLESOLUTION_H // Prevent double include

#include <iostream>

using namespace std;

class NQueensProblemSingleSolution {
private:
int n;
int *solutionVector;
public:
NQueensProblemSingleSolution(int numberOfQueens);
NQueensProblemSingleSolution(const NQueensProblemSingleSolution& solution);
~NQueensProblemSingleSolution();
NQueensProblemSingleSolution& operator=(const NQueensProblemSingleSolution& rhs);
friend ostream& operator<<(ostream& out, const NQueensProblemSingleSolution& solution);
};

#endif

NQueensProblemSingleSolution.cc:

#include "NQueensProblemSingleSolution.h"

using namespace std;

NQueensProblemSingleSolution::NQueensProblemSingleSolution(int numberOfQueens)
{
int i, j;

delete[] solutionVector;
n = numberOfQueens;
solutionVector = new int[n];

if ((n % 2) != 0)
{
--n;
solutionVector[n] = n;

}

if(n % 6 - 2 == 0)
{
for (int m = 1; m<=n/2; m++)
{ for(int k = 0; k<2; k++)
{
if(k == 0)
{
j = 1 + (2*(m-1) + n/2 - 1) % n;
i = m;
}
else
{
j = n - (2*(m-1) + n/2 - 1) % n;
i = n + 1 - m;
}
solutionVector[--j] = --i;
}
}

}

else
{
for (int m = 0; m<=n/2; m++)
{
for (int k = 0; k<2; k++)
{
if(k==0)
{
j = 2*m;
i = m;
}
else
{
j = 2*m-1;
i = n/2 + m;
}
solutionVector[--j] = --i;
}
}

}

else
{
for (int m = 0; m<=n/2; m++)
{
for (int k = 0; k<2; k++)
{
if(k==0)
{
j = 2*m;
i = m;
}
else
{
j = 2*m-1;
i = n/2 + m;
}
solutionVector[--j] = --i;
}
}
}

}

NQueensProblemSingleSolution::NQueensProblemSingleSolution(const NQueensProblemSingleSolution& solution)
{
n = solution.n;
solutionVector = new int[n];
for (int i = 0; i<n; i++)
solutionVector[i] = solution.solutionVector[i];
solutionVector[n] = 0;
}

NQueensProblemSingleSolution::~NQueensProblemSingleSolution()
{
delete[] solutionVector;
}

NQueensProblemSingleSolution& NQueensProblemSingleSolution::operator=(const NQueensProblemSingleSolution& rhs)
{
if (this == &rhs)
return *this;
delete[] solutionVector;
n = rhs.n;
solutionVector = new int[n];
for (int i = 0; i<n; i++)
solutionVector[i] = rhs.solutionVector[i];
solutionVector[n] = 0;
return *this;
}

ostream& operator<<(ostream& out, const NQueensProblemSingleSolution& solution)
{
out<<"{";
for (int j = 0; j<solution.n; j++)
{ if(j == solution.n-1)
out<<"("<<solution.solutionVector[j]<<","<<j<<")";
else
out<<"("<<solution.solutionVector[j]<<","<<j<<"), ";
}

out<<"}"<<endl;
}

testNQueens.cc:

#include "NQueensProblemSingleSolution.h"

int main()
{
/*
* The following output should be produced:
*
* 4: {(2,0), (0,1), (3,2), (1,3)}
* 5: {(2,0), (0,1), (3,2), (1,3), (4,4)} ...
*/

for (int i = 4; i <= 16; i++) {
NQueensProblemSingleSolution result = NQueensProblemSingleSolution(i);
cout << i << ": " << result << endl;
}

return(0);
}
[6073 byte] By [szap] at [2007-11-20 11:43:40]
# 1 Re: Need help with Bus Error, Core Dumped
Your code is not correct in this line

n = solution.n;
solutionVector = new int[n];
for (int i = 0; i<n; i++)
solutionVector[i] = solution.solutionVector[i];
solutionVector[n] = 0; /* --> This will cause memory error. Max. index is (n-1) */
henky@nok.co.id at 2007-11-9 1:26:04 >
# 2 Re: Need help with Bus Error, Core Dumped
I tried erasing that line and changing new int[n] to new int[n+1] and keeping that line, but I'm still getting the same error. Any other suggestions?
szap at 2007-11-9 1:27:01 >
# 3 Re: Need help with Bus Error, Core Dumped
See this code:

for (int m = 0; m<=n/2; m++)
{
for (int k = 0; k<2; k++)
{
if(k==0)
{
j = 2*m;
i = m;
}
else
{
j = 2*m-1;
i = n/2 + m;
}
/* See if m == 0 & k == 0 then j will be 0, then below code will cause error */
solutionVector[--j] = --i;
}
}

i'm sorry i don't check the whole algorithm of your code but just attempting to find the bugs.
henky@nok.co.id at 2007-11-9 1:28:11 >
# 4 Re: Need help with Bus Error, Core Dumped
NQueensProblemSingleSolution::NQueensProblemSingleSolution(int numberOfQueens)
{
int i, j;

delete[] solutionVector;

That delete is illegal. You are attempting to delete an uninitialized pointer. The solutionVector pointer is not set to anything, but you are calling delete[] on this memory.

The instant fix to all of your problems is to use std::vector<int> and not raw pointers, and leave the memory management to the vector class.

Regards,

Paul McKenzie
Paul McKenzie at 2007-11-9 1:29:11 >
# 5 Re: Need help with Bus Error, Core Dumped
For example:

#ifndef NQUEENSPROBLEMSINGLESOLUTION_H // Avoid double includes
#define NQUEENSPROBLEMSINGLESOLUTION_H // Prevent double include

#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

class NQueensProblemSingleSolution
{
private:
int n;
std::vector<int> solutionVector;

public:
NQueensProblemSingleSolution(int numberOfQueens);
friend std::ostream& operator<<(std::ostream& out, const NQueensProblemSingleSolution& solution);
};

#endif

NQueensProblemSingleSolution::NQueensProblemSingleSolution(int numberOfQueens)
{
int i, j;
n = numberOfQueens;
solutionVector.resize(n);
//...

There is no longer any need for a copy constructor, assignment operator, or destructor, or new[]/delete[]. Using vector, the only thing you have to be aware of is that you are accessing valid data when you say "solutionVector[whatever]". Nothing can save you if "whatever" is out of bounds, unless you use the std::vector::at() function instead of [].

Regards,

Paul McKenzie
Paul McKenzie at 2007-11-9 1:30:12 >
# 6 Re: Need help with Bus Error, Core Dumped
I appreciate all the help. I wish I could use your last suggestion, but my professor wants us to do it this way. I filtered through it and made a few changes that I figured would throw it off, however I am still receiving the error. I'll be going through it some more tomorrow trying to find anything else. I'll post my updated code tomorrow.
szap at 2007-11-9 1:31:13 >
# 7 Re: Need help with Bus Error, Core Dumped
I appreciate all the help. I wish I could use your last suggestion, but my professor wants us to do it this way.But you do no checking on whether you are accessing the array at legal indices.

Several places, you are doing things like this:

solutionVector[--j]

But you don't check if "--j" or whatever the index you're using is within bounds or not. Why not just write a cout to make sure that the index is between 0 and n-1?

Plus, your assignment operator is not exception safe. If new[] happened to throw an exception, you've lost your solutionVector for good. Look at your code closely to see what I'm referring to.

if (this == &rhs)
return *this;
delete[] solutionVector; // destroyed this->solutionVector
n = rhs.n;
solutionVector = new int[n]; // what if new[] throws an exception here?

solutionVector[n] = 0; // This is an error. n is an illegal index!!

Not only is it not exception safe, why are you setting solutionVector[n] to *anything*? Even if "n" were in bounds (which it isn't), isn't this supposed to be an assignment operator? So just assign what is in the object you passed (rhs) and be done with it. It doesn't make sense to start making up your own values there.

Regards,

Paul McKenzie
Paul McKenzie at 2007-11-9 1:32:15 >
# 8 Re: Need help with Bus Error, Core Dumped
When I originally wrote the code I didn't fully understand everything, I was looking at the examples and trying to write it in a similar manner. However, after going over it a few times, I understand it much better and I understand everything you are saying. Some of that stuff I had added after first getting the Bus Error to see if it would fix it. I have gone through everything and removed stuff I realized didn't make any sense.

I see how I should check to make sure that --j never goes below 0 since that would cause an error. However, in the case of this program, j will always result in a values 1 to n(n being the number of columns on a given chess board). I realized that in the original code I posted, this was not the case, but I'm pretty sure I fixed that.

updated NQueensProblemSingleSolution.cc:

#include "NQueensProblemSingleSolution.h"

using namespace std;

NQueensProblemSingleSolution::NQueensProblemSingleSolution(int numberOfQueens)
{
int i, j;

n = numberOfQueens;
solutionVector = new int[n];

if ((n % 2) != 0)
{
n=n-1;
solutionVector[n] = n;

}

if(n % 6 - 2 == 0)
{
for (int m = 1; m<=n/2; m++)
{ for(int k = 0; k<2; k++)
{
if(k == 0)
{
j = 1 + (2*(m-1) + n/2 - 1) % n;
i = m;
}
else
{
j = n - (2*(m-1) + n/2 - 1) % n;
i = n + 1 - m;
}
solutionVector[--j] = --i;
}
}

}

else
{
for (int m = 1; m<=n/2; m++)
{
for (int k = 0; k<2; k++)
{
if(k==0)
{
j = 2*m;
i = m;
}
else
{
j = 2*m-1;
i = n/2 + m;
}
solutionVector[--j] = --i;
}
}

}

else
{
for (int m = 1; m<=n/2; m++)
{
for (int k = 0; k<2; k++)
{
if(k==0)
{
j = 2*m;
i = m;
}
else
{
j = 2*m-1;
i = n/2 + m;
}
solutionVector[--j] = --i;
}
}
}

}

NQueensProblemSingleSolution::NQueensProblemSingleSolution(const NQueensProblemSingleSolution& solution)
{
n = solution.n;
solutionVector = new int[n];
for (int i = 0; i<n; i++)
solutionVector[i] = solution.solutionVector[i];
}

NQueensProblemSingleSolution::~NQueensProblemSingleSolution()
{
delete[] solutionVector;
}

NQueensProblemSingleSolution& NQueensProblemSingleSolution::operator=(const NQueensProblemSingleSolution& rhs)
{
if (this == &rhs)
return *this;
delete[] solutionVector;
n = rhs.n;
solutionVector = new int[n];

for (int i = 0; i<n; i++)
solutionVector[i] = rhs.solutionVector[i];
return *this;
}

ostream& operator<<(ostream& out, const NQueensProblemSingleSolution& solution)
{
out<<"{";
for (int j = 0; j<solution.n; j++)
{ if(j == solution.n-1)
out<<"("<<solution.solutionVector[j]<<","<<j<<")";
else
out<<"("<<solution.solutionVector[j]<<","<<j<<"), ";
}

out<<"}"<<endl;
}

Update:

After fixing all of the problems I had found, I was still getting a bus error in the same spot(right after the output). I realized I forgot to "return out". Now, my program works correctly.

I really appreciate all of your help. It gave me a much better understanding of the material.
szap at 2007-11-9 1:33:13 >