【问题标题】:Debugging a C program using Structs使用 Structs 调试 C 程序
【发布时间】:2012-06-09 18:54:03
【问题描述】:

我正在尝试编写一个程序,对速配信息进行排序,以便在我的计算机科学 1 课程中获得额外学分;我遇到了一个奇怪的问题。程序在运行循环两次后遇到一系列嵌套循环时崩溃(在调用调试函数的地方)。函数传输数据;仅设计用于从文件中获取数据并将其移动到结构中,以便稍后由尚未编写的函数进行处理。我这辈子都想不通,所以非常欢迎任何建议,下面我已经放置了代码,下面是我正在使用的测试数据。

caData transferData(fData * fileData, caData * workingData, int numCouples)
{
    int i = 0, j = 0, k = 0;
    workingData->maData = (struct mData*)malloc(sizeof(mData)*(numCouples*2));//Create Array of Match Data with cells equal to double the number of couples
    workingData->maData->couData = (struct cData*)malloc(sizeof(cData)*numCouples);//Create a Couple structure array with a number of cells equal to the number of couples.
    ///TODO:
    //Read in Names
    for(i = 0; i < (numCouples * 2); i++)//Rotate through Matches
    {
        if(i < numCouples)//Men First
        {
            debug();
            for(j = 0; j < numCouples; j++)//Scan in First half of Couples
            {
                for(k = 0; k < numCouples; k++)//Scan In Male Names
                {
                    fscanf(fileData->inputData, "%s", workingData->maData[i].couData[j].guyName);
                }

                for(k = 0; k < numCouples; k++)//Scan In Female Names
                {
                    fscanf(fileData->inputData, "%s", workingData->maData[i].couData[j].girlName);
                }
            }
        }
        else//Now the women
        {
           for(j = 0; j < numCouples; j++)//Scan in First half of Couples
            {
                for(k = 0; k < numCouples; k++)//Scan In Female Names
                {
                    fscanf(fileData->inputData, "%s", workingData->maData[i].couData[j].girlName);
                }

                for(k = 0; k < numCouples; k++)//Scan In male Names
                {
                    fscanf(fileData->inputData, "%s", workingData->maData[i].couData[j].guyName);
                }
            }
        }
    }
    //Read In Scores
    //Compute differances

}

编辑:将代码压缩到错误的位置,即调用调试函数的位置;它运行两次,然后崩溃。它返回一个 -1073741819 (0xC0000005)。

【问题讨论】:

  • 让陌生人通过检查发现代码中的错误是没有效率的。您应该使用调试器或打印语句来识别(或至少隔离)问题,然后返回更具体的问题(一旦您将其缩小到 10 行 test-case)。
  • 您需要 1. 告诉我们是哪一行导致崩溃,2. 告诉我们错误消息,以及 3. 将大量代码归结为显示问题的小型代表性示例。
  • 抱歉,我发布了整个代码以防止混淆;我将尝试重新格式化上述帖子。
  • Doesn't compile on visual studio for me...error C2027: use of undefined type 'main::caData' 如果我是你,我不会使用 typedef 结构来定义结构。 C++ 接受 struct foo {};很好。
  • @CJohnson OP 正在使用 C。在 C 中,typedef 是必需的。

标签: c file pointers structure


【解决方案1】:

fscanf() 接受 FILE* 参数,您已将 fData* 传递给它。除非那是 FILE* 的别名,否则它不会做任何明智的事情,如果它是类型别名 (typedef),为什么要以这种方式混淆它?

您正在分配传递给函数的某些结构的元素。如果不显示您传递的内容和结构的定义,则无法验证所示代码的正确性,但使用fscanf() 分配字符串时最常见的错误是溢出缓冲区或未分配首先缓冲。在这种情况下,我将不得不询问 guynamegirlname 成员是否是足够长度的数组。如果它们只是指针,那么您需要为数据分配空间。您还可以修改格式说明符以确保无法将过长的字符串复制到这些成员。

如果调用者传入workingData,则调用者应负责分配任何必要的数据。在本地分配动态内存并将其传回给调用者是不好的做法 - 它提出了一个问题,即内存将在何时何地被释放并要求内存韭菜。

最后,调试此代码的最简单、最快和最好的方法是在符号调试器中单步调试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-12
    • 2014-07-25
    • 2020-12-04
    相关资源
    最近更新 更多