Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The monitored command dumped core?

Tags:

c

Well on a regular basis i get obstructed with this error , the monitored command dumped core.

Which is pretty much alien language to me, hence i cannot not possibly understand what the compiler is saying.

I looked up the internet , for what could be the reason and found out that i could be accessing index which has not been allocated memory, therefore i set on to make a simplest code possible and encounter the same error.

#include<stdio.h>
int main()
{
    int n;
    int a[100000];
    scanf("%d",&n);
    int j=0;
    for(int i=2;i<=n;i+2)
    {   

        if (i%2==0)
            {   
                 a[j]=i;
                 j+=1;
            }
     }
     return 0;
} 

But i don't understand how i could be accessing non allocated memory.

Also what could be the other reasons for the same error to occur such frequently.

like image 895
Kshitij Dhyani Avatar asked Oct 23 '18 15:10

Kshitij Dhyani


4 Answers

I think the issue could be your for loop, as in the third part you're not updating i. To update, write it as i=i+2 or i+=2.

like image 108
AnshulP10 Avatar answered Oct 11 '22 14:10

AnshulP10


Your index j gets out of bounds:

Demonstration:

#include<stdio.h>
int main()
{
  int n;
  int a[100000];
  scanf("%d", &n);
  int j = 0;
  for (int i = 2; i <= n; i + 2)
  {
    if (i % 2 == 0)
    {
      if (j > 100000)      // <<<<<<<<<<<<<<
      {
        printf("Bummer\n");
        return 1;
      }
      a[j] = i;
      j += 1;
    }
  }
  return 0;
}

Accessing an array with out of bounds results undefined behaviour (google that term).

like image 32
Jabberwocky Avatar answered Oct 11 '22 13:10

Jabberwocky


You are incrementing the value of i. You have written i+2 instead of i+=2.

#include<stdio.h>
int main()
{
    int n;
    int a[100000];
    scanf("%d",&n);
    int j=0;
    for(int i=2;i<=n;i+=2)
    {   

        if (i%2==0)
            {   
                 a[j]=i;
                 j+=1;
            }
     }
     return 0;
}
like image 34
Akash Jaiswal Avatar answered Oct 11 '22 13:10

Akash Jaiswal


You are writing i+2 ,but you have to write i+=2.

like image 24
Aryan Jain Avatar answered Oct 11 '22 15:10

Aryan Jain