简体   繁体   English

在C程序中找不到内存泄漏

[英]Can't find the memory leak in C program

I am trying to write my first project in C and have some problems with the memory leak. 我试图用C编写我的第一个项目,并且内存泄漏有一些问题。 I can't find the mistake, but I assume something's wrong with the do-while loop. 我找不到错误,但是我认为do-while循环出了点问题。 The program compiles, but when I try to open it, I get a segmentation fault error. 该程序可以编译,但是当我尝试打开它时,出现了分段错误错误。 What am I doing wrong? 我究竟做错了什么?

The file I am opening in my program is a .txt with the sequence of chars, for example: dvorndvl. 我在程序中打开的文件是一个.txt文件,该文件带有字符序列,例如:dvorndvl。

My aim is: I open the program with argument, it opens the file named argv[1] and writes the sequence of chars from the file to the array. 我的目标是:我使用参数打开程序,它打开名为argv [1]的文件,并将字符序列从文件写入数组。

Here's my code: 这是我的代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"file1.h"
#include"file2.h"

void bye(){
    puts("See you!");
}

int main(int argc, char *argv[]) {
    char a[30];
    int i, x;

    printf("-----SOME TEXT-----");

    FILE *f;
    f = fopen(argv[1], "r");

    if (f = NULL) {
        printf("User doesn't exist.\n");
        exit(1);
    }
    else{
        do{
            a[i] = getc(f);
            i++;
        } while(a[i]!=EOF);

        char b[30];
        printf("Password? ");
        scanf("%c", b);

        if(strcmp(a,b) == 0){
            printf("\nHi, %s!\n", argv[1]);
            printf("What do you want to do?");
            printf("1. Turn the devices on/off. \n");
            printf("2. Change my password. \n");
            printf("3. -EXIT-\n");

            switch(x) {
                case 1:
                    devices(); break; //in file1
                case 2:
                    encrypt(argv[1]); break;//in file2
                case 3:
                    atexit(bye); break;
            }
        }
        else
            printf("Password is incorrect\n");
    }
    fclose(f);
    return 0;
}

I don't have 50 reputation to give you only a comment :( but I guess a handler to *f is a issue. You don't check if 'f' is a NULL but you are assign f to NULL. 我没有50的声誉,只能给您一个评论:(但是我想* f的处理程序是个问题。您不检查'f'是否为NULL,而是将f赋给NULL。

  if (f=NULL) {
     printf("User doesn't exist.\n");
     exit(1);

should be: 应该:

  if (f==NULL) {
         printf("User doesn't exist.\n");
         exit(1);

good practice is to always check if NULL is equal f : 好的做法是始终检查NULL是否等于f:

   if (NULL == f) {
         printf("User doesn't exist.\n");
         exit(1);

in that case if you make mistake, program will not compile :) 在这种情况下,如果您输入错误,程序将无法编译:)

there are number or minor errors here for first 首先有数字或小错误

int i, x; i doesnt have a value so you cant raise it with i++ also if (f = NULL) { should be changed to if (f == NULL) { i不必须的值,以便你不能与提高它i++if (f = NULL) {应改为if (f == NULL) {

char a[30]; and getc are pretty risky you should use a temp string and malloc an array to fit your needs and switch from getc to fgetc getc很有风险,您应该使用临时字符串并malloc一个满足您需要的数组,然后从getc切换到fgetc

The advice to reverse the comparison and have (NULL == f) is both irrelevant and bad. 反向比较并具有(NULL == f)的建议既无关紧要又不好。

The real problem is that you are compiling your code wrong and that probably stems from using bad resources for learning. 真正的问题是您编译的代码错误,可能是由于使用了不良的资源进行学习。

I suspect you are using gcc and just passing the target file as an argument. 我怀疑您正在使用gcc并将目标文件作为参数传递。

Instead, you should pass -Wall and possibly other flags, like this: 相反,您应该传递-Wall并可能传递其他标志,如下所示:

$ gcc -Wall -Wextra crap.c 
crap.c: In function ‘main’:
crap.c:18:5: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
     if (f = NULL) {
     ^~

As you can see the compiler spots the unintended assignment no problem, you just have to ask it. 如您所见,编译器发现了意外分配,没问题,您只需要问一下。 It can also spot a myriad of other problems, which you know ask for with the aforementioned flag. 它还可以发现许多其他问题,您知道使用上述标志可以解决这些问题。

Also note how reversing the comparison does not help if 2 variables are at play, eg switching if (foo = bar) to if (bar = foo) does not help you whatsoever. 还要注意,如果有两个变量在起作用,那么反向比较将无济于事,例如,将if(foo = bar)切换为if(bar = foo)毫无帮助。 On the other hand asking the compiler to warn you about problems catches the case. 另一方面,要求编译器警告您有关问题的情况。

If you are using a different compiler, it most certainly has a way to enable warnings. 如果您使用其他编译器,则肯定可以使用一种方法来启用警告。 For the sake of argument, if you got one which cannot do basic analysis, stop using it. 为了争辩,如果您得到了不能进行基本分析的功能,请停止使用它。 I strongly advise you DON'T if (NULL == f) because that's against most commonly used style and as noted above it does not help with anything. 我强烈建议您不要使用(NULL == f),因为这违反了最常用的样式,如上所述,它对任何事情都没有帮助。

The code is still pretty buggy, beyond what the compiler can easily spot. 该代码仍然存在很多错误,超出了编译器可以轻易发现的范围。 I may get around to annotating it later. 我稍后可能会注释它。

A couple of things are wrong here: 这里有几处错误:

 if (f = NULL) {

You are changing f to be NULL , not testing it against NULL . 您将f更改为NULL ,而不是针对NULL对其进行测试。 Use == . 使用== To prevent this kind of error in the future, put the r-value on the left hand side, like so: 为避免将来发生此类错误,请将r值放在左侧,如下所示:

 NULL == f

Next: 下一个:

char b[30];
scanf("%c", b);

If you were going to use scanf to read the string, you need to use %s format specifier, not %c , which is for single characters. 如果要使用scanf读取字符串,则需要使用%s格式说明符,而不是%c ,它用于单个字符。 I'd not use scanf at all though; 我不会使用scanf use fgets instead because it's safer. 请改用fgets因为它更安全。

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

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