【问题标题】:array assignment with weird result [closed]具有奇怪结果的数组分配[关闭]
【发布时间】:2020-01-10 18:02:55
【问题描述】:

问题是将二维数组分配给一维数组

uint8_t Schedules[24][8]={
 {0,1,1,1,1,1,1,0},
 {0,2,2,2,2,2,2,0},
 {0,3,3,3,3,3,3,0},
 ...
 {0,24,24,24,24,24,24,0}
}


for (this_hour=0;this_hour<24;this_hour++){
        for(this_section=1;this_section<7;this_section++)
        {
            buff[this_hour*9+this_section+2]=Schedules[this_hour][this_section];
        }
    }

结果必须是

buff[3..8]=1; /* Pseudocode; means buff[3]=1, buff[4]=1, ..., buff[8]=1 */
buff[9..11]=0;
buff[12..17]=2;
buff[18..20]=0,
...

然而,这就是结果

我不知道我错在哪里。我检查了十次循环,没有发现任何错误。我在 keil 5 中为 stm32 ARM 编写 freertos。我确保仅在 for 循环中访问 buff 而在代码中没有其他位置。这不是堆栈溢出,因为我给堆栈一个很大的数字

【问题讨论】:

  • 为什么要乘以 9?
  • this_section 应初始化为 0,而不是 1。buff 的索引应为 this_hour*8+this_section。 8 是因为每个二维数组中有 8 个元素。不需要添加偏移量 2。
  • 9 是我的计划的一部分,我有 9 个部分,其中 6 个已填满时间表,其他 3 个将在稍后填写。我在 buff 的开头有 3 个标头字节。它们不重要,为什么buff填充不正确?
  • 您的输出模式表明您在其他地方有堆损坏。
  • @david:我绝对确定它不在您发布的代码中。

标签: c for-loop variable-assignment


【解决方案1】:

这样,数组从0开始,均值

Schedules[x][y] x,y 的位置会出现在下方

x*8+y

0......7 从 0,0~0,7 映射

8......15 从 1,0~1,7 映射

16......23 从 2,0~2,7 映射

........

@kevinmont 你对问题是正确的,我编辑了我的问题并更正了它,是的,你是对的,我有 3 个“0”的标题字节,然后是 6 个“1”,另外 3 个“0”,然后是 6 “2”,然后是 3 个“0”,以此类推。 – 大卫 6 小时前

需要数组大小为24*9+1+2

x*9+y+2

2......9 从 0,0~0,7 映射

11......18 从 1,0~1,7 映射

20......27 从 2,0~2,7 映射

........

#include <stdio.h>
#include <stdlib.h>
typedef unsigned char uint8_t;
uint8_t Schedules[24][8]={
{0,1,1,1,1,1,1,0},
{0,2,2,2,2,2,2,0},
{0,3,3,3,3,3,3,0},
{0,4,4,4,4,4,4,0},
{0,5,5,5,5,5,5,0},
{0,6,6,6,6,6,6,0},
{0,7,7,7,7,7,7,0},
{0,8,8,8,8,8,8,0},
{0,9,9,9,9,9,9,0},
{0,10,10,10,10,10,10,0},
{0,11,11,11,11,11,11,0},
{0,12,12,12,12,12,12,0},
{0,13,13,13,13,13,13,0},
{0,14,14,14,14,14,14,0},
{0,15,15,15,15,15,15,0},
{0,16,16,16,16,16,16,0},
{0,17,17,17,17,17,17,0},
{0,18,18,18,18,18,18,0},
{0,19,19,19,19,19,19,0},
{0,20,20,20,20,20,20,0},
{0,21,21,21,21,21,21,0},
{0,22,22,22,22,22,22,0},
{0,23,23,23,23,23,23,0},
{0,24,24,24,24,24,24,0}
};

int main(){
    int this_hour,this_section,i;
    uint8_t buff[192]={0};//24*8
    for (this_hour=0;this_hour<24;this_hour++){
        for(this_section=0;this_section<8;this_section++)
        {
            buff[this_hour*8+this_section]=Schedules[this_hour][this_section];
        }
    }
    for(i=0;i<192;i++){

        if(i!=0 && i%8==0){
            printf("\n");
        }
        printf("%d",buff[i]);
    }

    printf("\n------------------------------------\n");


    uint8_t buff2[219]={0};//24*9+1+2
    for (this_hour=0;this_hour<24;this_hour++){
        for(this_section=0;this_section<8;this_section++)
        {
            buff2[this_hour*9+this_section+2]=Schedules[this_hour][this_section];
        }
    }
    for(i=0;i<219;i++){

        if(i!=0 && i%9==0){
            printf("\n");
        }
        printf("%d",buff2[i]);
    }





    return 0;
}

【讨论】:

  • 我的问题不是如何编写代码,而是为什么我的代码不起作用?你的代码不再适合我了。
  • 编译器 gcc 还是 armcc ?和您的优化工具
  • ARM C/C++ 编译器和我检查没有优化和优化都没有区别,正如我在回答中写的那样,这是一个内存问题
  • 好的,我了解您将主文件和计划拆分到不同的文件中......
【解决方案2】:

正如 Joshua 在 cmets 中所说,内存有问题,代码是正确的。我创建了一个局部变量,它正在工作。

cuprit 是一个错误的声明

extern uint8_t Schedules[24][11];

这个数组的定义是

extern uint8_t Schedules[24][8];

extern 声明中数组的第二个参数错误。所以代码写入内存中,在调试中看不到

【讨论】:

    猜你喜欢
    • 2022-08-17
    • 1970-01-01
    • 1970-01-01
    • 2023-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多