0

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)

5
  • Dimensions of Array2? Commented Dec 23, 2015 at 4:51
  • 4 times array. That is not the issue. I also tested this with array[k-rows][l]='X' which removes any uncertainty with array2 and still the error occurs. Commented Dec 23, 2015 at 4:54
  • Why don't you step through the loop and see what the values of rows, k, and l are, and what the values of array[k-rows] and array[k-rows][l] are? That should help you quickly narrow down which part of the expression is going wrong and probably point to why. Commented Dec 23, 2015 at 5:13
  • array[k-rows] accesses out of bounds . Your array is 2x2, but k-rows takes values 0, 1, 2 (the last of those being out of bounds). You probably want to do the %2 check on 5 BEFORE diving it by 2. Commented Dec 23, 2015 at 5:24
  • Why don't you use (a) std::vector and (b) one vector of size rows*cols. That would be considerably easier. Commented Dec 23, 2015 at 6:06

1 Answer 1

2

With rows= 5/2; you set rows to 2. Than you allocate 2 rows array = new char*[rows];. But you iterate from 2 to 4 for(int k = rows; k<5; k++) and write to rows 0 to 2 array[k-rows][l]. You never allocated row with index 2.

rows= 5/2; // now rows is 2
...
if(rows%2 !=0 ) // 2%2!=0

Try this:

rows= 5;
cols= 4;

int allocRows = ( rows%2==0 ) ? rows/2 : rows/2+1;
rows=rows/2;

int allocCols = ( cols%2==0 ) ? cols/2 : cols/2+1;
cols=cols/2;

array = new char*[allocRows];
for(int k = 0; k < allocRows; k++)
    array[k] = new char[allocCols];

... or this ...

int origRows = rows; 
rows= 5/2;
...
if(origRows%2 !=0 )
...
Sign up to request clarification or add additional context in comments.

4 Comments

i am allocating 3 rows array = new char*[rows+1]; due to the conditions in the first piece of code
@john.s Or rows = (rows + 1) / 2; cols = (cols + 1) / 2; instead of the if/else, since both will be positive.
@Gernot1976 the way you did it is correct, however i do want to iterate from 2 to 4, the way you solved it would iterate from 3 to 4.
would this iterate from 2 to 4? i think i still need to do the if else block since i have other stuff to put in the block

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.