close

我定義了一個變數 uint8 TASK_MGR_NUM_PERIODIC_TASKS;

這個變數是從別的function extern出來的, 且是程式開始跑後才計算出來的

然後我想要用在某個struct 的array中

static per_task_status_t    per_task_status[ TASK_MGR_NUM_PERIODIC_TASKS ]; 此時compiler會跳出error

ERROR : expression must have a constant value

之後上網查得知若arrary要使用動態配置的大小, 應該用malloc, 

另外, 因為我想要用在static 變數中, 也不能直接在宣告時直接指定

否則會出現 function call is not allowed in a constant expression

而是先使用  static per_task_status_t*           per_task_status;

然後在某個start up funtion 中去initialize

per_task_status = (per_task_status_t*)malloc(TASK_MGR_NUM_PERIODIC_TASKS * sizeof(per_task_status_t));

static的變數會放在Stack當中, 不過malloc的變數會在heap中, 那這樣per_task_status會在哪??

不知道有沒有達人可以幫我解答~~~~~~~~~~~

arrow
arrow
    文章標籤
    programming C
    全站熱搜
    創作者介紹
    創作者 紅線 的頭像
    紅線

    紅線的生存日誌

    紅線 發表在 痞客邦 留言(0) 人氣()