3

I am using vector<pair<int,int> > ar[100000]; and I have to use it for several test cases where every time I want it to initialize but I am getting segmentation fault for this.

I tried it with declaring inside the test case loop and globally. its running fine for the first test case or if there is only one test case.

I also tried to delete the vector after every test case but I don't know the exact syntax of deleting a vector of this type, any help ??

int main() {
    long long a, b, c, d = 0, i, j, n, m, t; 
    scanf("%lld", &t);
    while (t--) {
        scanf("%lld %lld", &n, &m);

        vector<pair<long long, long long> > ar[n + 9];
        for(i = 0; i < m; i++) {
            scanf("%lld %lld %lld",&a,&b,&c);
            ar[a - 1].push_back(make_pair(b - 1, c));
            ar[b - 1].push_back(make_pair(a - 1, c));
        }
        vector<long long> distance(10000, 100000000); 
        scanf("%lld", &a); 
        dijkstra(ar, a - 1, distance); 

        for (i = 0; i < n; i++) {
            if (i == a - 1)
                continue;
            if (distance[i] != 100000000)
                printf("%lld ", distance[i]);
            else {
                // printf("%lld\n", visited[i]);
                printf("-1 ");
            }
        }
        printf("\n");
        // ar.clear();
        distance.clear();
    }
    return 0;
}
Mivaweb
  • 5,580
  • 3
  • 27
  • 53
whishky
  • 396
  • 3
  • 13
  • 3
    You have an *array* of vectors, maybe you want an vector of vectors? – Some programmer dude Jun 29 '15 at 07:33
  • 2
    It would also help if you pointed out *where* you get the crash. You can do that by running a debug build of your program in a debugger, and when the crash happens examine the call stack to see where in *your code* the crash happens. Also helpful would be if you could walk up the call stack to your code, and get the values of involved variables, like e.g. the indexes you use for the vectors. – Some programmer dude Jun 29 '15 at 07:35
  • By the way, if `n` is `100000` (or thereabouts) and the size of the `distance` vector is only `10000`, you can go way out of bounds of the `dinstance` vector in the second `for` loop. – Some programmer dude Jun 29 '15 at 07:36
  • is m always less than n+9? is n always less than 10000? Why don't you initialize distance with the number of nodes in the graph? – Brahim Jun 29 '15 at 08:22

1 Answers1

2

vector<pair<long long,long long> > ar[n+9]; is illegal in C++. C-style array dimensions must be known at compile-time.

If your compiler allows this you must be using a compiler extension, which could be leading to your crashes. For example possibly this causes a stack overflow, although we are well beyond what is covered by C++ standards.

Instead of using a C-style array, use a vector:

vector<vector<pair<long long,long long>>> ar(n+9);

Then it is legal, and if you run out of memory you will get a bad_alloc exception thrown. (Adding a catch handler for this case might be useful).

You should also check that array indices are not out of bounds before using them. For example:

scanf("%lld %lld %lld",&a,&b,&c);
if ( a < 1 || a > ar.size() || b < 1 || b > ar.size() )
   throw std::runtime_error("Edge out of bounds");

Also you should check n < 10000 before entering the for(i=0;i<n;i++){ loop, because i is used as an index into distance. In fact hard-coding 10000 seems suspicious here.

Alternatively, using ar.at(a-1) instead of ar[a-1], etc., would work to do the bounds checking.

M.M
  • 138,810
  • 21
  • 208
  • 365
  • Still a mystery to me why the standards committee haven't got a std::multi_array yet. Vectors of vector are clunky and don't perform so well (500 allocations in a 500x500 array, at least) - you have to resize each row as well. Terrible! I hope we get one soon. – Robinson Jun 29 '15 at 07:50
  • actually i tried using vector>> ar but i don't know how to access elements from it can you please tell me ?? – whishky Jun 29 '15 at 08:00
  • and i think vector > ar[n+9]; is not illegal in c++, because its working when no of test cases is 1 – whishky Jun 29 '15 at 08:04
  • size of distance is not a problem because i have only n<=3000 – whishky Jun 29 '15 at 08:08
  • @whishky access elements using `[ ]` the same for a vector as for a C-style array. `vector > ar[n+9];` really is illegal, please see section titled [dcl.array] of the C++ standard. I don't know what you mean by "its working" but if it *appears* to work then it just means your compiler is not following the C++ standard. I explain this in the second paragraph of my answer. If you stick to what is covered by the standard then it is much easier for people to help you as it removes the guesswork. – M.M Jun 29 '15 at 08:08
  • @black have added that to my answer. – M.M Jun 29 '15 at 08:10
  • @MattMcNabb: Several compilers which implement the C99 standard with variable length array declarations also implement them for C++? Why not, since they already put in the implementation effort? You can usually disable that by telling the compiler to use the particular standard you want. gnu++11 vs c++11, for example. – Zan Lynx Jun 29 '15 at 09:04
  • @Matt when i am using iterator as for(auto i=ar[u].begin();i – whishky Jun 29 '15 at 10:31
  • and its guaranteed that a>=1 && b>=1; – whishky Jun 29 '15 at 10:34
  • @ZanLynx C++ has a more complicated type system. Nobody has yet been able to propose VLAs for C++ that don't collapse into disaster somewhere; that's why they were removed from the running for C++14 at an early stage. – M.M Jun 29 '15 at 10:37
  • @whishky you probably have some other problem. Post a [MCVE](http://stackoverflow.com/help/mcve) along with a data set that causes the problem. – M.M Jun 29 '15 at 10:38
  • http://ideone.com/1wKpNM this is my complete code its dijkstra function is causing the problem i guess i am not accessing the vector in appropriate manner – whishky Jun 29 '15 at 10:59