简体   繁体   中英

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. I can't find the mistake, but I assume something's wrong with the do-while loop. 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.

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.

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.

  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 :

   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) {

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

The advice to reverse the comparison and have (NULL == f) is both irrelevant and bad.

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.

Instead, you should pass -Wall and possibly other flags, like this:

$ 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. 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.

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 . Use == . To prevent this kind of error in the future, put the r-value on the left hand side, like so:

 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. I'd not use scanf at all though; use fgets instead because it's safer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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