fclose and mem leack problem.

This is a discussion on fclose and mem leack problem. within the C Programming forums, part of the General Programming Boards category; Hello. I have such a code: Code: FILE *plik; if(argc > 1) plik = fopen(argv[1], "r"); if(argc <= 1 || ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    76

    fclose and mem leack problem.

    Hello.
    I have such a code:
    Code:
    FILE *plik;
            if(argc > 1)
                    plik = fopen(argv[1], "r");
            if(argc <= 1 || plik == NULL)
            {
                    plik = stdin;
            }
    and now, can i just do fclose(plik) or i have to check if plik == stdin, and if not, then fclose(plik). Would it be something wrong to do fclose(plik) when plik=stdin ?

    And i have also a question about mem leak in my program. I used valgrind to investigate it:
    ==13011==
    ==13011== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
    ==13011== malloc/free: in use at exit: 4096 bytes in 1 blocks.
    ==13011== malloc/free: 806 allocs, 805 frees, 35664 bytes allocated.
    ==13011== For counts of detected errors, rerun with: -v
    ==13011== searching for pointers to 1 not-freed blocks.
    ==13011== checked 1353972 bytes.
    ==13011==
    ==13011== 4096 bytes in 1 blocks are still reachable in loss record 1 of 1
    ==13011== at 0x3C034183: malloc (in /usr/local/lib/valgrind/vgpreload_memcheck.so)
    ==13011== by 0x3C108DAD: __smakebuf (in /lib/libc.so.5)
    ==13011== by 0x3C108D1D: __swsetup (in /lib/libc.so.5)
    ==13011== by 0x3C0FE606: __vfprintf (in /lib/libc.so.5)
    ==13011==
    ==13011== LEAK SUMMARY:
    ==13011== definitely lost: 0 bytes in 0 blocks.
    ==13011== possibly lost: 0 bytes in 0 blocks.
    ==13011== still reachable: 4096 bytes in 1 blocks.
    ==13011== suppressed: 0 bytes in 0 blocks.
    So it seems there isn't a pointer losing but still, there is one more alloc than free. I suspect that this one more alloc is made by program, because my main() declaration looks like:
    Code:
    int main(int argc, char *argv[])
    but do I have to free it somewhere or am i wrong ?
    Regards,
    apacz.

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    It doesn't make to me any sense this code

    Code:
    FILE *plik;
            if(argc > 1)
                    plik = fopen(argv[1], "r");
            if(argc <= 1 || plik == NULL)
            {
                    plik = stdin;   // <-- you are overwritting the pointer which is actually pointing to the file
            }
    The second If condition will be exceuted making the Plik pointer to be overwritten by the stdin (even thought plik is not null, that is because of OR logical operator)

    ssharish2005

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    76
    My program should be run like this: ./a.out file_name
    and now - if someone did just "./a.out" then he can input data from keyboard. But if argc > 1 (so he put file name) we try to open this file for read. If something failed (then plik == NULL) then program autmatically switch to stdin.
    Regards,
    apacz.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    > and now, can i just do fclose(plik) or i have to check if plik == stdin
    Probably falls into the "should be OK, but better to check" categories.

    > malloc/free: in use at exit: 4096 bytes in 1 blocks.
    I'm guessing this was a buffer allocated the first time you called vfprintf(), in which case there isn't much you can do about it. The pointer to that memory is probably held in some static variable which you don't have access to.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Quote Originally Posted by apacz
    My program should be run like this: ./a.out file_name
    and now - if someone did just "./a.out" then he can input data from keyboard. But if argc > 1 (so he put file name) we try to open this file for read. If something failed (then plik == NULL) then program autmatically switch to stdin.
    Regards,
    apacz.
    if that was your requirnment, the program would look something as follow

    Code:
    #include<stdio.h>
    
    int main(int argc, char **argv)
    {
        FILE *Ptr;
        char FileName[20];
        
        if(argc > 1)
        {
            Ptr = fopen(argv[1],"r");
        fclose(Ptr);
        }
        if((argc == 1) || Ptr == NULL)
        {
            Ptr = stdin;
            
            printf("Enter the file name\n?");
            fgets(FileName,20,Ptr);
     
            printf("Filename : %s",FileName);
        }
    
        getchar();
        return 0;
    }
    /*my output
    
    C:\Documents and Settings\harish\Desktop\programs>testStdinPtr.exe
    Enter the file name
    ?test.txt
    Filename : test.txt
    
    
    C:\Documents and Settings\harish\Desktop\programs>
    */
    ssharish2005
    Last edited by ssharish2005; 09-18-2006 at 02:06 PM.

  6. #6
    Registered User
    Join Date
    May 2005
    Posts
    76
    Well, my intension was to get a data from a file if it was gave while running program ("./a.out filename" situation) or to get a data from keyboard. So if there was no filename while running a program ("./a.out" situation) i set plik = stdin, and then later i just do fscanf(plik, "...", ...); But what about fclose() on such a pointer (as in my first question) ?
    Regards,
    apacz.

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    well, it is just the same code. did u find the difference in your code and my code

    ssharish2005

  8. #8
    Registered User
    Join Date
    May 2005
    Posts
    76
    Ok, thx. So there is nothing bad in fclose(ptr) when ptr == stdin ? Do you know maybe something about my mem leak problem?
    Regards,
    apacz.

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    ohhh, sorry it isn't a good idea of closing Ptr when it is pointing to stdin. which means u closing the standard input pointer, where it stops u in reading any more char from the keyboard

    ssharish2005

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21