Finding this strange bug? It crashes without noticing anything - c++

I wrote a function within my code that should create some sort of matrices. It is fine when the size is small, but when it gets bigger, it crashes at the middle of this function without giving any information. I did that with both debug and release mode and same thing happened. Any idea on what can be wrong? Someone suggested me it could be buffer overrun.
In this function when kl.mechelms get bigger than a certain number, it crashes. The following code uses a function and gets a 3 by 3 matrix and stores it in kl.scoff which size is [3][3][kl.mechelms][kl.mechelms]. The problem happens when kl.mechelms are like bigger than 7000, but we need far more than that for our code.
Could the function Calc_3D which I use within this part cause the problem? I think it shouldn't since it just reads some values.
for (int z = 0;z<3;z++) {
for (int q = 0;q<3;q++) {
kl.scofsarr[z][q] = new double *[kl.mechelms];
}
}
for (int i = 0;i<kl.mechelms;i++) {
cout << "element " << i << "of " << kl.mechelms << endl;
kl.comments << "element " << i << "of " << kl.mechelms << endl;
for (int z = 0;z<3;z++) {
for (int q = 0;q<3;q++) {
kl.scofsarr[z][q][i] = new double[kl.mechelms];
}
}
for (int j = 0;j<kl.mechelms;j++) {
Calc_3D(i,j, kl.elmx[j], kl.elmy[j], kl.elmz[j], kl.anglemat[j], kl.dip[j], kl.elmx[i],kl.elmy[i],kl.elmz[i],
kl.elma[i],kl.rectza,kl.anglemat[i],kl.dip[i], kl.G, kl.v, kl.scofs, kl.rdepth);
for (int z = 0;z<3;z++) {
for (int q = 0;q<3;q++) {
kl.scofsarr[z][q][i][j] = kl.scofs[z][q];
}
}
}
}

Related

Release vs Debug version: suddenly runtime error

I have a program that performs an FFT on a 2d array. In order to work with the fft library fftw3 I have to use a temporary array (called FTtemp) that reads out the result of the FFT: it is 3d since it contains the x & y axis plus the real and imaginary value for each (x,y) tupel.
The transfer of the data from the FFT array (which has a special variable type) to the ordinary array is working in debug mode but not in release. In release I get the following runtime error: Access violation writing location 0x02913000.
From my google search I found that release version bugs are usually related to uninitialized objects. This led me to explicitly initialise every item in FTtemp with 0.0, however to no avail. Furthermore, I printed the FFt array items to console and numbers appeared which means that they are also initialised. Hence, I am a bit out of ideas and wondered if someone might be wiser than me?
Here is the code snippet I am talking about. Since the program relies on a lot of other things, I was not quite able to recreate a minimal example yet, but I will add one as soon as I got the same error.
Fun fact: I print the I & j values of the loop to the console for trouble shooting and it is another (I,j) tupel where it crashes every time when I run it: eg: 49,212 or 116,169. I am really confused by this.
FTtemp = new double** [width];
for (i = 0; i < width; i++) {
FTtemp[i] = new double*[height];
}
for ( i = 0; i < width; i++)
{
for (j = 0; j < height; j++) {
FTtemp[i][j] = new double[2];
FTtemp[i][j][0] = 0.0;
FTtemp[i][j][1] = 0.0;
}
}
cout << "width,height: " << width << "," << height << endl;
for (i = 0; i < width; i++)
{
for (j = 0; j < height; j++) {
/*
cout << "access to out: " << out[indexFFT(i, j)][0] << endl;
cout << "access to FTtemp: " << FTtemp[i][j][1] << endl;
*/
cout << "i,j is: " << i << "," << j << endl;
FTtemp[i][j][1] = out[indexFFT(i, j)][0]; <--------- error occours here
FTtemp[i][j][2] = out[indexFFT(i, j)][1];
}
}
Thank you for your consideration.
All the best,
Blue
There is an error in this line:
FTtemp[i][j][2] = out[indexFFT(i, j)][1];
Notice that FTtemp[i][j] is initialized to new double[2] earlier in your code, which means that FTtemp[i][j][2] is an out-of-bounds write.
There may be other issues here - perhaps indexFFT(i, j) gives a bad index? - but without seeing how out was initialized or how indexFFT works we can't be sure.
Hope this helps!

Passing the elements of a vector to a different data structure by address

I am building a 2d game and I am storing all my enemy objects in an array. Right now I am trying to implement a quadtree. Currently I am just trying to build the quadtree and am not concerned with collisions. The code that pushes items to the quadtree is the following :
for (std::vector<Enemy>::iterator i=m_enemies.begin(); i != m_enemies.end(); ++i) {
std::cout << &(*i) << "Address of the object" << std::endl;
m_quad.Insert(&(*i));
}
The code for the Insert is the following :
void Quad::Insert(sf::RectangleShape* l_gameObject){
std::cout << &l_gameObject << "dsa1" << std::endl;
std::cout << "called insert " << m_objects.size() << std::endl;
m_objects.push_back(l_gameObject);
if (m_level < m_maxLevel) {
if (m_objects.size() > 3) {
std::cout<< "creating subregions " << m_objects.size() << std::endl;
m_subRegions.push_back(Quad(m_x,m_y,m_width/2.f, m_height/2, m_level + 1, m_maxLevel-1));
m_subRegions.push_back(Quad(m_x+m_width/2.f,m_y,m_width/2.f,m_height/2.f, m_level + 1, m_maxLevel-1));
m_subRegions.push_back(Quad(m_x+m_width/2.f, m_y + m_height/2.f, m_width/2.f, m_height/2.f, m_level + 1, m_maxLevel-1));
m_subRegions.push_back(Quad(m_x, m_y + m_height/2.f, m_width/2.f, m_height/2.f, m_level + 1, m_maxLevel-1));
std::vector<int> temp;
for (int i=0; i < m_objects.size(); i++){
for (int j=0; j< m_subRegions.size(); j++) {
if (m_subRegions[j].Contains(m_objects[i])) {
m_subRegions[j].Insert(m_objects[i]);
temp.push_back(i);
break;
}
}
}
for (int i = temp.size(); i > -1; i--){
m_objects.erase(m_objects.begin() + temp[i]);
}
}
}
}
When I print the address that I am passing to the Insert function and the one I have in the function I see that they are different. In fact the on in is always the same and the one I pass is always different as it should be. Could anyone clarify why that is the case ?
EDIT : Thanks to gsamaras for pointing out that I was printing the address of the parameter.
Followup question
When I use the methods of the object I am addressing in the first for loop I get the correct results, but when I do the same thing in the Insert function I get 0. Why is that ?
You are printing the address of the address.
Change this:
std::cout << &l_gameObject << "dsa1" << std::endl;
to this:
std::cout << l_gameObject << "dsa1" << std::endl;
in order to print the same thing as outside your of your function.
Inside Insert, you're printing the address of the parameter.
Outside Insert, you're printing the parameter's value.
You want
std::cout << l_gameObject << "dsa1" << std::endl;
since l_gameObject is the address you're passing in.

Double vector hides variables

I'm having an interesting problem when I'm accessing a double vector. The idea is that I have deleted all information prior to accessing the vector. A for loop tries to access the vector and successful says that the vector is empty, but when I access the vector point directly it shows that there are variables still in the vector.
Also, the vector was set up like so:
vector<vector<string>> proTable;
Here is the loop attempting to access the vector.
for(int a = 0; a < proTable.size(); a++)
{
for(int b = 0; b < proTable[a].size(); b++)
{
cout << proTable[a][b] << "\t";
}
}
But if I edit the for loop this way it returns the variable inside.
for(int a = 0; a < proTable.size(); a++)
{
for(int b = 0; b < proTable[a].size(); b++)
{
cout << proTable[a][b] << "\t";
}
cout << proTable[0][0];
}
The first prints nothing out. The second prints X which was in the vector before. Also, the vector does not show that it is empty.
This is how I was deleting it if it matters.
void MRelation::RemoveColumn(vector<int> rem)
{
while(!rem.empty())
{
int z = rem[rem.size() - 1];
for(int a = 0; a < proTable.size(); a++)
{
for(int b = z; b < proTable[a].size() - 1; b++)
{
proTable[a][b] = proTable[a][b+1];
}
proTable[a].pop_back();
}
rem.pop_back();
}
}
The vector rem holds the columns that need to be deleted from the table.
I have deleted all information prior to accessing the vector.
Accessing an vector out of bounds has undefined behaviour. Since your vector is empty, proTable[0] is out of bounds. In the line cout << proTable[0][0];, you access proTable[0]. Therefore the behaviour of your program is undefined.
it shows that there are variables still in the vector.
You cannot jump to such conclusion from observing undefined behaviour.
"There are variables still in the vector" was not necessarily the reason why you saw output. You saw output because the behaviour was undefined.
I found out what it was. I you delete the contents of the inner vector but don't delete the vectors themselves then the vector will think that it contains something still and will pull out information that doesn't exist anymore. here is the code I was have problems with. It has been edited with an if statement to correct it.
void MRelation::FinalPrint()
{
if(curTable.size() < 2)
{
ss << "? No\n";
}
else
{
ss << "? Yes(" << curTable.size() - 1 << ")\n";
}
if(!proTable[0].empty()) //This was added in after to correct the problem
{
for(int c = 1; c < proTable.size(); c++)
{
ss << " " << proTable[0][0] << "=" << proTable[c][0];
for(int d = 1; d < proTable[0].size(); d++)
{
ss << ", ";
ss << proTable[0][d] << "=" << proTable[c][d];
}
ss << "\n";
}
}
}
Sorry about not putting everything in context before. I was trying to put in as much relavant information without putting in 300 lines of code.

simple for loop finishes and fails the run

For some unknown reason this simple code runs, does what it's expected to do and then crashes the run. I am using NetBeans IDE, which overlapped my arrays before (tends to be buggy), so I was wondering if someone gets the same error - that would mean I certainly have to change the IDE environment.
#include <iostream>
using namespace std;
int main ()
{
int first[4][4];
for (int a = 0; a < 5; a++)
{
for (int b = 0; b < 5;b++)
{
cout << a << " " << b << " ";
if (first [a][b] != 0)
{
first[a][b] = 0;
}
cout << first[a][b] << " ";
}
cout << endl << endl << endl;
}
return 0;
};
here you are declearing a array with 4 indexes.In c/c++ index number starts at 0.
In your code you are saying :
int first[4][4];
that means indexs are : 0 1 2 3.Array length or total index are 4.
But in for loop you are saying
for (int a = 0; a < 5; a++) {
....
}
so you are trying to access index number 0 1 2 3 4 respectively.But remember you don't have index number 4.That is why it should give array index out of bound error.
Also at the end of main function you are using a semicolon.remove that
main () {
....
};
Hope this solves the problem.From next time Please try to provide details about the errors your IDE is giving you as it will be easier for the people who are giving answer.

C++ Skipping elements in a one d array

I have an assignment with several ways to manipulate an array, but I'm having trouble with one of the parts.
I am reading about 50 numbers into the array from a .txt file
And for every odd location in the array (1,3,5,…), I have to subtract it from the previous even location (0,2,4,…) and store results in the odd location. Then I print out all values in the array.
Here is what I have so far:
void oddMinusEven(int ary[],int num)
{
for(int idx = ary[0]; idx<num; ary[idx+2])
{
ary[idx] = ary[idx+2]-ary[idx];
cout<<ary[idx]<<endl;
}
}
How do I do this? If you could provide some examples, that would be great.
This should do:
void oddMinusEven(int ary[], int num) {
for(int i = 1; i < num; i += 2) {
ary[i] = ary[i-1] - ary[i];
std::cout << "a[" << i << "] = " << ary[i] << std::endl;
}
}