#14931: 為甚麼記憶體區段錯誤呢?


samisthegod (aaa878787)


#include<iostream>
using namespace std;
int main()
{
int n,a[1000001];
while(cin>>n)
{
int i,sum1=0,sum2=0,ans=0;
for(i=1;i<=n;i++) cin>>a[i];
for(i=1;i<=n;i+=2) sum1+=a[i];
for(i=2;i<=n;i+=2) sum2+=a[i];
ans=max(sum1,sum2);
cout<<ans<<endl;
}
}

 

這是陣列太小的問題嗎???\\

該怎麼處理呢???

 

 

 

#30: 2% RE (SIGSEGV)

記憶體區段錯誤!
Segmentation fault (core dumped)

#31: 2% RE (SIGSEGV)

記憶體區段錯誤!
Segmentation fault (core dumped)

#32: 2% RE (SIGSEGV)

記憶體區段錯誤!
Segmentation fault (core dumped)

#33: 2% RE (SIGSEGV)

記憶體區段錯誤!
Segmentation fault (core dumped)

#34: 2% RE (SIGSEGV)

記憶體區段錯誤!
Segmentation fault (core dumped)

#35: 2% RE (SIGSEGV)

記憶體區段錯誤!
Segmentation fault (core dumped)

#36: 2% RE (SIGSEGV)

記憶體區段錯誤!
Segmentation fault (core dumped)

#37: 2% RE (SIGSEGV)

記憶體區段錯誤!
Segmentation fault (core dumped)

#38: 2% RE (SIGSEGV)

記憶體區段錯誤!
Segmentation fault (core dumped)

#39: 2% RE (SIGSEGV)

記憶體區段錯誤!
Segmentation fault (core dumped)

#40: 2% RE (SIGSEGV)

記憶體區段錯誤!
Segmentation fault (core dumped)

#41: 2% RE (SIGSEGV)

記憶體區段錯誤!
Segmentation fault (core dumped)

#42: 2% RE (SIGSEGV)

記憶體區段錯誤!
Segmentation fault (core dumped)

#43: 2% RE (SIGSEGV)

記憶體區段錯誤!
Segmentation fault (core dumped)

#44: 2% RE (SIGSEGV)

記憶體區段錯誤!
Segmentation fault (core dumped)

#45: 2% RE (SIGSEGV)

記憶體區段錯誤!
Segmentation fault (core dumped)

#46: 2% RE (SIGSEGV)

記憶體區段錯誤!
Segmentation fault (core dumped)

#47: 2% RE (SIGSEGV)

記憶體區段錯誤!
Segmentation fault (core dumped)
#14932: Re:為甚麼記憶體區段錯誤呢?


kev8067@gmail.com (卓承緯)


#include
using namespace std;
int main()
{
int n,a[1000001];
while(cin>>n)
{
int i,sum1=0,sum2=0,ans=0;
for(i=1;i<=n;i++) cin>>a[i];
for(i=1;i<=n;i+=2) sum1+=a[i];
for(i=2;i<=n;i+=2) sum2+=a[i];
ans=max(sum1,sum2);
cout<<ans<<endl;
}
}

 

這是陣列太小的問題嗎???\\

該怎麼處理呢???

 

 

 

#30: 2% RE (SIGSEGV)

記憶體區段錯誤!
Segmentation fault (core dumped)

#31: 2% RE (SIGSEGV)

記憶體區段錯誤!
Segmentation fault (core dumped)

#32: 2% RE (SIGSEGV)

記憶體區段錯誤!
Segmentation fault (core dumped)

#33: 2% RE (SIGSEGV)

記憶體區段錯誤!
Segmentation fault (core dumped)

#34: 2% RE (SIGSEGV)

記憶體區段錯誤!
Segmentation fault (core dumped)

#35: 2% RE (SIGSEGV)

記憶體區段錯誤!
Segmentation fault (core dumped)

#36: 2% RE (SIGSEGV)

記憶體區段錯誤!
Segmentation fault (core dumped)

#37: 2% RE (SIGSEGV)

記憶體區段錯誤!
Segmentation fault (core dumped)

#38: 2% RE (SIGSEGV)

記憶體區段錯誤!
Segmentation fault (core dumped)

#39: 2% RE (SIGSEGV)

記憶體區段錯誤!
Segmentation fault (core dumped)

#40: 2% RE (SIGSEGV)

記憶體區段錯誤!
Segmentation fault (core dumped)

#41: 2% RE (SIGSEGV)

記憶體區段錯誤!
Segmentation fault (core dumped)

#42: 2% RE (SIGSEGV)

記憶體區段錯誤!
Segmentation fault (core dumped)

#43: 2% RE (SIGSEGV)

記憶體區段錯誤!
Segmentation fault (core dumped)

#44: 2% RE (SIGSEGV)

記憶體區段錯誤!
Segmentation fault (core dumped)

#45: 2% RE (SIGSEGV)

記憶體區段錯誤!
Segmentation fault (core dumped)

#46: 2% RE (SIGSEGV)

記憶體區段錯誤!
Segmentation fault (core dumped)

#47: 2% RE (SIGSEGV)

記憶體區段錯誤!
Segmentation fault (core dumped)

宣布在int main內跟外 試看看結果


#14933: Re:為甚麼記憶體區段錯誤呢?


nkavengertree (LaG)


修改陣列的宣告方式

int * a = new int[1000001]

 

 
#14942: Re:為甚麼記憶體區段錯誤呢?


samisthegod (aaa878787)


修改陣列的宣告方式

int * a = new int[1000001]

 

 

這是甚麼意思???

小的剛入門看不太懂....