• 카테고리

    질문 & 답변
  • 세부 분야

    알고리즘 · 자료구조

  • 해결 여부

    미해결

std::bad_alloc 힙메모리 부족 문제

20.12.22 22:58 작성 조회수 1.57k

0

int main(){
    freopen("input.txt""rt"stdin);
    int nksum = 0max = 0;
    std::vector<inttemp(n); //Create int type vector with the size of 'n', initialize it as 0
    scanf("%d %d", &n, &k);
    for(int i=0i<n; ++i){
        //Storing input values in the integer array
        scanf("%d", &temp[i]);
    }  
    //Increment the sum of first mth numbers in a vector
    for(int i=0i<k; ++i){
        sum += temp[i]
    }
    max = sum
    //Then from mth number use special formula to calculate the sum of adjacent numbers
    for(int i=ki<n; ++i){
        sum = sum + (temp[i] - temp[i-k]);
        if(sum > maxmax = sum;
    }
    printf("%d"max);

    return 0;
}
선생님이 주신 소스코드랑 변수명만 다르고 똑같이 한거같은데
이걸로 돌리면 std::bad_alloc 힙메모리가 터졌다는 에러가
뜨면서 실행이 안되더라구요...
정답코드는 잘 돌아갑니다! 혹시 제 코드랑 선생님 소스코드랑
다른점이 무엇인지 알 수 있을까요?

답변 1

답변을 작성해보세요.

1

안녕하세요^^

std::vector<inttemp(n)

위 코드를 n값을 받고 난 후 해보세요.