Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C ARM处理器的链表问题_C_Gcc_Arm - Fatal编程技术网

C ARM处理器的链表问题

C ARM处理器的链表问题,c,gcc,arm,C,Gcc,Arm,我试图用C语言创建一个链表,以便在ARM处理器上运行(不确定具体的处理器规格,但将-mcpu=arm7tdmi传递给编译器)。代码如下: #include <posapi.h> #include <posapi_all.h> const APPINFO AppInfo={ "POS-Simple example", "APP-TEST", "1.0", "pcteam", "demo program", "", 0

我试图用C语言创建一个链表,以便在ARM处理器上运行(不确定具体的处理器规格,但将
-mcpu=arm7tdmi
传递给编译器)。代码如下:

#include <posapi.h>
#include <posapi_all.h>

const APPINFO AppInfo={
    "POS-Simple example",
    "APP-TEST",
    "1.0",
    "pcteam",
    "demo program",
    "",
    0,
    0,
    0,
    ""
};

typedef struct st_dllNode {
    struct st_dllNode * next;
    struct st_dllNode * prev;
    void * data;
} dllNode;

typedef struct {
    dllNode* first;
    dllNode* cur;
    uchar size;
} ListContainer;

typedef ListContainer* List;

List createList(void)
{
    List listContainer;
    listContainer = (List) malloc(sizeof(ListContainer));
    listContainer->first = NULL;
    listContainer->cur = NULL;
    listContainer->size = 0; // exception occurs here
    return listContainer;
}

int event_main(ST_EVENT_MSG *msg)
{
    SystemInit();
    return 0;
}


int main(void)
{
    List list;
    SystemInit();
    while (1)
    {
        list = createList();
        free(list);
        Beep();
    }
}

我不知道为什么这段代码在Windows中运行得很好,但在为ARM编译时,会出现这样的错误。有什么想法吗?

解决方案:尝试在代码中添加一个barrier()或类似的宏函数

详情:

List createList(void)
{
    List listContainer;
    listContainer = (List) malloc(sizeof(ListContainer));
    smb();      // this is Linux memory barrier API. Replace it with your OS similar API
    listContainer->first = NULL;
    listContainer->cur = NULL;
    listContainer->size = 0; // exception occurs here
    return listContainer;
}
原因:目标ARM处理器可能会无序执行代码。
listContainer可能无法成功分配,但首先执行“listContainer->size=0”。

作为旁白:您真的认为
typedef listContainer*List使代码更易于阅读?我认为使用typedef仅仅是因为你可以让事情变得不可读。正是这一点导致了另一个用户先前的混乱/错误评论。预回迁中止意味着您试图执行无效内存-我猜实际上是下面的返回语句生成了它,这意味着以前的一些代码破坏了堆栈。@codemonkey更新了代码。使用这些缩写的原因是,这是一个大得多的应用程序的一部分,有许多这样的
typedef
定义。我不负责编码风格。。。由于公司的政策,我不得不遵守它:(:(@iManBiglari)你有没有尝试过编译最小的程序来复制这个bug?我在这里没有看到
main
。如果你能把代码缩减到几十行我们都能看到的全部代码就好了。现在,这里没有人知道什么叫
createList
。我们要找的是:它是probably不是一个好的malloc,仅此而已。您的代码不是错误安全的。这就是问题所在。那么解决方案不是检查
malloc
的返回,而不是放置一个内存屏障。。。?
List createList(void)
{
    List listContainer;
    listContainer = (List) malloc(sizeof(ListContainer));
    smb();      // this is Linux memory barrier API. Replace it with your OS similar API
    listContainer->first = NULL;
    listContainer->cur = NULL;
    listContainer->size = 0; // exception occurs here
    return listContainer;
}