本文介绍了Serial.println 更改函数的返回值(Arduino)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在经历一个非常简单的函数的一些非常奇怪的行为:一个整数被传递给函数,并且根据它的值将返回一个特定于该整数的无符号字符.

I've been experiencing some very odd behaviour from what should be a very simple function: an integer is passed to the function, and depending on its value will return an unsigned char specific to that integer.

我一直遇到一个问题,它会返回与硬编码到函数中的值不同的值.我不得不多次重写代码才能让它停止发生,但我找到了一种每次都重新创建它的方法来说明不应该改变返回值的事情是如何做的.

I kept experiencing a problem in which it would return a different value to those that are hard coded into the function. I've had to rewrite the code a number of times to get it to stop happening, but I have found a way to recreate it every time to illustrate how something that shouldn't change the return value is doing.

下面是一个示例草图来说明:

Below is an example sketch to illustrate:

unsigned char* brokenFunction(int id)
{
    switch (id)
    {
        case 0:
        {
            //Serial.println("Break it...");
            unsigned char retval[8] = {
                0b01110,
                0b11011,
                0b10001,
                0b10001,
                0b10001,
                0b10001,
                0b10001,
                0b11111
            };

            return retval;
        }
        break;

        case 5:
        {
            unsigned char retval[8] = {
                0b01110,
                0b11011,
                0b11111,
                0b11111,
                0b11111,
                0b11111,
                0b11111,
                0b11111
            };

            return retval;
        }
        break;
    }
}

void setup()
{
    Serial.begin(9600);
    unsigned char* array = brokenFunction(0);
    Serial.println(*array);
}

void loop()
{

}

第 7 行是对当前被注释掉的 Serial.println 的调用.如果在注释掉这一行的情况下在 Arduino 上运行草图,setup 函数中的 Serial.println 调用将返回值 14(这是正确的).

On line 7 is a call to Serial.println which is currently commented out. If you run the sketch on your Arduino with this line commented out, the Serial.println call in the setup function will return the value 14 (which is correct).

但是,如果您取消第 7 行的注释,setup 函数中的 Serial.println 调用现在将返回看似随机的值;对于我的最后一次测试,它是值 91;请参阅下面的串行窗口日志:

If, however, you uncomment line 7, the Serial.println call in the setup function will now return what seems like a random value; for my last test it was the value 91; see the serial window log below:

14打破它...91

如您所见,我的第一次尝试返回了 14,但随后尝试使用额外的 Serial.println 调用导致返回值被更改.

As you can see, my first attempt returned 14, but the subsequent attempt with the extra Serial.println call resulted in the return value being changed.

有人知道这里可能出了什么问题吗?我做了其他一些事情也设法复制了这一点,但这是最简单的例子.这几乎就像,如果函数需要更长的时间才能完成,返回值正在以某种方式被破坏.

Does anyone have any idea what may be going wrong here? There were other things I did that managed to replicate this too, but this is the most simple one to use as an example. It's almost as if, if the function is taking a bit longer to finish that the return value is being corrupted some how.

任何帮助或想法将不胜感激.

Any help or ideas would be much appreciated.

推荐答案

您正在从函数 brokenFunction(int id) 返回一个本地数组,该数组在您返回时停止存在,从而导致未定义的行为就这一点.

You are returning a local array from the function brokenFunction(int id), which stops existing when you return, causing undefined behavior from that point on.

这篇关于Serial.println 更改函数的返回值(Arduino)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 06:51