-1

What is problem with my code for variable size multidimensional array .How to fix this problem. My code is not passing all test cases.Can anyone help me to fix it.This is question from hackerrank challenge.

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,q;
    cin>>n;
    cin>>q;
    int *arr[n];  
    for(int i=0;i<n;i++)
    {
       int x;
       cin>>x;
       int b[x];
       for(int j=0;j<x;j++)
       {
           cin>>b[j];
       } 
       arr[i]=b;
    }
    while(q--)
    {
        int i,e;
        cin>>i>>e;
        cout<<arr[i][e]<<endl;
    }
    return 0;
}

  • 2
    To begin with, C++ doesn't have [variable-length arrays](https://en.wikipedia.org/wiki/Variable-length_array). – Some programmer dude Jun 08 '20 at 10:14
  • 1
    _"What is problem with my code"_ This depends entirely on what it _should_ do.If you talk about standard C++ VLAs and [`#include `](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) are wrong. – churill Jun 08 '20 at 10:16
  • 1
    You're storing pointers to objects whose lifetimes have ended by the time you access them, so the accessing has undefined behaviour. Use `std::vector`. – molbdnilo Jun 08 '20 at 10:16
  • To continue: `arr[i]=b;` makes `arr[i]` point to the ***local*** array `b`, an array which will go out of scope and end its life-time every iteration of the loop. Leading to *undefined behavior* if you ever try to dereference that pointer after the loop. Or even inside the loop once it has iterated. – Some programmer dude Jun 08 '20 at 10:16
  • Also you are storing a pointer to a local variable which goes out of scope, here `arr[i]=b;` – john Jun 08 '20 at 10:16
  • But in cp contest we can use this header file. – Deepak kumar soni Jun 08 '20 at 10:17
  • The solution to *both* problems is (as already mentioned) [`std::vector`](https://en.cppreference.com/w/cpp/container/vector). – Some programmer dude Jun 08 '20 at 10:17
  • You really have two choices, learn how to use pointers (much too big a topic to cover here) or learn how to use `std::vector` (again a big topic, although a bit easier than pointers). – john Jun 08 '20 at 10:17
  • @Deepakkumarsoni Can use it doesn't mean should use it. It's obviously better to use standard C++ features instead of non-standard. – john Jun 08 '20 at 10:19
  • Regarding "competition" sites, don't use them as a learning resource. All they teach are bad habits and how to create programs for such sites and such sites only. If you want to learn to program properly, take a few classes, including ones in the language of choice but also about data-structures and algorithms and discrete math and logic. Also buy [a few good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282). Then once you're feeling confident and have good habits, use such sites as *training*. – Some programmer dude Jun 08 '20 at 10:20
  • Here is correct code #include using namespace std; int main() { int n,q; cin>>n; cin>>q; int *arr[n]; // passed all test cases for(int i=0;i>x; int *b=new int[x]; for(int j=0;j>b[j]; } arr[i]=b; } while(q--) { int i,e; cin>>i>>e; cout< – Deepak kumar soni Jun 08 '20 at 10:26

1 Answers1

0

Here is correct code.

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,q;
    cin>>n;
    cin>>q;
    int *arr[n];   // passed all test cases
    for(int i=0;i<n;i++)
    {
       int x;
       cin>>x;
       int *b=new int[x];
       for(int j=0;j<x;j++)
       {
           cin>>b[j];
       } 
       arr[i]=b;
    }
    while(q--)
    {
        int i,e;
        cin>>i>>e;
        cout<<arr[i][e]<<endl;
    }
    return 0;
}