KEIL C258 _mspace illegal in struct/union

来源:互联网 发布:mac电脑如何删除文件 编辑:程序博客网 时间:2024/06/07 09:14

作       者:武力戡乱

修改日期:2017-09-13

备       注:

1、总备注信息

2、联系方式

3、其它博文链接:武力戡乱博客目录总表

4、转载地址:http://blog.sina.com.cn/s/blog_446cfdc60100zb0q.html

内       容:

先看KEIL给出的解释(一下是引用KEIL的):
Error/Warning C258
memory-space Illegal on Struct/Union Member
Summary *** Error/Warning C258 memory-space Illegal on Struct/Union Member memory-space on Parameter Ignored Description Memory types may not be specified for members of struct and union types or for function parameters. Objects referenced by pointers may contain a memory type. Cause A memory type (code, xdata, data, ...) was specified for a struct or union member or for a function argument. For example: unsigned char function ( unsigned int xdata parm_1, unsigned char data parm_2){} Resolution
Remove the memory type from the struct, union, or function definition. These objects have very specific storage requirements which may not be altered using memory types.
Example struct vp { char code c; int xdata i; };
generates error 258.
struct v1 { char c; int xdata *i; };
is the correct declaration for the struct.
这里的意思应该是结构体内不能规定变量的存储区域,比如:
typedef struct {
    unsigned char code/idata/data/xdata/bdata variable;
}newStruct
这里声明的结构体类型newStruct中规定了变量variable的存储区域,所以报错c258。
纠正方法如下:
typedef struct {
    unsigned char variable;//这里变量variable存储在默认的存储区域内(small在data内,compact在pdata内,large在xdata内)
    //或者
    unsigned char code/idata/data/xdata 1 * variable;//这里的变量variable指向指定的存储区域,而变量variable本身存储在默认的存储区域内
}newStruct
声明无错以后,定义结构体变量可以如下:
newStruct structVariable;//这里是单纯的将结构体定义在默认的存储区域内,在这里可以指定真实变量的存储区域,因为变量声明中是不为变量分配内存的,但在定义中会给变量分配唯一的内存单元。比如:
newStruct code/idata/data/xdata 2 structVariable;//这里会将structVariable结构中的指针变量variable定位在指定的存储区间2中,由于指针变量指向明确的存储区间1,所以指针变量variable在指定的存储区间2中占用2字节的空间。
如果需要操作字符串又希望得到节俭的代码,建议使用这种方式:
typedef struct {
    unsigned char code *variable;
}newStruct;
newStruct code structVariable;
原因的话自己写一段程序,软模拟看汇编代码就知道啦~

原创粉丝点击