c - C 中多个嵌套结构的硬故障

标签 c struct nested fault

我在头文件的父结构中有几个嵌套结构,如下所示:

struct Treatment {

     unsigned char num_phases;
     unsigned char duration[6];
     unsigned char max_current[3];   

     struct Phase{

        unsigned char duration[6];
        unsigned char start_delay;
        unsigned char ramp_up_threshold;
        unsigned char ramp_up[3];
        unsigned char contraction[4];
        unsigned char ramp_down_threshold;
        unsigned char ramp_down;
        unsigned char relaxation;
        unsigned char end_delay;
        unsigned char frequency;
        unsigned char time[3];

        struct Pulse{

           unsigned char type;
           unsigned char bipolar_gap[3];
           unsigned char num_timeslots;
           unsigned char pre_pulse_delay[5];        

           struct Timeslot{

              unsigned char duration[3];
              unsigned char dead_time[2];
              unsigned char amplitude[3];
              unsigned char electrodes_1_2[2];
              unsigned char electrodes_3_4[2];

           } timeslots[20];

       } pulses[10];

   } phases[5];

};

我正在这样的源文件中创建父结构的实例(当然包括头文件):

struct Treatment treatment = {0};

我在 Keil 项目的 STM32 上使用 J-LINK 在 Debug模式下运行代码。由于父结构中的最内层结构,我遇到了硬故障异常:

struct Timeslot{

    unsigned char duration[3];
    unsigned char dead_time[2];
    unsigned char amplitude[3];
    unsigned char electrodes_1_2[2];
    unsigned char electrodes_3_4[2];

} timeslots[20]; 

当我删除这个内部结构时,代码运行正常。我没有收到任何构建错误。

硬故障发生在使用父结构的函数处:

parseMessage(TR, &message[0], treatment); 

谁能帮我解决这个问题?我不知道为什么调试器不喜欢内部结构。

干杯, 托尼

最佳答案

您有如此多的结构嵌套数组,以至于每个 struct Treatment 的大小为 12625 B(假设它已打包)。删除内部结构数组后,大小减少了 12 kB,仅为 625 B,虽然仍然很大,但还是合理的。

当您将一个结构传递给一个函数时,它会在调用该函数之前将其压入堆栈,因此您很可能会用完堆栈空间并遇到堆栈溢出。如果你需要使用这个结构,创建一个使用指针的函数:(猜测涉及的其他类型,你会使用实际的)

// Function declaration
void parseMessage(int, char *message, struct Treatment *treatment);

// Function usage
parseMessage(TR, &message[0], &treatment); 

需要考虑的一件事是,按值传递给函数的结构将被复制,因此对 scruct 所做的任何更改都只会存在于函数内的副本中。当函数退出时,该副本将被删除,函数执行期间所做的任何更改都将消失。如果你想对原始结构进行更改,那么上面写的方法应该有效。如果您不需要随时对结构进行任何更改,则可以使用如下函数:

void parseMessage(int, char *message, struct Treatment const *treatment);

您将无法修改treatment 所指向的结构的内容,如果您尝试这样做,编译器将会对您大吼大叫。

如果函数的性质是您对结构进行更改,但希望这些更改在函数结束后消失,那么唯一合理的方法就是复制它。由于它似乎对堆栈来说太大了,你必须使用 malloc 创建一个动态分配,memcpy 将数据复制到新副本,进行你的操作,然后 free 它。然而,这会占用堆中的大量空间,如果看到对 malloc 的调用失败(返回 NULL),我不会感到惊讶。

关于c - C 中多个嵌套结构的硬故障,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50992060/

相关文章:

javascript变量范围: returning variables from nested functions?

c - 在用 C 编写的 DL 列表中出现段错误?

c - 多种语言的文学编程

C - realloc 导致结构问题

C编程: Dereferencing pointer to incomplete type error

jQuery 将类添加到嵌套列表中的所有最后一个子级

c - ^ scanf 修饰符实际上做了什么?

python - 通过 c (debian) 在终端中运行 python 模块

c - C中结构内数组的初始化

haskell - 将嵌套类型转换为 monad 转换器堆栈