Click here to Skip to main content
15,906,455 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am unable to find the proper reason for my program segmentation fault ,I don't Know the input also, in which it is getting segmentation fault, because I am submitting it to an online judge.
Please only you guys can help me ,may be you can tell the error by seeing it once.
I am new to programming.Help me ...



I was trying to solve very basic problem https://www.spoj.com/problems/CANDY/

This is the one of most solved problem on spoj
I am getting a segmentation fault when submitting the below solution.
But in Visual Studio its working fine.
I also declared variables by considering the size (sum as long long int)
because it can be large






1) Is it due to the fact that I am declaring the array inside the while loop;
should I declare that array outside of while loop so that for every test cases it uses that same array

2) Is every time loop runs(for every test cases) the new array is created, will it lead to garbage collection or compiler will automatically free the memory after every test cases (I know about dynamic memory allocation in that case we have to free memory explicitly ) can you tell me in which scope I
should declare the variables?
3)If we declare a fresh array(s) for each iteration of while loop will it lead to the garbage until main remains on the stack ?



I got above doubts because segmentation fault is regarding memory access.

What I have tried:

#include<iostream>
 using namespace std;



 int main(){

     while(1){
         int n;
         int arr[10001];
         cin>>n;
         if(n==-1)
             break;
         long long int sum=0;
         for(int i=0;i<n;i++){
             int temp;
             cin>>temp;
             sum+=temp;
             arr[i]=temp;

         }
          int mean=sum/n;
         if((sum%n)!=0){
             cout<<-1<<endl;
             continue;
             }
          int count1=0;
          for(int i=0;i<n;i++){
             if(arr[i]>mean){
                 count1+=(arr[i]-mean);

             }
          }
          cout<<count1<<endl;

     }

 }
Posted
Updated 3-Jan-19 22:21pm

1 solution

Segmentation faults happen when you try to access memory that isn't "Yours", so you need to start by looking at everywhere that uses an address or pointer.
And that includes array indexes, because arrays are in effect a pointer (in fact the name of an array is a pointer to the first element)

And the most likely reason for your problem is that n is larger than 10001 ... but you need to use the debugger to find out for sure.

And no, it's not garbage collection - unless managed, C++ doesn't use GC but expects you to be "responsible" and clean up after yourself. In this case however, a single stack allocation will be made and reused as you don't declare your array using malloc or new.
 
Share this answer
 
v3
Comments
kavinderrana121 4-Jan-19 4:28am    
if i declare a static array inside main function in a for loop that runs n times then will n arrays will be going to stay there until main function remains on stack?
OriginalGriff 4-Jan-19 4:32am    
No, it'll only be created once. And a static array isn't created on the stack, it's allocated from the memory pool. Stack data is different, it isn't in the same place every time a function is called, because it depends when the function is called!
kavinderrana121 4-Jan-19 4:30am    
If it happens then n different arrays will be created ,will it be better that i bring out my array out of that while loop?
Because we can use same array many times?
And that will be reduce the space used also?
kavinderrana121 4-Jan-19 4:35am    
sorry i am not talking about static keyword static mean here something like this

arr[100] in a for loop
OriginalGriff 4-Jan-19 4:42am    
Then be precise about what you say! :laugh:

And still no: one stack allocation will be made for the array at the beginning of the function, the loop just controls it's scope, not it's duration.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900