【问题标题】:segmentation fault in a scanf c++scanf c ++中的分段错误
【发布时间】: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


【解决方案1】:

%d 需要指向 int 的指针。根据您自己的话,您正在传递指向双打的指针。

【讨论】:

    【解决方案2】:

    更新:您的分段错误不是因为 scanf,而是因为您在无限 while 循环内的向量中插入元素。检查 aux_map_int 是否正在增加:)

     std::cin>>num_dom>>num_next_dom; /** SEGFAULT **/
    

    我假设该行应该是

     std::cin>>num1>>num2; /** SEGFAULT **/
    

    如果是这样,当您尝试在内部 while 循环中插入 my_vector 时会出现段错误:

            if(firstTime){
                my_vector[0].attr1 = num1;
                my_vector[0].attr2 = true;
                firstTime=false;
            }
    
            my_vector[num1].attr1 = num2;
            my_vector[num1].attr2 = false;
    

    在此之前,您尚未在向量中插入任何内容,并且您正在尝试从中读取。这是也会出现段错误的代码

    #include <vector>
    
    typedef struct my_type{
       int attr1;
       bool attr2;
    } my_type;
    
    int main (int argc, char * const argv[]) 
    {
        std::vector<my_type> v;
        v[0].attr1 = 1;
        v[0].attr1 = true;
    }
    

    修复上面的代码,您将学会如何解决您的问题。对不起,我不能给你答案:)

    另外,您在 std::cin 上使用 scanf 是否有特定原因?如果答案是否定的,请改用这个:

    #include <iostream>
    ....
    std::cin>>num1>>num2; 
    

    它也可以正常工作,没有任何通常的 scanf 麻烦。

    如果你必须使用 scanf,我建议你参考它的文档。您的问题是您没有使用正确的格式说明符。与其只告诉你它是什么,我更愿意you read it for yourself。

    从您发布的代码看来,您使用 scanf 的原因是为了处理空白。如果是这种情况,请使用 std::cin 并且您的空白问题已成为历史。

    【讨论】:

    • @FredericoNobredeCarvalho 更新了答案。分段错误与scanf无关:)
    • 感谢您指出其他错误,但我仍然遇到分段错误,无法弄清楚原因
    • @FriedRike 我已经更新了我的答案。虽然我知道问题出在哪里,但 StackOverflow 是关于学习的。所以虽然我没有告诉你需要做什么来修复它,但我希望我已经为你指明了正确的方向。
    【解决方案3】:

    正如@Tomek 所说。 %d 代表整数。 %f 表示浮点数 试试这个:

    double num1, num2;
    char str[128];
    scanf("%f %f", &num1, &num2);
    

    【讨论】:

    • str 是干什么用的?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多