c - 有没有更好的方法从 C 函数内修改数组?

标签 c arduino esp32

我正在尝试创建一个格式化字符串,但是我不知道为什么我无法打印我在函数内修改的全局数组。而且奇怪的行为是我无法仅访问特定的全局数组(rand_session_key)的其余部分其他全局数组的行为正常(除了它们的大小不同之外,正在对它们进行类似的操作)并且我可以正确访问它们的值。该代码在 esp32(DOIT Dev Kit V1)(带有 Arduino-Core)上运行,当我在计算机上运行该程序(修改一些功能等)时,结果就是我所期望的,我认为我重叠了字符在内存中或以错误的方式访问它,但如果是这种情况,我就不会在我的计算机上产生预期的输出。

我尝试修改我的程序并使其更加冗长。我还运行了相同的代码(进行了一些明显的修改以使其在我的计算机上运行),并且结果符合预期。

char persistent_peripheral_id[] = "FRUCTOSE96";
char rand_session_iden[7] = {'\0'};
char rand_session_key[17] = {'\0'};
char rand_session_channel[3] = {'\0'};

char *generate_random_session_identifier(char *rand_session_iden_local)
{
    srand(time(NULL));
    int counter = 0;
    for (counter = 0; counter < 6; counter++)
        *(rand_session_iden_local + counter) = (random(10) % ('~' - ' ')) + 'k';
    rand_session_iden_local[counter] = '\0';
    printf("Identifier : %s\n", rand_session_iden); //acessing global defintion of array everything is good until here
    return &rand_session_iden_local[0];
}

char *generate_random_session_key(char *rand_session_key_local)
{
    srand(time(NULL));
    int counter = 0;
    for (counter = 0; counter < 16; counter++)
        *(rand_session_key_local + counter) = (random(10) % ('~' - ' ')) + 'b';
    rand_session_key_local[counter] = '\0';
    printf("Key : %s\n", rand_session_key);//acessing global defintion of array everything is good until here
    return &rand_session_key_local[0];
}

char *generate_random_session_channel(char *rand_session_channel_local)
{
    srand(time(NULL));
    int channel_value = random(100);
    sprintf(rand_session_channel_local, "%03ld", channel_value);
    printf("Channel : %s\n", rand_session_channel);//acessing global defintion of array everything is good until here
    return &rand_session_channel_local[0];
}

void begin_exchange_package()
{
    //If this does not works here (observe rand_session_key) , it will not work for sprintf also ??
    printf("\n %s-%s-%s-%s \n", (char *)persistent_peripheral_id,
           generate_random_session_identifier(rand_session_iden),
           generate_random_session_key(rand_session_key),
           generate_random_session_channel(rand_session_channel));

    //Notice it prints here ????
    printf("\n %s \n",generate_random_session_key(rand_session_key));
    Serial.println("Done");


    //sprintf((char *)plain_text_package, "{\"p\":\"%s\",\"r\":\"%s\",\"k\":\"%s\",\"c\":\"%s\"}", (char *)persistent_peripheral_id,(char *)rand_session_iden, (char *)rand_session_key , (char *)rand_session_channel);

}

void setup()
{
    Serial.begin(115200);
    begin_exchange_package();
}

void loop()
{
}

输出是 果糖96-tnltkp--094 我期望在哪里打印所有 4 个数组?但它确实单独打印,我的数组是否以错误的方式终止?此外,分配随机字符的逻辑将始终产生可打印的 ASCII 字符(我从 esp32 网站上的论坛中了解到这一点)

最佳答案

这段代码...

    sprintf(rand_session_channel_local, "%03ld", channel_value);

...需要rand_session_channel_local指向一个至少包含四个字符的数组,因为at将打印至少三个数字加上一个字符串终止符。它指向的数组 rand_session_channel 只有三个字符长。由此产生的行为是未定义的。

观察到的 UB 表现与内存中布局的全局数组一致,因此 rand_session_key 紧跟在 rand_session_channel 之后,后者溢出意味着字符串终止符被写入前者的位置 0,使其成为空字符串。但请注意,您不能依赖于预测 UB 的表现,而且通常分析它们也没有多大用处。相反,请避免使用 UB。

不清楚您正在使用什么random函数,因为C标准库不带参数,但如果您的参数指定独占上限,那么您只需将 sprintf 格式更改为 "%02ld" 即可。或者,将 rand_session_channel 的大小增加到至少 4。

关于c - 有没有更好的方法从 C 函数内修改数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56871593/

相关文章:

android - Android 手机上的电话铃声响起时在后台启动应用程序

c - 如何通过esp32 rmt Ringbuffer接收长数据帧

c - 在启用换行的情况下将终端光标返回到行首

可以通过 O(r * c) 改进计算位图中的连续区域吗?

c - 使用整个 unicode 库的 LZW 压缩

linux - 在 Linux 上开始使用 arduino

Arduino - 将 HEX 转换为 RGB 的奇怪行为

通过循环计算无法正常工作的行数

arduino - Esp32cam 无法连接到 ESP32 : Timed out waiting for packet header

c++ - 如何在一个属性中动态存储基本类型?