【发布时间】:2013-03-01 23:07:35
【问题描述】:
在我的程序中,在某些时候,我创建了一个scanf(" %d %d", &num1, &num2);,并且不断收到 SegmentationFault。我不明白为什么......我的意思是,他们是int。我不确定是否应该为他们分配内存。
这是完整的代码。
typedef struct my_type{
int attr1;
bool attr2;
} my_type;
int main (int argc, char * const argv[]) {
int testCase, result, totalDom, numLines, num1, num2;
int aux_map_int = 0, linesScanned = 0;
bool firstTime;
scanf("%d\n %d %d", &testCase, &totalDom, &numLines);
printf("totalDom: %d; numLines: %d\n", totalDom, numLines);
std::map<int, std::vector<my_type> > my_map;
while(aux_map_int < testCase+1){
std::vector<my_type> my_vector;
firstTime=true;
while (linesScanned < numLines) {
std::cin>>num_dom>>num_next_dom; /** SEGFAULT **/
if(firstTime){
my_vector[0].attr1 = num1;
my_vector[0].attr2 = true;
firstTime=false;
}
my_vector[num1].attr1 = num2;
my_vector[num1].attr2 = false;
linesScanned++;
}
aux_map_int++;
my_map.insert(std::pair<int, std::vector<my_type> >(aux_map_int, my_vector));
}
}
【问题讨论】:
-
你能发布一个完整的小程序来展示你的问题的核心吗?
-
如果没有更多代码、示例输入等,很难判断发生了什么。
-
显示更多代码... 没有看到代码很难提供帮助
-
您可能不希望格式字符串中有这些空格。
-
如果他们是双打,那么我想知道问题是
%d而不是%lf。 (en.wikipedia.org/wiki/…)
标签: c++ segmentation-fault scanf