是否有更好的方法可以从C中的函数内部修改数组



我正在尝试创建一个格式化的字符串,但是我不知道为什么我无法打印我在功能中修改的全局数组。此外,奇怪的行为是我无法仅访问一个特定的全局阵列(rand_session_key(其他全局阵列的表现是正常的(除了它们的大小不同,在它们上进行了相似的操作(,我可以正确访问其值。该代码在ESP32(DOIT DEV KIT V1((带有Arduino核心(上运行,当我在计算机上运行此程序(修改一些功能等(时在内存或以错误的方式访问它,但是如果是这种情况,我不会在计算机上产生预期的输出。

我试图修改程序并使其更详细。另外,我运行了相同的代码(有一些明显的修改以使其在我的计算机上运行(,结果是预期的。

char persistent_peripheral_id[] = "FRUCTOSE96";
char rand_session_iden[7] = {''};
char rand_session_key[17] = {''};
char rand_session_channel[3] = {''};
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] = '';
    printf("Identifier : %sn", 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] = '';
    printf("Key : %sn", 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 : %sn", 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指向至少四个字符的数组,因为在将至少打印至少三位数加上字符串终端。它指向的数组rand_session_channel只有三个字符。最终的行为是不确定的。

观察到的Ub的表现与在内存中布置的全局阵列一致,因此rand_session_key立即遵循rand_session_channel,因此溢出后者意味着字符串终结器将其写入前者的位置0,使其成为空的空位细绳。但是,请注意,您不能依靠预测UB的表现,也不是在分析它们的一般情况。相反,避免锻炼UB。

目前尚不清楚您使用的是什么 random函数,因为C标准库没有参数,但是如果与您的参数指定 excrusive 上限,那么您可以将sprintf格式更改为"%02ld"。或者,将rand_session_channel的大小增加到至少4个。

最新更新