좋아,일부 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);
}
'void main'은 비표준입니다. – chris
단순한 목록이이 솔루션보다 예쁘다고 생각하지 않습니까? 리스트에서 원소를 제거하기 위해서는'free()'하고 그 원소의'next' 포인터를 수정하면됩니다. –
@chris : 권장 도서 : http://homepage.ntlworld.com/jonathan.deboynepollard/FGA/legality-of-void-main.html. dr :'void main()'은 C99 표준에서 구현 정의 방식으로 허용됩니다. – nneonneo