Home > Net >  Negative indexing of 2d array in C
Negative indexing of 2d array in C

Time:01-21

In the following code section:


    // Convolution Operation   Sigmoid Activation
    for (int filter_dim=0; filter_dim<5; filter_dim  ) {
        for (int i=0; i<28; i  ) {
            for (int j=0; j<28; j  ) {
                max_pooling[filter_dim][i][j] =0;

                conv_layer[filter_dim][i][j] = 0;
                sig_layer[filter_dim][i][j] = 0;
                for (int k=0; k<filter_size; k  ) {
                    for (int l=0; l<filter_size; l  ) {
                        conv_layer[filter_dim][i][j] = img[-2 i k][-2 j l]*conv_w[filter_dim][k][l];
                    }
                }
                sig_layer[filter_dim][i][j] = sigmoid(conv_layer[filter_dim][i][j]   conv_b[filter_dim][i][j]);
            }
        }
    }

Because img is a 32-by-32 2d array of type int[][], when i=j=k=l=0, img[-2 i k][-2 j l] becomes img[-2][-2] which uses negative indexing on a 2d array, how is this possible? Wouldn't that be index out of bound?

Below is the code in main()

int main() {
    read_test_data();
    read_train_data();
    initialise_weights();

    //for (int i=0; i<
    int epoch = 2000;
    int num = 0;
    cout << "Start Training." << endl;
    for (int i=0; i<epoch; i  ) {
        cout << "Epoch " << i << " done." << endl;
        for (int j=0; j<batch_size; j  ) {
            num = rand()`000;
            int img[32][32];
            int vector_y[10];
            give_y(label_train[num], vector_y);
            give_img(data_train[num], img);
            forward_pass(img);
            backward_pass(dense_softmax, vector_y, img);
            update_weights();
            //num  ;
        }
    }

and

void give_y(int y, int *vector_y) {
    for (int i=0; i<10; i  ) vector_y[i] =0;
    vector_y[y]=1;
}
void give_img(int* vec , int img[][32]) {
    int k=0;
    for (int i=0; i<32; i  ) {
        for (int j=0; j<32; j  ) {
            if (i<2 || j<2) {
                img[i][j] = 0;
            } else {
                img[i][j] = vec[k];
                k  ;
            }
        }
    }
}

CodePudding user response:

Wouldn't that be index out of bound?

Yes it is out of bounds of the array which leads to undefinded behavior.

Undefined behavior means anything1 can happen including but not limited to the program giving your expected output. But never rely(or make conclusions based) on the output of a program that has undefined behavior.

So the output that you're seeing(maybe seeing) is a result of undefined behavior. And as i said don't rely on the output of a program that has UB.

So the first step to make the program correct would be to remove UB(in this case by making sure that index don't go out of bound). Then and only then you can start reasoning about the output of the program.


1For a more technically accurate definition of undefined behavior see this where it is mentioned that: there are no restrictions on the behavior of the program.

  •  Tags:  
  • Related