Segmentation Fault for some inputs

Hey,
This is my first time posting. So here I go. In the code below, I have basically used BFS to implement a task. Now, it works for MOST of the inputs, but at very very large inputs i get an error - Segmentation Fault. Now, as far i know it is something to do with memory or buffer overflow(although i dont know what this is), but i cant figure out how to correct it. Any tips would be greatly appreciated.

// code below

#include <queue>
#include <iostream>

using namespace std;

int main()
{

int n,m;
cin>>n>>m;
int**arr=new int*[m+1];
int**adj=new int*[n+1];
for(int i=0;i<=m;i++)
{
arr[i]=new int[3];

}
for(int i=0;i<=n;i++)
{
adj[i]=new int[n+1];

}

for(int i=1;i<=m;i++)
{
for(int j=1;j<=2;j++)
{
cin>>arr[i][j];
}
}
int ss,f;
cin>>ss>>f;

for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
adj[i][j]=0;
}
}

for(int i=1;i<=m;i++)
{
adj[arr[i][1]][arr[i][2]]=1;
adj[arr[i][2]][arr[i][1]]=1;
}







queue<int> s;
int*mark=new int[n+1];
int*length=new int[n+1];
length[ss]=0;
for(int i=0;i<=n;i++)
{mark[i]=0;
}
mark[ss]=1;
s.push(ss);

while(s.empty()==false)
{


int top=s.front();
if(top==f)
{break;
}
s.pop();
for(int i=1;i<=n;i++)
{
if(adj[top][i]==1&&mark[i]==0)
{


mark[i]=1;
length[i]=length[top]+1;
s.push(i);
}
}
}
if(mark[f]==0)
{
cout<<0;
}
else
cout<<length[f];


}




























[code] "Please use code tags" [/code]
¿What is a very very large input? Keep in mind that you are trying to allocate O(n^2)
Well without looking at your code and cause error is segfault I bet it has to do with buffer overflow as you mentioned.

It has to do with your arrays. You are either trying to write to a memory you don't own (like a pointer which in not initialized and points to anything) or in a variety of this case you allocate some memory but exceeds the limits so again trying to access a memory you don't own.

Check you array limits (and you use a lot so good luck)
Hmm eypros, u seem to be right. As it works on the majority of the inputs, I'm guessing there's no problem as far as Accesing memory you don't own goes; this is surely a case of exceeding the allowed memory limit. So to resolve ill have to free some memory and so I'll delete on of the above arrays when I'm done using it. This way I'm might get a solution. Can u tell m a way to do that? (is it even possible?)

Thnx for replying
Ohk!

I put a : delete [] arr;
at the end of initializing the adj[][] array as i was done with it. Now, it seems to run properly.
Thanx All!
Topic archived. No new replies allowed.