Error in using free(*variable)

This is a discussion on Error in using free(*variable) within the C Programming forums, part of the General Programming Boards category; Hello, I am using code::blocks (10.05) for building and running c-programs on Windows-7. In some programs, function free(*variable) produces code ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    19

    Error in using free(*variable)

    Hello,

    I am using code::blocks (10.05) for building and running c-programs on Windows-7. In some programs, function free(*variable) produces code crash. I do not why? I think on 64-bit system it produces error, as perhaps C99 implementation returns 32 bit int (irrespective of cast??). Please can any one help me in this matter i.e. what is the way out on Windows-7?. Code is below:
    Code:
    #include<stdlib.h>
    #include<stdio.h>
    #include<string.h>
    #include <sys/stat.h>
    #include <dirent.h>
    #include"keyword_loader.h"
    int main(void)
    {
        int i, j, k, nfiles;
        file_kewords *ptr_files;  // structure defined in keyword_loader.h
        char *pr_path;
        if((ptr_files=(file_kewords*)calloc(FILEMAX, sizeof(file_kewords))) == NULL)
        {
            printf("\n  err in mem-allocation\n");
            getchar();
            return(0);
        }
        if((pr_path=(char*)calloc(FILENAME_MAX, sizeof(char))) == NULL)
           {
               free(ptr_files);
               printf("\n  err in mem-allocation\n");
               getchar();
               return(0);
           }
        // Calling function for getting all the filenames
        i=load_filenames(ptr_files, &nfiles, pr_path);
        if(i == 0)
        {
            printf("\n Path for all files: %s\n",pr_path);
            for(j=0; j<=nfiles-1; j++)
            {
                printf("\n Passed filename: %s",(ptr_files+j)->filename);
            }
        }
        printf("\n\n Returned value: %d",i);
        // Calling function for loading keywords from specified file
        k=read_file((ptr_files+1)->filename, pr_path);
        printf("\n ----Returned value: %d",k);
        //free(ptr_files);
        //free(pr_path);
        getchar();
        return(0);
    }
    If I remove the comments indication from last two free() function calls, code crashes.

    Thanks and regards,
    Amal.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,847
    We need all the code, not just main()

    It will be nothing to do with whether you're using a 32/64 bit compiler/OS or anything to do with C99.
    It will be a bug in your code - almost certainly.
    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.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    19
    Thanks Salem,
    The code for functions is a bit long, so I am giving here the last portion of function "read_file". Without calling this function there is no error.
    Code:
    
            if(brk_index == 3)break;
        }
        printf("\n\n  Before program end\n");
        fclose(fp_test);
        fclose(fp_read);
        free(ptr_str);
        free(path_n_file);
        printf("\n\n  Just before return\n");
        return(0);
    }
    On running the last two printf calls (in function "read_file") runs sucessfully. Error occurs only when I uncommented the free() functions in main(). Here "ptr_str"and "path_n_file" are local variables of function. Although I am once again in try to debug the function.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,847
    > free(ptr_str);
    > free(path_n_file);
    So are you trying to free the same thing twice?
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DEBUG ERROR WHEN USING free() ??
    By mugiwara528 in forum C Programming
    Replies: 14
    Last Post: 07-13-2011, 12:33 PM
  2. free function error.
    By brack in forum C Programming
    Replies: 13
    Last Post: 10-28-2010, 02:34 PM
  3. Error with 'free(pointer)'
    By dunxton in forum C Programming
    Replies: 7
    Last Post: 03-05-2009, 09:44 AM
  4. Malloc - Free giving double free or corruption error
    By andrew.bolster in forum C Programming
    Replies: 2
    Last Post: 11-02-2007, 07:22 AM
  5. error when using free
    By rotis23 in forum C Programming
    Replies: 11
    Last Post: 08-21-2002, 11:27 AM

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