I am working on a C++ program and the issue is in the below code. (NB: code has been simplified for readability but the essence is still there). In the first part i am just creating a 2D array with a few simple conditions
rows= 5/2;
cols= 4/2;
char** array;
if(rows%2 !=0 ){
array = new char*[rows+1];
}else {
array = new char*[rows];
}
for(int k = 0; k < rows; k++)
{
if(columns%2 !=0){
array[k] = new char[cols+1];
}else{
array[k] = new char[cols];
}
}
So far so good. the code works perfectly. the next part is where the issue is,
for(int k = rows; k<5; k++){
for (int l=0; l< 2; l++){
array[k-rows][l]=Array2[k][l];
}
}
so basically this code is just retrieving a small part of a larger array (array2) and inserting it into array. but An error keeps coming up at array[k-rows][l]=Array2[k][l]; which says Thread 1:Exc_BAD_ACCESS(code = 1, address = 0xe). (i am using xcode 7.2)
Array2?array. That is not the issue. I also tested this witharray[k-rows][l]='X'which removes any uncertainty with array2 and still the error occurs.rows,k, andlare, and what the values ofarray[k-rows]andarray[k-rows][l]are? That should help you quickly narrow down which part of the expression is going wrong and probably point to why.array[k-rows]accesses out of bounds . Your array is 2x2, butk-rowstakes values0,1,2(the last of those being out of bounds). You probably want to do the%2check on5BEFORE diving it by 2.