简体   繁体   English

嵌入式系统内存冲突

[英]Memory conflict in embedded system

I am building an application with Diab compiler for PowerPC.我正在为 PowerPC 使用 Diab 编译器构建一个应用程序。 I am having an issue with memory conflict.我遇到了内存冲突问题。

In header1.h I have declaredheader1.h我已经声明

...
void NullFunct_1 (int);
...

The function NullFunct_1 is then defined in main.c然后在main.c定义函数NullFunct_1

    #include "header1.h"
...
           void NullFunct_1 (int f)
           {
              return;
           }

In header2.h I have declaredheader2.h我已经声明

    struct tp_key 
    {
        int a;                    
        void (*fn_ptr)(int);     // function pointer    
        unsigned int b;             
        unsigned int c;                
        int d;                  
        int e;                 
    };

In header2.c I initialize the array Key as followheader2.c我初始化数组Key如下

struct tp_key Key[48] = {0, &NullFunct_1, 0, 0, 0, 0};

In header3.h I declare the global variable Unitsheader3.h我声明了全局变量Units

extern int Units;

In header3.c I define and initialize the variable Unitsheader3.c我定义并初始化了变量Units

int Units = 0; //Units variable can then assume the values of 0, 1000 or 10000 only.

The program is compiled and linked without errors generating the executable.程序编译和链接没有错误生成可执行文件。

The Link Editor Memory Map generated is the following生成的链接编辑器内存映射如下

    input           virtual                        
    section         address     size     file
...
    .data           50036adc    00000484 header2.o
           Key      50036adc    00000480
           Xxxx     50036f5c    00000004

    .data           50036f60    00000170 header3.o
           Units    50036f60    00000004
...
...
        NullFunct_1 500038b8    0000000c main.o
...

You may notice the variables Key and Units are memory "neighbors" 0x50036adc + 0x480 = 0x50036f5c .您可能会注意到变量KeyUnits是内存“邻居” 0x50036adc + 0x480 = 0x50036f5c

In some cases the variable Units contains value 1342191800 (0x500038B8 in hex) that corresponds to the memory address of the variable Null_Funct_1在某些情况下,变量Units包含值1342191800 (0x500038B8 in hex) ,对应于变量Null_Funct_1的内存地址

This happens because of the instruction这是因为指令

struct tp_key Key[48] = {0, &NullFunct_1, 0, 0, 0, 0};

Is this a linker problem that did not calculate the memory size of the variable Key correctly?这是一个链接器问题,没有正确计算变量 Key 的内存大小吗? or did I make a mistake?还是我做错了? Even if there is a linker problem then may you suggest how to avoid such kind of problems, please?即使存在链接器问题,您能否建议如何避免此类问题?

There are no "conflicts" here.这里没有“冲突”。

sizeof(struct tp_key) [24] * 48 in hex is 0x480. sizeof(struct tp_key) [24] * 48 十六进制是 0x480。 So everything is 100% fine.所以一切都是 100% 好。

My advice: do not focus on chasing "compiler errors".我的建议:不要专注于追逐“编译器错误”。 Assume that the compilers were tested and are used by millions of programmers.假设编译器经过测试并被数百万程序员使用。

Key has 48 elements but in my source code I assigned variable Key[48] and it is, of course, incorrect. Key有 48 个元素,但在我的源代码中,我分配了变量 Key[48],当然,这是不正确的。 The cause of the error in the code is代码中错误的原因是

for ( i=0; i <= 48; i++) 
{ 
     Key[i].fn_ptr = &NullFunct_1; 
}

To fix the problem I changed the for loop as follow为了解决这个问题,我改变了 for 循环如下

for ( i=0; i < 48; i++) 
{ 
     Key[i].fn_ptr = &NullFunct_1; 
}

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

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