简体   繁体   English

为什么我的程序只能在调试器中工作?

[英]Why does my program only work in the debugger?

I'm using Dr. Memory for memory debugging of a C program I wrote today. 我正在使用Dr.Memory来调试我今天编写的C程序的内存。 This program, compiled with MinGW's gcc only works when I run it from the debugger gdb, so I'm assuming its a memory error. 这个程序是用MinGW的gcc编译的,仅当我从调试器gdb运行它时才起作用,因此我假设它存在内存错误。 The results.txt file Dr. Memory returned to me is something like: Dr. Memory返回给我的result.txt文件类似:

Dr. Memory version 1.5.0 build 5 builton Aug 31 2012 16:19:51
Application cmdline: ""c:\Users\Lincoln\Desktop\USACO\gift1.exe""
Recorded 63 suppression(s) from default c:\Program Files\Dr. Memory/bin/suppress-default.txt

Error #1: UNINITIALIZED READ: reading register eflags
# 0 replace_memcmp               [d:\derek\drmemory\withwiki\trunk\drmemory\replace.c:557]
# 1 parseInputs                  [c:\Users\Lincoln\Desktop\USACO/gift1.c:55]
# 2 main                         [c:\Users\Lincoln\Desktop\USACO/gift1.c:126]
Note: @0:00:00.473 in thread 3824
Note: instruction: jnz    $0x7388a607

Error #2: UNINITIALIZED READ: reading register eax
# 0 parseInputs               [c:\Users\Lincoln\Desktop\USACO/gift1.c:55]
# 1 main                      [c:\Users\Lincoln\Desktop\USACO/gift1.c:126]
Note: @0:00:00.473 in thread 3824
Note: instruction: test   %eax %eax

Error #3: UNINITIALIZED READ: reading register esi
# 0 replace_memcmp               [d:\derek\drmemory\withwiki\trunk\drmemory\replace.c:556]
# 1 parseInputs                  [c:\Users\Lincoln\Desktop\USACO/gift1.c:55]
# 2 main                         [c:\Users\Lincoln\Desktop\USACO/gift1.c:126]
Note: @0:00:00.474 in thread 3824
Note: instruction: movzx  (%esi,%ecx,1) -> %edi

Error #4: UNADDRESSABLE ACCESS: reading 0x00000004-0x00000005 1 byte(s)
# 0 replace_memcmp               [d:\derek\drmemory\withwiki\trunk\drmemory\replace.c:556]
# 1 parseInputs                  [c:\Users\Lincoln\Desktop\USACO/gift1.c:55]
# 2 main                         [c:\Users\Lincoln\Desktop\USACO/gift1.c:126]
Note: @0:00:00.474 in thread 3824
Note: instruction: movzx  (%esi,%ecx,1) -> %edi

I have no idea how to read this, or where to start trying to fix my program. 我不知道该怎么读,或者从哪里开始尝试修复我的程序。 What do these error messages mean, and how do I fix them? 这些错误消息是什么意思,我该如何解决?

EDIT: This is my code: 编辑:这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

struct personsData {
    char * name;
    int accountMoney, receivedMoney, numAquaintances;
};

void parseInputs( FILE * fin, int NP, struct personsData * person ) {

    int i;
    for ( i = 0; i < NP; i ++ )
        fscanf( fin, "%s", person[i].name );

    char * tempName_1;
    while ( !feof( fin ) ) {

        tempName_1 = malloc ( sizeof( char ) * 15 );
        fscanf( fin, "%s", tempName_1 );

        int index = 0;
        while ( memcmp( tempName_1, person[index].name, 15 ) != 0 )
            index ++;
        free( tempName_1 );

        fscanf( fin, "%d %d", &person[index].accountMoney, &person[index].numAquaintances );

        int b;
        char * tempName_2;
        for ( b = 0; b < person[index].numAquaintances; b ++) {
            tempName_2 = malloc( sizeof( char ) * 15 );
            fscanf( fin, "%s", tempName_2 );
            i = 0;
            while ( memcmp( tempName_2, person[i].name, 15 ) != 0 )
                i ++;

            free( tempName_2 );

            person[i].receivedMoney += ( int ) floor( person[index].accountMoney / person[index].numAquaintances );
            person[index].accountMoney = floor( person[index].accountMoney / person[index].numAquaintances ) * person[index].numAquaintances;

        }
    }

}

int main() {

    FILE * fin  = fopen ("gift1.in",  "r");
    FILE * fout = fopen ("gift1.out", "w");

    int NP;
    fscanf( fin, "%d", &NP);

    struct personsData person[NP];
    int i;
    for ( i = 0; i < NP; i ++ ) {
        person[i].accountMoney = 0;
        person[i].receivedMoney = 0;
        person[i].numAquaintances = 0;
        person[i].name = ( char * ) malloc ( sizeof( char ) * 15 );
    }

    parseInputs( fin, NP, person );

    for ( i = 0; i < NP; i ++ ) {
        // print to output file (and also to console for development purposes
        fprintf( fout,  "%s %d\n", person[i].name, person[i].receivedMoney - person[i].accountMoney );
        printf(         "%s %d\n", person[i].name, person[i].receivedMoney - person[i].accountMoney );
    }

    for ( i = 0; i < NP; i ++ ) {
        free( person[i].name );
    }

    exit(0);
}

And this is the input file gift1.in: 这是输入文件gift1.in:

5
dave
laura
owen
vick
amr
dave
200 3
laura
owen
vick
owen
500 1
dave
amr
150 2
vick
owen
laura
0 2
amr
vick
vick
0 0

Are you familiar at all with x86 assembly? 您对x86汇编完全熟悉吗? If you aren't, some of these errors will be kind of hard to explain. 如果不是这样,那么其中一些错误将很难解释。 Either way, it's telling you what lines of your code are involved in the tracebacks, like the first one: 无论哪种方式,它都告诉您回溯涉及代码的哪些行,如第一个一样:

# 0 replace_memcmp               [d:\derek\drmemory\withwiki\trunk\drmemory\replace.c:557]
# 1 parseInputs                  [c:\Users\Lincoln\Desktop\USACO/gift1.c:55]
# 2 main                         [c:\Users\Lincoln\Desktop\USACO/gift1.c:126]

In general, these errors appear to indicate that you're passing uninitialized memory to memcmp() and dereferencing a NULL pointer. 通常,这些错误似乎表明您要将未初始化的内存传递给memcmp()并取消引用了NULL指针。 But, as Mitch suggests, we'll need to see the code to say for sure. 但是,正如米奇(Mitch)所建议的,我们需要确定代码才能肯定地说。

I was wondering if you read the documentation on preparing your program for Dr Memory. 我想知道您是否阅读了有关为Memory博士准备程序的文档 It looks like you have used Visual Studio, so compile with /Zi, /MD or /Mt, /Ob0, /Oy-. 看起来您已经使用过Visual Studio,因此可以使用/ Zi,/ MD或/ Mt,/ Ob0,/ Oy-进行编译。 Make sure it is 32-bit, because Dr Memory does not support 64-bit binaries. 确保它是32位的,因为Dr Memory不支持64位的二进制文件。

This should give you output with source code lines rather than machine instructions. 这应该给您输出源代码行而不是机器指令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM