세그먼트를 잘못 실행하면 어떻게됩니까 ??번호 인쇄 프로그램의 세그먼트 오류
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static char* exe;
void usage(void) {
printf("Usage: %s <number of integers>\n", exe);
}
int main(int argc, char** argv) {
//This program reads in n integers and outputs them/
//in reverse order. However, for some odd reason, I/
//am getting an error when I run it with no command/
//line arguments. It is supposed to display helpful/
//usage information out, but instead it segfaults??/
exe = malloc(50 * sizeof(*exe));
strncpy(exe, argv[0], 49);
if(argc != 2) {
usage();
exit(0);
}
int n = atoi(argv[1]);
int* numbers = malloc(n * sizeof(*numbers));
int i;
for(i = 0; i < n; i++) {
scanf("%d\n", &numbers[i]);
}
for(i = 9; i >= 0; i--) {
printf("%d:\t%d\n", 10 - i, numbers[i]);
}
free(numbers);
free(exe);
return 0;
}
디버거에서 segfault가 발생하는 위치는 어디입니까? 이 프로그램에 어떤 인풋을 제공합니까? – templatetypedef
'strncpy' 라인은 segfault를 발생시킵니다. 나는'./numbers 10'와 같은 것을 실행하고 있습니다. – C0deH4cker
가능한 복제본은 무엇입니까? operator do ?!] (http://stackoverflow.com/questions/7825055/what-does-the-c-operator-do) –