memory management - What is the cause of SIGABRT error in my C code and how to overcome this? -


the error occurs when try input large data. after reading lot of resources found causes of error follows- 1. touching memory location not allowed to. 2. using free() on memory have not reserved or double free() pointer. 3. initializing invalid index (such creating arrays negative index) found 1 cause may have caused error in code-

link - https://discuss.codechef.com/questions/59703/prime-number-generation-sigabrt-problem-c

which says- "may limit on n quite large! can atmost make array of 10^8 size online judges. in heap memory ie. outside main function. making array of size of 10^7 in main function can lead runtime errors. try not use more of order 10^8 elements array.. :)

in question attempting upper limit 10^9 can make array of size. thats why sigabrt error means exceeded memory limit."

then modified declaration , input part this-

int main(void) {     int test_cases;     scanf("%i",&test_cases);      (int i=0; i<test_cases; i++)     {         findresult(i);     }  void findresult(int i) {         int relations; // of size 10^6         scanf("%i",&relations);          int* pair1 = malloc(sizeof(int)*relations);         int* pair2 = malloc(sizeof(int)*relations);         (int j=0; j<relations; j++)         {             scanf("%i %i",&pair1[j],&pair2[j]);         } 

what got know creating big array int pairs[relations][2] (int relations 10^6) on stack memory causes error , creating such big array in main function cause error. so, can seen in code above, created function , allocated memory in heap instead, using malloc (pair1 , pair2). still getting same error. there not errors in rest of code working fine small data. if problem related creating arrays of big sizes indeed problem please me how overcome this.

edit 1- explaining code- every test case "i" function "findresult" called. every "i" function takes input 2 integers , per second input "relations" declares 2 integers arrays of size "relations". user povide input every element of both arrays. rest of functions involves reading arrays , checking conditions.

edit 2- need store data first , perfom condition checking require precense of data. why can not 1 one (i.e taking first elements of both arrays performing checks second , on)

for start, should check return value of malloc, especially if you're allocating large chunks of memory are.

if cannot allocate memory, return null which, should attempt dereference, give undefined behaviour.

if case, need find way store data doesn't exhaust memory, such using file store , loading sections of file @ time.

you should checking return value scanf ensure it's scanning 2 items, lest friends and/or relations may set arbitrarily value.

the other thing watch out you're freeing memory allocated within findresult. haven't shown entire function failure free memory before returning loop in main result in memory leak of millions of bytes (e.g., 8mb assuming int type 4 bytes long) every single time through loop. exhaust memory depending on value you're entered test_cases.


Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

mapreduce - Resource manager does not transit to active state from standby -

serialization - Convert Any type in scala to Array[Byte] and back -