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

时间:2012-11-16 00:28:13

标签: c memory dr-memory

我正在使用Dr. Memory进行我今天写的C程序的内存调试。用MinGW的gcc编译的这个程序只有在我从调试器gdb运行它时才有效,所以我假设它有一个内存错误。结果.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

我不知道怎么读这个,或者从哪里开始尝试修复我的程序。这些错误消息意味着什么,以及如何解决它们?

编辑:这是我的代码:

#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);
}

这是输入文件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

2 个答案:

答案 0 :(得分:1)

您熟悉x86程序集吗?如果不是,其中一些错误将难以解释。无论哪种方式,它都会告诉您回溯中涉及哪些代码行,例如第一行:

# 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]

通常,这些错误似乎表明您将未初始化的内存传递给memcmp()并取消引用NULL指针。但是,正如米奇建议的那样,我们需要看到代码才能确定。

答案 1 :(得分:0)

我想知道你是否在为记忆博士准备你的程序时阅读了documentation。看起来您使用的是Visual Studio,因此使用/ Zi,/ MD或/ Mt,/ Ob0,/ Oy-进行编译。确保它是32位,因为内存博士不支持64位二进制文​​件。

这应该为您提供源代码行而不是机器指令。