Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

dougphd

macrumors member
Original poster
Apr 28, 2016
70
0
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.
 
Last edited by a moderator:
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.
 
Last edited:
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]
 
I will look for it. I'd like to get this right.
[doublepost=1464836101][/doublepost]It's simply a matter of having a free for every calloc?
 
It's simply a matter of having a free for every calloc?
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.