You are not logged in. Please login at www.codechef.com to post your questions!

×

Please help in SIGABRT and INTSET

Can any one help me in identifying the error in this code and reason for this error SIGABRT?

Question Enormous Input test code INTSET ?

#include<bits/stdc++.h>
using namespace std;
int main()
{
 long long int n,k,i,a,co=0;
 scanf("%lld%lld",&n,&k);
 long long int arr[n];
 for(i=0;i< n;i++)
 {
  scanf("%lld",&arr[i]);
 }
 for(i=0;i< n;i++)
 {
  if(arr[i]%k==0)
        co++;
 }
 delete[] arr;
 printf("%lld\n",co);
}

asked 19 May, 19:10

sanket007's gravatar image

3★sanket007
61
accept rate: 0%

converted to question 19 May, 22:10

vijju123's gravatar image

5★vijju123
9.0k212

1

Write header file correctly

(19 May, 19:59) hruday9685★

Header is correct but it is showing error as SIGABRT

(19 May, 20:41) sanket0073★

Please try to comment such one liners.

(19 May, 22:07) vijju1235★

You are getting SIGABRT because the argument to delete[] must be a pointer to some block of memory that can be deallocated. However, your array arr is declared as a constant size array, so it does not qualify and you get undefined behaviour.
But you might say that it is not a constant size array since its size is provided during runtime. Your array arr is actually what is called a variable length array. VLAs are not a part of the C++ standard, however the gcc compiler (under which your submission was made) allows it. So you don't get a compilation error and it works, but you cannot free that memory because it is treated by delete[] as a constant size array. Hope that made sense.
The fix is to change "long long int arr[n];" to "long long int *arr = new long long int[n];". Now arr is dynamically allocated which can be freed by delete[]. Or maybe just don't free the memory, it's only competitive coding :P

link

answered 20 May, 00:37

meooow's gravatar image

6★meooow
4.5k49
accept rate: 46%

This undefined behaviour is the reason of half of my debugging troubles. Hard to figure out!

(20 May, 00:48) vijju1235★

Declare array outside the int main and try once,let me know if this doesn't work

link

answered 19 May, 22:53

hruday968's gravatar image

5★hruday968
1.4k8
accept rate: 13%

Can't believe I didn't notice that. Your main function is supposed to return an int. add return 0 at the end of the function.

link

answered 19 May, 22:59

abdullah768's gravatar image

5★abdullah768
1.1k10
accept rate: 12%

edited 19 May, 23:02

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×1,795
×141

question asked: 19 May, 19:10

question was seen: 109 times

last updated: 20 May, 00:48