Segmentation fault (arrays) queries...

[b]No need to read the full code just see the global array declaration(edit[2001][2001]) as my
discussion is based on that only.


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
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
int edit[2001][2001];
char a[2001],b[2001];
int main()
{
   int t;unsigned int m,n,pre1=0,pre2=0;
   bool flag;
   scanf("%d",&t);getchar();
    while(t--)//test cases
    {
        
        scanf("%s",a);m=strlen(a)+1;//cout<<m;
        scanf("%s",b);n=strlen(b)+1;//cout<<n;
     
     for(unsigned int x=0;x<m;x++)
        {
            for(unsigned int y=0;y<n;y++)
            {
                if(x==0&&y==0){edit[x][y]=0;}
                else if(x==0){edit[x][y]=edit[x][y-1]+1;}//insert operation
                else if(y==0){edit[x][y]=edit[x-1][y]+1;}//delete operation
                else
                {   
                    if(a[x-1]==b[y-1]){flag=0;}
                    else {flag=1;}
                        edit[x][y]=min(min(edit[x-1][y-1]+flag,edit[x-1][y]+1),edit[x][y-1]+1);
                    
                }
            }
        }

printf("%d\n",edit[m-1][n-1]);

}
 
    return 0;
} 

when i submitted this solution on spoj i my solution got accepted.Here i declared array globally:
edit[2001][2001] and character array a[2001],b[2001]

Initially when i approached the solution
i declared array inside the main function
1
2
3
4
main(){
edit[2001][2001];
a[2001],b[2001];
}

i got segmentation fault;

so i thought instead of allocating memory from stack i should declare
it dynamically i.e to allocate it from heap as array size was large and also after that delete it to avoid any memory leaks
i.e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
main()
{
int**edit=new int*edit[2001];
for(unsigned int k=0;k<2001;k++)
{
edit[k]=new int[2001];
}


//for deleting  edit[2001][2001]
for(unsigned int k=0;k<2001;k++)
{ delete []edit[k];
}
delete edit;
}

but then also i got segmentation fault

Then after searching for the reason i got to know that there is another way of allcoating memory from heap thet is declaring it globally.

So i declared it globally as shown in my solution and it got accepted

My question is
1) Why static allocation from stack directly in main function didn't worked?
is that because array size was 2001*2001 =4*10^6??
2) If static allocation didn't why dynamic allocation inside main using new delete didn't workded
3) And why global declaration worked ?? Also is global declaration allocate memory from heap.??
4) Also, what difference declaring global array static creates as my both solution with or without using static worked.
[/b]
Last edited on
Why static allocation from stack directly in main function didn't worked?
The static is a finite resource. You should use it with care.

The default stack on MS-DOS on the Latice, Microsoft and Borland C compilers is 2K. You're attempting to allocate (sizeof(int) + 1)*sq(2001) bytes, that's quite a bit.

If static allocation didn't why dynamic allocation inside main using new delete didn't workded
It would.

And why global declaration worked ?? Also is global declaration allocate memory from heap.??
It doesn't come from the heap, it comes from a seperate segment for globals.

And why global declaration worked ?? Also is global declaration allocate memory from heap.??
There's plenty of space in the static segment and heap, the stack isn't.
> delete edit;
I told you already, you are invoking undefined behaviour.
http://www.parashift.com/c++-faq-lite/delete-array.html


Fix the font in your post.
@ne555 and @ kbw thanks for your answers :)
But as you said @kbw that my code with new and delete should worked .
It worked on codeblocks and also it worked on ideone but when i submitted my solution to SPOJ. It gave segmentation fault. here is my code
http://ideone.com/LG8ydc
Though i took care of deleting the array after use to avoid any memory leaks
still it gave segmentation fault on SPOJ

@ ne555 :Is it like that you want to say avoid using dynamic allocation as much as possible ??

I aslo want to ask :
How should we decide in that array to be declared statically or dynamically ie in which cases static allocation is better and in which case dynamic allocation is better.And also in which case global declaration is beneficial ?

Your program had a bug, you needed to write delete[] edit;
You wouldn't have such an issue if you
1- didn't use dynamic memory allocation. Possible given that you do know the maximum size that you want to get
a- encapsulate the behaviour. By instance, by using std::vector or std::unique_ptr.

> which case dynamic allocation is better
I don't know of any case.
There are times when dynamic allocation is necessary, though.

> in which case global declaration is beneficial ?
¿when do you want to access a global variable?


> it gave segmentation fault on SPOJ
I've got accepted by changing your getline() to cin>>*a>>*b;
Thanks a lot ne555
Topic archived. No new replies allowed.