2013-04-05 3 views
3

좋아,일부 C 프로그램은 디버그에서는 작동하지만 릴리스에서는 작동하지 않는 이유는 무엇입니까?

그래서 여기 붙어 있습니다. 알고리즘을 기반으로 한 서클에 서있는 사람을 체계적으로 실행하는 프로그램 코드가 있지만 릴리스 모드에서 충돌하는 문제가 있습니다. 디버거 (codeblocks)를 사용하여 실행하면 코드가 제대로 실행되지만 충돌이 발생하지 않으면 코드가 제대로 실행됩니다. 온라인에서 살펴 보았지만 유일하게 발견되지 않은 변수가 있지만 선언시 변수를 즉시 설정하려고 시도했지만 문제가 해결되지 않았습니다.

누구든지 내 문제가 무엇인지 알면 도움을 주시면 감사하겠습니다.

#include<stdio.h> 
#include<stdlib.h> 
#include<string.h> 

// if the program does not work, please run in debugger mode. It will work. 

void remove_person(int** array, int arraySize, int position) 
{ 
    int i; 
    for (i = 0; i < arraySize; ++i) 
     printf("%d ", (*array)[i]); 
    printf("\n"); 

    int* temp = malloc((arraySize - 1) * sizeof(int)); // create temporary array smaller by one element 

    memmove(temp,*array,(position+1)*sizeof(int)); // copy entire array before position 

    memmove(temp+position,(*array)+(position+1),(arraySize - position)*sizeof(int)); // copy entire array after postion 

    for (i = 0; i < arraySize - 1; ++i) 
     printf("%d ", (temp)[i]); 
    printf("\n"); 

    free (*array); 
    *array = temp; 
} 


int kill(int** a, int n) 
{ 
    int pos = 0; 
    int round = 1; 
    while(n > 1) 
    { 
     pos = pos + 2 - (round % 2); 

     while(pos >= n) 
      pos = pos - n; 

     remove_person(a,n,pos); 
     n--; 

     while(pos >= n) 
      pos = pos - n; 
     round++; 
    } 
    return *a[0]; 
} 

void main() 
{ 
    int n, survivor, i; 
    int* people; 

    printf("Enter number of people for Russian Roulette: \n"); 
    scanf("%d", &n); 

    people = (int*) malloc(n*sizeof(int)); 

    for(i=0; i < n; i++) 
    { 
     people[i] = i; 
    } 

    survivor = kill(&people, n); 
    printf("The survivor is person #%d\n", survivor); 
} 
+4

'void main'은 비표준입니다. – chris

+0

단순한 목록이이 솔루션보다 예쁘다고 생각하지 않습니까? 리스트에서 원소를 제거하기 위해서는'free()'하고 그 원소의'next' 포인터를 수정하면됩니다. –

+0

@chris : 권장 도서 : http://homepage.ntlworld.com/jonathan.deboynepollard/FGA/legality-of-void-main.html. dr :'void main()'은 C99 표준에서 구현 정의 방식으로 허용됩니다. – nneonneo

답변

0

프로그램 세그먼테이션 폴트 (segfault) 내가 입력으로 4 (4 이상 또는 숫자)를 입력하면,하지만 난 그렇게 이유를 코드로 가지 않을 것이다.

그 외에도 프로그램이 잘 작동한다는 점 외에도 Windows에서 실행한다고 생각하기 때문에 결과가 표시되지 않는 것이 문제입니다.

결국 scanf("%d", &n);과 같은 것을 추가하거나 cmd.exe을 실행한다고 말하면 실행 파일이있는 디렉토리로 이동하여 거기에서 실행하십시오.

6

제목 질문에 대한 기본적인 대답 ("일부 C 프로그램은 디버그에서는 작동하지만 릴리스에서 작동하지 않는 이유는 무엇입니까?")는 정의되지 않은 동작 인을 호출 할 때 ""입니다.

memmove(temp,*array,(position+1)*sizeof(int)); // copy entire array before position 

memmove(temp+position,(*array)+(position+1),(arraySize - position)*sizeof(int)); // copy entire array after postion 

에서 여기

, 당신은 너무 많이 복사합니다. 왜보고 관찰하는 제 memmove 복사본 temp[0], temp[1], ... temp[position], 두 번째 복사본 temp[position], temp[position+1] 행, ..., temp[position+arraySize-position-1] = temp[arraySize-1] (temp[position]의 중첩을 참고). 그러나 temparraySize-1 요소에 대한 공간 만 가지고 있습니다. 보유 할 수있는 것 이상을 복사 했으므로 은 정의되지 않은 동작이됩니다.

힙을 다르게 배치했기 때문에 디버그에서는 작동하지만 릴리스 모드에서는 작동하지 않을 수 있습니다 (디버거 모드 할당자는 디버거 또는 프로파일 러에서 실행 중일 때 이와 같은 버그를 잡기 위해 추가 공간을 할당 할 수 있습니다).

관련 문제