conflicting types for get_date

Discussion in 'Mac Programming' started by dougphd, May 19, 2016.

  1. dougphd macrumors member

    Joined:
    Apr 28, 2016
    #1
    Huh?

    Code:
    /
    char * find_start_date( int i, int j, char **etf_list)
    
    {
    
        char *start_date1,*start_date2, *full_name, *line;
    
        FILE *etf1,*etf2;
    
        int comp, k;
    
       
    
        start_date1 = (char *)calloc(14, sizeof(char));
    
        start_date2 = (char *)calloc(14, sizeof(char));
    
        line = (char *)calloc(100, sizeof(char));
    
        full_name = (char *)calloc(100, sizeof(char));
    
    
        strcpy(full_name,home);
    
        strcat(full_name,etf_list[i]);
    
      // printf("%s\n",full_name);
    
        if ( (etf1 = fopen(full_name,"r") ) == NULL)
    
            printf("did not open %s\n",full_name);
    
       
    
        strcpy(full_name,home);
    
        strcat(full_name,etf_list[j]);
    
    
       
    
      if ((etf2 = fopen(full_name,"r")) == NULL)
    
            printf("did not open %s\n",full_name);
    
       
    
       
    
        // read first line
    
        fgets(line,100,etf1);
    
        start_date1 =  get_date(etf1);
    
        fgets(line,100,etf2);
    
        start_date2 =  get_date(etf2);
    
       
    
        fclose(etf1);
    
        fclose(etf2);
    
       
    
      if ((comp = strcmp(start_date1,start_date2)) == 0)
    
         
    
            return (start_date1);//return (etf_list[i]);
    
            elseif (comp > 0)
    
            return ("0");
    
            else
    
            return("0");
    
    
       
    
    };
    
    
    char* get_date(FILE *fp)
    
    {
    
       
    
        char  *start_date, *line;
    
    
        start_date = (char *)calloc(20, sizeof(char));
    
        line = (char *)calloc(100, sizeof(char));
    
        fgets(line,100,fp);
    
        strtok(line,",");
    
        strtok(NULL,",");
    
        strcpy(start_date,strtok(NULL,","));
    
    
        return(start_date);
    
    
    };
    
    /[code]
     
  2. chown33, May 19, 2016
    Last edited: May 19, 2016

    chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    Implicit function declarations.

    The first use of get_date() is here:
    Code:
        start_date1 =  get_date(etf1);
    
    If a function hasn't been declared yet, the compiler will implicitly declare it as a function returning an int type.

    Later (as in "lexically after the implicit declaration of get_date()"), you wrote this:
    Code:
    char* get_date(FILE *fp)
    {
      .. code here ..
    }
    Unfortunately, this conflicts with the implied declaration, so the compile complains.

    Put a declaration before your first use of get_date(). A declaration alone looks like this:
    Code:
    char* get_date(FILE *fp);
    Or move the definition of get_date() so it's lexically before the first use.


    You should probably turn on the compiler warning that tells you about implicit function declarations. Then at least you'll know why.

    Suggested search terms:
    c implicit function declaration


    EDIT
    You're leaking these calloc'ed blocks:
    Code:
        start_date1 = (char *)calloc(14, sizeof(char));
        start_date2 = (char *)calloc(14, sizeof(char));
    
    The leak occurs when you assign values to those ptrs without first freeing the blocks they're pointing to.

    This is also a leak in get_date:
    Code:
        line = (char *)calloc(100, sizeof(char));
    
     
  3. dougphd thread starter macrumors member

    Joined:
    Apr 28, 2016
    #3
    Thanks. Shouldn't the complier default be to have the warning on?
    --- Post Merged, May 19, 2016 ---
    I freed some pointers (see next post) thread but if I free start_date1, I can't return it. If I declare it as start_date1[14] does that create a leak?
     
  4. mfram macrumors 65816

    Joined:
    Jan 23, 2010
    Location:
    San Diego, CA USA
    #4
    The thing to remember is that every allocation needs to eventually get freed. In a program that does something and then quickly exits, it doesn't really cause a problem. But it's a bad habit that will eventually come back to haunt you as you write more complex code.

    In both functions you have shown us, the variable "line" seems to only be used within the scope of those functions. As such, it should be declared as a local variable "char line[100];" Then you don't need to worry about freeing it later. That space is allocated from the stack where you don't have to worry about freeing it. But that memory may NOT be accessed by any other function. For example, in the get_date() function, the start_date pointer is handed by to the caller. Therefore, the memory that start_date refers to may not be on the stack in that function.

    Any memory where you using some kind of alloc function is allocated from the heap and must be explicitly freed by your program at some point. That presents extra difficulties. That's why the get_date() function is problematic. One the one hand, it's nice that you are trying to abstract that logic into a smaller piece away from the larger function. But the complication is that you need to pass a string back. Which means you need to allocate it on the heap and eventually free the memory in the calling function. That extra complexity may not be worth the effort to separate out the logic. Instead for that type of function, I would pass the buffer in.

    Code:
    void get_date(FILE *fp, char *start_date)
    {
       char line[100];
    
       fgets(line,sizeof(line),fp);
       strtok(line,",");
       strtok(NULL,",");
       strcpy(start_date,strtok(NULL,","));
    }
    
    Notice in this function, get_date() isn't in charge of the output buffer. It's relying on the caller to pass in a buffer. Whether the calling function uses the heap or the stack is not relevant to get_date(). The line array will not leak because it's on the stack.
     

Share This Page