Reputation: 77
The error only occurs when i try to input large data. After reading a lot of resources I found the causes of this error as follows- 1. touching the memory location we are not allowed to. 2. using free() on the memory we have not reserved or double free() a pointer. 3. initializing invalid index (such as creating arrays with negative index) I found one cause which may have caused this error in my code-
link - https://discuss.codechef.com/questions/59703/prime-number-generation-sigabrt-problem-c
which says- "May be the limit on n is quite large! You can atmost make an array of 10^8 size for online judges. That too in the heap memory ie. outside the main function. Making an array of size of even 10^7 in the main function can lead to runtime errors. Try not to use more than of the order 10^8 elements for the array.. :)
In the question you are attempting the upper limit is 10^9 You can make an array of that size. Thats why SIGABRT error which means you exceeded the memory limit."
then i modified the declaration and input part to this-
int main(void)
{
int test_cases;
scanf("%i",&test_cases);
for (int i=0; i<test_cases; i++)
{
findresult(i);
}
void findresult(int i)
{
int relations; // of the size 10^6
scanf("%i",&relations);
int* pair1 = malloc(sizeof(int)*relations);
int* pair2 = malloc(sizeof(int)*relations);
for (int j=0; j<relations; j++)
{
scanf("%i %i",&pair1[j],&pair2[j]);
}
What i got to know is that creating a big array int pairs[relations][2]
(int relations is 10^6) on stack memory causes this error and also creating such big array in main function itself will also cause the error. So, as can be seen in the code above, i created a function and allocated the memory in the heap instead, using malloc (pair1 and pair2). Still i am getting the same error. Also there is not errors in the rest of the code as it is working fine for small data. If the problem related to creating the arrays of big sizes is indeed the problem then please help me how to overcome this.
EDIT 1- Explaining the code- For every test case "i" a function "findresult" is called. For every "i" The function takes input for two integers and as per the second input "relations" it declares two integers arrays of size "relations". Then user povide input for every element of both the arrays. Then the rest of the functions just involves reading from the arrays and checking some conditions.
EDIT 2- I need to store all the data first and then perfom the condition checking which require the precense of all the data. That is why i can not do it one by one (i.e taking first elements of both the arrays then performing checks then second and so on)
Upvotes: 2
Views: 3170
Reputation: 882028
For a start, you really should check the return value of malloc
, especially if you're allocating large chunks of memory as you are.
If it cannot allocate memory, it will return NULL
which, should you then attempt to dereference, will give you undefined behaviour.
If that is the case, you will need to find a way to store your data that doesn't exhaust memory, such as using a file to store it and only loading sections of the file at a time.
You should also be checking the return value from scanf
to ensure that it's actually scanning two items, lest friends
and/or relations
may be set to some arbitrarily value.
The other thing to watch out for is that you're actually freeing that memory allocated within findresult
. You haven't shown us the entire function but failure to free the memory before returning to the loop in main
will result in a memory leak of millions of bytes (e.g., about 8MB assuming your int
type is four bytes long) every single time through that loop. That could quickly exhaust your memory depending on what value you're entered for test_cases
.
Upvotes: 1