简体   繁体   English

C 指向函数指针的数组在一定长度以上中断程序

[英]C array of pointers to functions pointers breaks program above certain length

Hi im trying to call functions from a string the microcontroller receives.嗨,我试图从微控制器接收的字符串中调用函数。 To do this the micro controller find recieves and compares this to the names in the peripheral name list.为此,微型 controller 查找接收并将其与外围设备名称列表中的名称进行比较。 It uses the index to get a pointer to a list of functions names available for that peripheral and then compares then function name to that list to get a functions index.它使用索引获取指向该外设可用的函数名称列表的指针,然后将 function 名称与该列表进行比较以获得函数索引。 These two indices are then used to call the functions.然后使用这两个索引来调用函数。

The system works fine for 2 peripherals but then adding a third causes the program to stop running, sometimes it gives the warning stack pointer is out of range, other times it will just show no position when the program is paused.系统在 2 个外围设备上工作正常,但添加第三个外围设备会导致程序停止运行,有时它会发出警告堆栈指针超出范围,有时它只会在程序暂停时显示没有 position。 Adding a third peripheral to the peripheral_fnct_pntr_pntr list causes this to happen, it functions fine with all combinations of 2 peripherals and commenting the line will work aswell.将第三个外设添加到 peripheral_fnct_pntr_pntr 列表会导致这种情况发生,它可以在 2 个外设的所有组合中正常工作,并且注释该行也可以正常工作。

I made custom types for functions pointers as shown:我为函数指针制作了自定义类型,如下所示:

typedef void (*fp)(int); //Declares a type of a void function that accepts an int
typedef char (*p)[6][20]; //pointer to array 

the lists of funstions and pointers are created as shown:函数和指针列表的创建如下所示:

    char peripheral_names[6][20] = {"rgb","button"};  //[no of strs][size of str]
    char rgb_fnct_names[6][20] = {"rgb_brightness","red_brightness","green_brightness","blue_brightness","rgb_onTime","rgb_breath"};
    char led1_fnct_names[6][20] = {"led1_brightness","led1_onTime","led1_breath"};
    char led2_fnct_names[6][20] = {"led2_brightness","led2_onTime","led2_breath"};

        //store pointers linking name to functiion index to function
    //rgb led functions pointers
    fp rgb_fnct_pntrs[] = {&rgb_brightness,&red_brightness,&green_brightness,&blue_brightness,&rgb_on_time,&rgb_breath};

    
    //led function pointers
    fp led1_fnct_pntrs[] = {&led1_brightness,&led1_on_time};
    fp led2_fnct_pntrs[] = {&led2_brightness,&led2_on_time};
    
    //store list of ponters to peripheral function pointer lists
    fp *perph_fnct_pntr_pntr[]  = {rgb_fnct_pntrs, led1_fnct_pntrs, led2_fnct_pntrs};


    
   //store pointers linking peripheral name index to list of function names
   p name_to_fnct_lst[3]= {&rgb_fnct_names,&led1_fnct_names,&led2_fnct_names};

Im not sure whats causing this problem so any help would be appreciated.我不确定是什么导致了这个问题,所以任何帮助将不胜感激。

I'm guessing the way this works is, you start with a string, and find the matching string by iterating through name_to_fnct_lst[] , then searching the arrays it points to ( rgb_fnct_names , led1_fnct_names , led2_fnct_names ).我猜它的工作方式是,你从一个字符串开始,通过迭代name_to_fnct_lst[]找到匹配的字符串,然后搜索它指向的 arrays ( rgb_fnct_namesled1_fnct_namesled2_fnct_names )。 When there's a match, you have two indexes (function list index, and name within the list).匹配时,您有两个索引(函数列表索引和列表中的名称)。

You use the first index to look up a function pointer list in perph_fnct_pntr_pntr , then the second to look up the index in rgb_fnct_pntrs , led1_fnct_pntrs , or led2_fnct_pntrs .您使用第一个索引在perph_fnct_pntr_pntr中查找 function 指针列表,然后使用第二个索引在rgb_fnct_pntrsled1_fnct_pntrsled2_fnct_pntrs中查找索引。

Which works in the rgb case, because both rgb_fnct_names and rgb_fnct_pntrs have 6 entries, but not for led1/2 because led1_fnct_names and led2_fnct_names have 3 entries, but led1_fnct_pntrs and led2_fnct_pntrs have 2 entries.这适用于 rgb 情况,因为rgb_fnct_namesrgb_fnct_pntrs都有 6 个条目,但不适用于 led1/2 因为led1_fnct_namesled2_fnct_names有 3 个条目,但led1_fnct_pntrsled2_fnct_pntrs有 2 个条目。 Am I following correctly?我是否正确遵循?

If so, it looks like the solution is to add the missing function pointers to led1_fnct_pntrs and led2_fnct_pntrs .如果是这样,看起来解决方案是将缺少的 function 指针添加到led1_fnct_pntrsled2_fnct_pntrs Or it might simplify things to combine the arrays into a char[3][6][20] and a fp[3][6] array (no downside in the first case, you're allocating unused chars anyway, the second case loses a few unused pointer addresses which you gain back from smaller code).或者将 arrays 组合成一个char[3][6][20]和一个fp[3][6]数组可能会简化一些事情(在第一种情况下没有缺点,无论如何你都在分配未使用的字符,第二种情况丢失了一些您从较小代码中获得的未使用的指针地址)。

Another way to structure it is:另一种构造它的方法是:

struct f_nam_ptr {
    fp func;
    char name[20];
};
// Using a single map array here, or split it
// into rgb, led1, led2 maps.
struct f_nam_ptr f_nam_ptr_map[] = {
    { &rgb_brightness, "rgb_brightness" },
    // etc...
};

That ensures every name has a matching pointer (and doesn't waste as much memory).这样可以确保每个名称都有一个匹配的指针(并且不会浪费太多内存)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM