SIGSEGV

closed account (1vf9z8AR)
I am unable to figure out why I am getting an error. removed some parts and ran code but can't point out what is causing the issue.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct node
{
    int val;
    int state;
    node* next=NULL;
};
int main()
{
    int m,n;
    cin>>n>>m;
    node* node[n+1];
    for(int i=1;i<=n;i++)
    {
        node[i]->val=i;
        node[i]->state=1;
    }
    for(int i=1;i<=m;i++)
    {
        int x,y;
        cin>>x>>y;
        node[x]->next=node[y];
        node[y]->state=-1;
        for(int j=1;j<=n;j++)
        {
            vector<int>ans;
            if(node[j]->state!=-1)
            {
                int len=1,cur=j;
                while(node[cur]->next!=NULL)
                {
                    len++;
                    cur=node[cur]->next->val;
                }
                ans.push_back(len);
            }
            sort(ans.begin(),ans.end());
            for(auto x:ans)
                cout<<x<<" ";
            cout<<endl;
        }
    }
}
closed account (E0p9LyTq)
I am somewhat surprised your code even compiles. Line 15 creates a variable length array, that is not allowed in C++.
The first problem is on line 15: You shouldn't use variable length array.

The array on line 15 will contain uninitialized pointers. When accessing them it is very likely that the program crashes. Actually it seem you don't even need pointers.

So either you initialize the pointer (using new) or you create a non pointer array.
> I am unable to figure out why I am getting an error
We're unable to see what you typed in to provoke that behaviour.

It's obviously another one of your quizzes from some site or other, so it's best to tell us which problem you're trying to solve, and what specific test case is causing you problems.

closed account (E0p9LyTq)
I am unable to figure out why I am getting an error.

Using a debugger can help.
Using a debugger can help.

O_o Are you seriously suggesting that some people don't use debuggers..?
closed account (E0p9LyTq)
O_o Are you seriously suggesting that some people don't use debuggers..?

As a matter of fact I am. I very rarely use a debugger.

Most times I can figure out where I screwed up without one.
Most times I can figure out where I screwed up without one.

Aren't you tempted by the time it saves to just double click on your debugger's prompt to take you directly to where you messed up in your code? Especially if you've got many lines of code?
hoogo, we're talking about people like FurryGuy here. People two levels above our imagination. Can't say they might even be writing in raw binary for all we know. Maybe they're using an AI to write their posts for them. Maybe they died 10 years ago and had predicted all upcoming posts and had replies readied to be posted at specific times. Maybe they are from the future and their pet dog learnt to time travel and is replying to our posts in their name. Maybe this is all just.. an illusion.

*magically disappears*
*mind = blown*
Last edited on
closed account (1vf9z8AR)
what no one told me that variable length arrays are not allowed. my college professor also taught us to do like this. :(
I think I will declare maximum size array or maybe vector.

Just to be sure,
you mean
cin>>n;
int arr[n];
these kinds of things are not allowed?

vector is also variable size so is it bad too?
Arrays are supposed to be static in that they're allocated for at compile time (even if the actual array statement is never reached). So yes it's wrong to write variable length arrays because it will compile only on some compilers which support variable length arrays. And the point of C++ code is to be able to be compiled anywhere on any platform with any standard compiler.

Vectors are not static. They use dynamic allocation which is a perfectly fine thing so you don't have to worry about not using vectors. In fact you should use vectors wherever you can because of the rich methods and precaution it offers.

If college means high-school then yes a lot of high school syllabus teach non-standard C++ to kids. For now just stick with what the professor says because you've got no choice.

What compiler are you using? And where do you live? Just curious you don't have to answer the second question.
> these kinds of things are not allowed?
Correct.
1
2
3
4
5
6
7
8
9
10
11
12
13
$ cat foo.cpp
#include <iostream>
int main ( ) {
    int n = 10;
    int a[n];
    a[0] = 0;
    std::cout << a[0];
}
$ g++ -Wall -pedantic foo.cpp
foo.cpp: In function ‘int main()’:
foo.cpp:4:12: warning: ISO C++ forbids variable length array ‘a’ [-Wvla]
     int a[n];
            ^

Your current compiler might let you get away without a warning, but later on you might find one that is less generous. But by then, you might be faced with the problem of unlearning a bad coding habit.

> vector is also variable size so is it bad too?
1
2
3
4
5
6
7
8
9
10
$ cat foo.cpp
#include <iostream>
#include <vector>
int main ( ) {
    int n = 10;
    std::vector<int> a(n);
    a[0] = 0;
    std::cout << a[0];
}
$ g++ -Wall -pedantic foo.cpp

No, because vectors are designed to expand.

The danger of VLA's is that you have zero warning and zero change of error recovery should the user type in say a very large (or negative) size for your array. With a VLA, the program will just crash or the OS will kill it.

At least with vector, you always get a std::bad_alloc exception you can catch, and do something with.

> my college professor also taught us to do like this.
So now you know not to take everything at face value.

while the vector will resize itself, this hides some expensive operations.
if you know anything at all about your data (cin n is good) you can use it to avoid this issue entirely:

vector<int> vla(n); //this allocates n locations for you at creation time.
if n is 10, you can do this right away:
vla[5] = 11; //5 is a valid location. this tiny example is 100% identical (functionally) to your variable length array code only it is legal.

closed account (1vf9z8AR)
@jonnin

so cin>>n;
vector<int>arr[n];
is correct?
closed account (1vf9z8AR)
@Grime

idk if college is same as high school

in college we graduate and get a degree such as bachelor of techonolgy.
or
initial medical degree
or
Bachelor of science etc
1
2
cin >> n;
vector<int> arr(n);

is correct.
closed account (1vf9z8AR)
@Grime
I use multiple compiler like
compilers on practice sites like codechef,hackerearth
codeblocks
ideone

I live in India
All of the compilers you mentioned, I assume, use some GCC extension for VLAs by default unless built with pedantic errors enabled.

Anyway, while the VLA is a concern for portability, the actual error is what the coder777 already mentioned: Deeferencing uninitialized pointers.
Topic archived. No new replies allowed.