该程序应打印Fibonacci系列的前50个数字。但只有前47个打印正确,后三个打印不正确。
/*FreeRTOS.org includes.*/
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "stdint.h"
#include "basic_io.h"
static void vSenderTask( void *pvParameters );
static void vReceiverTask( void *pvParameters );
int long resultado;
xQueueHandle xQueue;
int main( void )
{
xQueue = xQueueCreate( 10, sizeof( long ) );
if( xQueue != NULL )
{
xTaskCreate( vSenderTask, "Sender1", 240, ( long * ) resultado, 2, NULL );
xTaskCreate( vReceiverTask, "Receiver", 240, ( long * ) resultado, 1, NULL );
vTaskStartScheduler();
}
else
{
}
for( ;; );
return 0;
}
/*---------------------------------------------------------------------------------------------*/
static void vSenderTask( void *pvParameters )
{
portBASE_TYPE xStatus;
const portTickType xTicksToWait = 1000 / portTICK_RATE_MS;
long s =1;
long r=0;
int k=1;
uint64_t resultado;
resultado = ( long ) pvParameters;
for ( ;; ){
for (k; k<=61; k++) {
resultado= s+r;
s=r;
r=resultado;
xStatus = xQueueSendToBack( xQueue, &resultado, xTicksToWait );
}
if( xStatus != pdPASS )
{
vPrintString( "Could not send to the queue.\n" );
}
}
}
/*---------------------------------------------------------------------------------------------*/
static void vReceiverTask( void *pvParameters )
{
uint64_t resultado;
portBASE_TYPE xStatus;
for( ;; )
{
if( uxQueueMessagesWaiting( xQueue ) != 10 )
{
vPrintString( "Queue should have been full!\n" );
}
xStatus = xQueueReceive( xQueue, &resultado , 0 );
if( xStatus == pdPASS )
{
vPrintStringAndNumber( "Received = ", resultado );
}
else
{
vPrintString( "Could not receive from the queue.\r\n" );
}
}
}
/*---------------------------------------------------------------------------------------------*/
void vApplicationMallocFailedHook( void )
{
/* This function will only be called if an API call to create a task, queue
or semaphore fails because there is too little heap RAM remaining. */
for( ;; );
}
/*-----------------------------------------------------------*/
void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName )
{
/*This function will only be called if a task overflows its stack. Note
that stack overflow checking does slow down the context switch
implementation. */
for( ;; );
}
/*-----------------------------------------------------------*/
void vApplicationIdleHook( void )
{
/* This example does not use the idle hook to perform any processing. */
}
/*-----------------------------------------------------------*/
void vApplicationTickHook( void )
{
/* This example does not use the tick hook to perform any processing. */
}
输出结果为:
Received = 1
Received = 1
Received = 2
Received = 3
Received = 5
Received = 8
Received = 13
Received = 21
Received = 34
Received = 55
Received = 89
Received = 144
Received = 233
Received = 377
Received = 610
Received = 987
Received = 1597
Received = 2584
Received = 4181
Received = 6765
Received = 10946
Received = 17711
Received = 28657
Received = 46368
Received = 75025
Received = 121393
Received = 196418
Received = 317811
Received = 514229
Received = 832040
Received = 1346269
Received = 2178309
Received = 3524578
Received = 5702887
Received = 9227465
Received = 14930352
Received = 24157817
Received = 39088169
Received = 63245986
Received = 102334155
Received = 165580141
Received = 267914296
Received = 433494437
Received = 701408733
Received = 1134903170
Received = 1836311903
Received = 2971215073
Received = 512559680
Received = 3483774753
Received = 3996334433
答案 0 :(得分:0)
在您的系统上,long
为32位。它可以容纳的最大正整数是4294967295。
但1836311903 + 2971215073 = 4807526976,这是更大,所以它溢出,你得到512559680(这是4807526976 - 4294967296)。如果你想要超过第47个Fibonacci数,你需要更大的数据类型或进行多精度算术。