代码运行错误,望大家指点

yogi_yi
Posts: 65
Joined: Tue May 25, 2021 11:02 am

代码运行错误,望大家指点

Postby yogi_yi » Wed Aug 04, 2021 8:22 am

下列代码运行出现如下错误
100Task 1 is running

***ERROR*** A stack overflow in task Task 1 has been detected.

void vTask1( void *pvParameters )
{
const char *pcTaskName = "Task 1 is running\r\n";
volatile unsigned long ul;
int num_nn = 100;
//num_nn++;
//printf("%c", num_nn );
/* 和大多数任务一样,该任务处于一个死循环中。 */
for( ;; ){
/* Print out the name of this task. */
//printf("%d/r/n", num_nn );
printf("%d", num_nn );
printf("%s", pcTaskName );

/* 延迟,以产生一个周期 */
vTaskDelay(200 / portTICK_PERIOD_MS);
}
}


如果代码中改为
uint8_t num_nn=100;
printf("%c", num_nn );

或者将printf("%d", num_nn ); 写在TASK1之外
则不会出现运行错误。

ESP_Gargamel
Posts: 786
Joined: Wed Nov 14, 2018 8:45 am

Re: 代码运行错误,望大家指点

Postby ESP_Gargamel » Wed Aug 04, 2021 10:01 am

vTask1 的 stack size 太小了,printf 用的栈比较深。

yogi_yi
Posts: 65
Joined: Tue May 25, 2021 11:02 am

Re: 代码运行错误,望大家指点

Postby yogi_yi » Thu Aug 05, 2021 7:45 am

我用

uint8_t num_nn=100;
printf("%c", num_nn );

代替 TASK1中的

int num_nn = 100;
printf("%d", num_nn );

可以运行 ,栈的深度差异这么大吗???

ESP_Gargamel
Posts: 786
Joined: Wed Nov 14, 2018 8:45 am

Re: 代码运行错误,望大家指点

Postby ESP_Gargamel » Wed Aug 11, 2021 2:15 am

printf("%c", num_nn ); 会被优化成 putchar。
printf("%d", num_nn ); 还是 printf,栈会比较深。

你可以想办法用 uxTaskGetStackHighWaterMark 来获取一下栈的深度差异。

Who is online

Users browsing this forum: No registered users and 75 guests