Dynamic allocation giving problem in c++

Asked

Viewed 175 times

1

Have this problem to solve in Hacker Rank and my code for the solution of this problem was as follows:

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    int **arr, arrqtd, queries;

    cin >> arrqtd >> queries;

    arr = (int **) malloc(sizeof(int)*arrqtd);  
    for (int i = 0; i < arrqtd;i++){
        int size;

        cin >> size;
        arr[i] = (int *) malloc(sizeof(int)*size);

        for(int j = 0; j < size;j++){
            int value;
            cin >> value;
            arr[i][j] = value;
        }
    }

    for (int i = 0; i < queries;i++){
        int row,col;
        cin >> row >> col;
        cout << arr[row][col] << endl;
    }
    return 0;
}

The problem is that in an input example like this:

10 10
3 916135 272188 794963
3 178997 502468 671251
1 122266
3 223511 996043 990104
3 319694 335208 200789
2 867809 273793
1 925358
1 71140
1 862238
1 994309
6 0
5 0
5 0
7 0
5 0
6 0
3 2
3 1
0 0
9 0

The initial value of my matrix changes from the sixth iteration, I would like to know what I did wrong but so far I haven’t found anything (and I would also like to know why this initial value changes from "nothing").

inserir a descrição da imagem aqui

  • On my machine, it worked. Slackware Current, GCC 7.2.0 and Clang 5.0.0.

  • 1

    Whereas you even have the #include <vector> at the top would be much easier using vectors, unless it is a constraint of the problem. Still from what I ran and saw in the code it seemed correct. The platform gives you some error in the submission ?

  • 1

    If it is C++, why not use new int[]?

  • @lemoce, when running on my machine appears this print that I put in the question.

  • @Isac I do not use the vector for two reasons, I am new in c++ and do not understand and why I wanted to understand the pq of not working the way I wrote and I did not see any error in logic. And on submission, when no error gives exceeded time limit.

  • @Jeffersonquesado because I do not understand how the same works.

Show 1 more comment

1 answer

3


In fact you have a very subtle little error in your program, and it is in the memory allocation of the two-dimensional array:

arr = (int **) malloc(sizeof(int)*arrqtd);  

Which should be

arr = (int **) malloc(sizeof(int*)*arrqtd);  
//------------------------------^

It’s doing an array of pointers, so it’s sizeof of int* that matters. It will only make a difference on a machine where the size of int is different from int*, and so may not come across the error depending on where the code runs.

With this change the program produces the desired output:

925358
867809
867809
71140
867809
925358
990104
996043
916135
994309

Check in with Ideone

Recommendations:

Less educational/ludic purposes, it would be more appropriate to use either <vector> or new int[] as @Jeffersonquesado mentioned for 2 reasons:

1 - Because they become much simpler and avoid mistakes such as the one that had.

2 - Are more idiomatic in the world of C++. The code you wrote is basically C code using couts and cins

  • Actually, now it’s totally worked out. About the code being written "totally" in C is because I know only C and I am learning now C++, so I don’t know and I didn’t use the <vector> nor the new int []

Browser other questions tagged

You are not signed in. Login or sign up in order to post.