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會在哪??
不知道有沒有達人可以幫我解答~~~~~~~~~~~
文章標籤
全站熱搜