can't free pointer

Discussion in 'Mac Programming' started by dougphd, Jun 1, 2016.

  1. dougphd, Jun 1, 2016
    Last edited by a moderator: Jun 1, 2016

    dougphd macrumors member

    Joined:
    Apr 28, 2016
    #1
    Code:
    /
    
    typedef struct sauce
    {
        char date[10];
        float sauce;
        float open;
        int index;
    } pesto;
    
    pesto * make_instrument(pesto *etf1, pesto *etf2, int numi, int numk)
    {
        int comp,i,k, l , m;
        pesto *new_security;
         for(i = 0, k = 0; i < numi || k < numk; )
        {
        while((comp = strncmp(etf1[i].date,etf2[k].date,8))!= 0)
      {
            l = 0; m = 0;
              if (comp > 0)
            {
                k++;
            }
            elseif (comp < 0)
            {
            i++;
            }
        }
    if (comp == 0)
        {
            i++;
            k++;
        }
        }
        return(new_security);
    };
    
    void test_system()
    
    {
      int i,j, k, l,num;
      char **etf_list, *line, *file_name;
      FILE *etfi, *etfj;
        pesto *datai, *dataj , *instrument;
         instrument = (pesto*)calloc(days_of_data, sizeof(*instrument));
        datai = (pesto*)calloc(days_of_data, sizeof(*datai));
        dataj = (pesto*)calloc(days_of_data, sizeof(*dataj));
         line = (char*) calloc(200,sizeof(char));
        file_name = (char*) calloc(200,sizeof(char));
        etf_list = read_list_of_etfs();
        for (i = 0; i < num_of_etfs-1; i++)
        {
            strcpy(file_name,home);
            strcat(file_name, etf_list[i]);
            if (( etfi = fopen(file_name, "r")) == NULL)
                printf("Couldn't open %s\n", file_name);
            else
                    printf("%s ", file_name);
                fgets(line,200,etfi);
            k = 0;
            while ((fgets(line,200,etfi)) != NULL)
            {
                strtok(line,","); //symbol
                strtok(NULL,","); //D
                strcpy(datai[k].date,strtok(NULL,",")); //date
                strtok(NULL,","); //000000
                datai[k].open = atof(strtok(NULL,","));
                datai[k].index = k;
                k++;
            }
        for (j = i+1; j < num_of_etfs; j++)
            {   l= 0;
                strcpy(file_name,home);
                strcat(file_name, etf_list[j]);
                if (( etfj = fopen(file_name, "r")) == NULL)
                    printf("Couldn't open %s\n", file_name);
                    else
                    printf(" %s\n", file_name);
                fgets(line,200,etfj);
                while ((fgets(line,200,etfj)) != NULL)
                {
                    strtok(line,","); //symbol
                    strtok(NULL,","); //D
                    strcpy(dataj[l].date,strtok(NULL,",")); //date
                    strtok(NULL,","); //000000
                    dataj[l].open = atof(strtok(NULL,","));
                    dataj[l].index = l;
                    l++;
                }
                instrument = make_instrument(datai,dataj, k, l);
            //run_instrument
            //   store_result();
                fclose(etfj);
            };
            fclose(etfi);
        }
        free(line);
        free(instrument);
        free(datai);
        free(dataj);
        return;
    };
    /
    Test_system declares a variable pesto *instrument; and allocates memory instrument = (pesto*)calloc(days_of_data, sizeof(*instrument)); However when the codes gets to free(instrument); I get an error message that says the pointer wasn't allocated.

    Help, please.
     
  2. chown33, Jun 1, 2016
    Last edited: Jun 1, 2016

    chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    You have this code:
    Code:
    pesto * make_instrument(pesto *etf1, pesto *etf2, int numi, int numk)
    {
        int comp,i,k, l , m;
        pesto *new_security;
    
    Nowhere in the body of make_instrument is new_security assigned a value.

    Later in make_instrument() is this:
    Code:
        return(new_security);
    
    So this function is returning an uninitialized variable. Unassigned local variables are not guaranteed to be valid. They may contain any random junk. So you're returning a pointer that contains random junk. free() detects

    If you had compiler warnings turned on, compiling this code should warn you about returning (or using) variables before they've been assigned a value.

    What are you using to compile the code? If you're not using compiler warnings, you should be.


    You also have at least one memory leak. There may be others.
     
  3. dougphd thread starter macrumors member

    Joined:
    Apr 28, 2016
    #3
    I was just going to post that I fixed this.
    Code:
    /
    void make_instrument(pesto *etf1, pesto *etf2, int numi, int numk, pesto *new_security)
    
    {
    
        int comp,i,k, l , m;
    
       
    
        for(i = 0, k = 0; i < numi || k < numk; )
    
        {
    
          while((comp = strncmp(etf1[i].date,etf2[k].date,8))!= 0)
    
        {
    
            l = 0; m = 0;
    
              if (comp > 0)
    
            {
    
                k++;
    
            }
    
            elseif (comp < 0)
    
            {
    
            i++;
    
            }
    
        }
    
          strcpy(new_security[i].date, etf1[i].date);
    
            new_security[i].open = etf1[i].open/etf2[i].open;
    
    if (comp == 0)
    
        {
    
            i++;
    
            k++;
    
        }
    
        }
    
        return;
    
    };
    
    
    void test_system()
    
    {
    
      int i,j, k, l,num;
    
      char **etf_list, *line, *file_name;
    
        FILE *etfi, *etfj;
    
        pesto *datai, *dataj , *instrument;
    
       
    
        instrument = (pesto*)calloc(days_of_data, sizeof(*instrument));
    
       
    
        datai = (pesto*)calloc(days_of_data, sizeof(*datai));
    
        dataj = (pesto*)calloc(days_of_data, sizeof(*dataj));
    
        line = (char*) calloc(200,sizeof(char));
    
        file_name = (char*) calloc(200,sizeof(char));
    
        etf_list = read_list_of_etfs();
    
       
    
        for (i = 0; i < num_of_etfs-1; i++)
    
        {
    
            strcpy(file_name,home);
    
            strcat(file_name, etf_list[i]);
    
            if (( etfi = fopen(file_name, "r")) == NULL)
    
                printf("Couldn't open %s\n", file_name);
    
            else
    
                    printf("%s ", file_name);
    
    
                fgets(line,200,etfi);
    
            k = 0;
    
            while ((fgets(line,200,etfi)) != NULL)
    
            {
    
                strtok(line,","); //symbol
    
                strtok(NULL,","); //D
    
                strcpy(datai[k].date,strtok(NULL,",")); //date
    
                strtok(NULL,","); //000000
    
                datai[k].open = atof(strtok(NULL,","));
    
                datai[k].index = k;
    
                k++;
    
            }
    
           
    
            for (j = i+1; j < num_of_etfs; j++)
    
            {   l= 0;
    
                strcpy(file_name,home);
    
                strcat(file_name, etf_list[j]);
    
                if (( etfj = fopen(file_name, "r")) == NULL)
    
                    printf("Couldn't open %s\n", file_name);
    
                    else
    
                    printf(" %s\n", file_name);
    
                fgets(line,200,etfj);
    
                while ((fgets(line,200,etfj)) != NULL)
    
                {
    
                    strtok(line,","); //symbol
    
                    strtok(NULL,","); //D
    
                    strcpy(dataj[l].date,strtok(NULL,",")); //date
    
                    strtok(NULL,","); //000000
    
                    dataj[l].open = atof(strtok(NULL,","));
    
                    dataj[l].index = l;
    
                    l++;
    
                }
    
                make_instrument(datai,dataj, k, l, instrument);
    
            //run_instrument
    
            //   store_result();
    
            //  printf("%s %f \n", instrument[400].date, instrument[400].open);
    
                fclose(etfj);
    
            };
    
            fclose(etfi);
    
        }
    
        free(line);
    
        free(instrument);
    
        free(datai);
    
        free(dataj);
    
        return;
    
    };
    /[code]
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    The posted code still has a memory leak.
     
  5. dougphd thread starter macrumors member

    Joined:
    Apr 28, 2016
    #5
    I will look for it. I'd like to get this right.
    --- Post Merged, Jun 1, 2016 ---
    It's simply a matter of having a free for every calloc?
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    The rule is if you calloc something, you should eventually free it. When and where the free occurs will depend on how it's used. If you want an example to test your ability, look at the original posted code and tell us how many leaks you can identify, and why they're leaks. I can see at least two.

    If you can avoid calloc'ing something in the first place, then you can subsequently avoid having to free it. Some of the calloc'ed things in your posted code could be local arrays. If you don't know what that means, you should go back and study C fundamentals.
     

Share This Page