2015-09-09 124 views
0

在返回char的函數中,必須動態分配內存。但是,我們可以釋放內存使用free()後返回一個值回調用函數?在函數中返回值後,我們可以使用free()嗎?

//程序打印今天和過去三天的天氣數據結構中的

#include <stdio.h> 
#include <string.h> 
#include <time.h> 
#include <stdlib.h> 

struct weather 
{ 
    char *date; 
    int month; 
    int day; 
    int year; 
    unsigned int h_temp; 
    unsigned int l_temp; 
    int max_wind_speed; 
    int preciption; 
    char notes [80]; 
}; 

char *chrdate(struct weather *wdt, int loop) 
{ 
    char *stdate = (char*)calloc(11,sizeof(char)); 
    sprintf(stdate, "%d/%d/%d", (wdt+loop)->day,(wdt+loop)->month,(wdt+loop)->year); 
    return stdate; 
    free(stdate); 
} 

void prev_date(int loop, struct weather *pdate) 
{ 
    int lmonth = (pdate+(loop-1))->month-1; //assigning previous day's month to local variable 

    if (((pdate+(loop-1))->day == 1) && ((pdate+(loop-1))->month == 1)) 
    { 
     (pdate+loop)->year = (pdate+(loop-1))->year-1 ; 

     (pdate+loop)->month = 12; 

     (pdate+loop)->day = 31; 
    } 
    else if (((pdate+(loop-1))->day == 1) && ((pdate+(loop-1))->month != 1)) 
    { 
     (pdate+loop)->year = (pdate+(loop-1))->year ; 
     (pdate+loop)->month = (pdate+(loop-1))->month-1 ; 

     //assigned month as per struct tm 
     if ((lmonth == 4) || (lmonth == 6) || (lmonth == 9) || (lmonth == 11)) 
     { 
      (pdate+(loop))->day = 30; //assigning 30 days for respective months 
     } 
     //checking for leap year and assigning days for february 
     else if (lmonth == 2) 
     { 
      if ((pdate+(loop-1))->year % 4 == 0) 
      { 
       (pdate+(loop))->day = 29; 
      } 
      else 
      { 
       (pdate+(loop))->day = 28; 
      } 
     } 
     //assigning days for rest of the months 
     else 
     { 
     (pdate+(loop))->day = 31; 
     } 
    } 
    else if ( (pdate+(loop-1))->day != 1) 
    { 
     (pdate+loop)->year = (pdate+(loop-1))->year ; 
     (pdate+loop)->month = (pdate+(loop-1))->month; 
     (pdate+loop)->day = (pdate+(loop-1))->day-1; 
    } 
} 

void collect_data (struct weather *pinfo) 
{ 
    int loop; 
    char yes_no[2]; 

    time_t curtime; //declaring time variable 

    //storing current system time in the time variable 
    time(&curtime); 

    //storing current time to time structure 
    struct tm * wdate = localtime (&curtime); 


    for (loop=0;loop<4;loop++) 
    { 
      if (loop == 0) 
      { 
       (pinfo+loop)->day = wdate->tm_mday; 
       (pinfo+loop)->month = wdate->tm_mon+1; 
       (pinfo+loop)->year = wdate->tm_year+1900;    ; 
      } 
      else 
      { 
      prev_date(loop,pinfo); 
      } 

      (pinfo+loop)->date = chrdate(pinfo, loop); 

      if (loop == 0) 
      { 
       printf("Today's weather details\n"); 
       printf("-----------------------\n"); 
      } 
      else 
      { 
       printf("\n\nEnter weather data for %s\n",(pinfo+loop)->date); 
       printf("------------------------------------"); 
      } 

      printf("\nEnter the high temperature of the day:"); 
      scanf("\n%d",&(pinfo+loop)->h_temp); 
      printf("\nEnter the low temperature of the day:"); 
      scanf("\n%d",&(pinfo+loop)->l_temp); 
      printf("\nEnter the maximum wind speed of the day:"); 
      scanf("\n%d",&(pinfo+loop)->max_wind_speed); 
      printf("\nEnter the perciption of the day:"); 
      scanf("\n%d",&(pinfo+loop)->preciption); 
      printf("\nDo you have any notes about the weather of the day (y/n):"); 
      scanf("\n%[^\n]s",yes_no); 

      if (strcmp(yes_no,"y")==0) 
      { 
       printf("\nNotes (Max Characters to be used is 80 incl. spaces):\n"); 
       scanf("\n%79[^\n]s",(pinfo+loop)->notes); 
      } 
      else 
      { 
      printf("Notes are blank. Processing save..."); 
      } 
    } 
} 

int main() 
{ 
    struct weather info [4]; 
    int loop; 
    collect_data(info); 

    for (loop = 0; loop<4; loop++) 
    { 
    printf("%s\n",info[loop].date); 
    } 

    return 0; 
} 
+0

發佈您的代碼。 – LPs

+0

只要沒有調用'free()',分配的內存就不會被釋放。直到流程執行結束。 –

+1

返回值後,您無法執行任何操作*。代碼無法訪問,永遠不會執行。 – EJP

回答

2

你不能做到這一點

char *chrdate(struct weather *wdt, int loop) 
{ 
    char *stdate = (char*)calloc(11,sizeof(char)); 
    sprintf(stdate, "%d/%d/%d", (wdt+loop)->day,(wdt+loop)->month,(wdt+loop)->year); 
    return stdate; // your function execution end here 
    free(stdate); // the execution won't reach here 
} 

chrdate返回一個指向內存已分配和分配它到

(pinfo+loop)->date = chrdate(pinfo, loop); 

你不想free()它因爲你會在下一次使用它。

for (loop = 0; loop<4; loop++) 
{ 
    printf("%s\n",info[loop].date); // still use it here 
} 

而應該在完成使用該內存時調用free((pinfo+loop)->date)

+0

我該怎麼做才能在這裏釋放記憶? –

+1

@KiranCK寫一個單獨的函數,目的是清理混亂。但最好將分配留給調用者。或者更好的是,根本不要使用動態分配,因爲您發佈的代碼中絕對不需要它。 – Lundin

相關問題