简体   繁体   English

将数据写入特定的内存地址问题

[英]write data to specific memory address issue

When I use the following code to write data at specific address at memory i get an error told me that 当我使用以下代码在内存中的特定地址写入数据时,出现错误告诉我

" type error in argument 1" of function write. 函数写入的“参数1中的类型错误”。

as i understand S_LEN is a pointer to 0x1814 so it should work in correct way but at this case i get the previous error. 据我了解S_LEN是一个指向0x1814 ,所以应该以正确的方式工作,但在这种情况下,我得到了以前的错误。

when i replace the command write(S_LEN,0); 当我替换命令write(S_LEN,0); via write(&S_LEN,0); 通过write(&S_LEN,0); it work correct. 它工作正常。 although at first case i pass hex number casting to pointer to integer. 尽管在第一种情况下,我将十六进制数字传递给指向整数的指针。

can any one explain this matter? 谁能解释这个问题?

#define RAM_START 0x1800
#define S_LEN  (((int32_t *)(RAM_START))[0xFF])
    int32_t write(int32_t *dest_ptr, int32_t src)
    {                                     
        *dest_ptr = src;
         return 0;
    } 

    main()
    {
     write(S_LEN,0);
    }

Your write function expects int32_t * as first argument, which is a pointer , so you should use something like this instead, if you want S_LEN to be a pointer: 您的write函数期望将 int32_t *作为第一个参数,它是一个指针 ,因此,如果您希望S_LEN作为指针 ,则应使用如下所示的内容:

#define S_LEN  (((int32_t *)(RAM_START))+0xFF)

Your expression evaluates to an int32_t since you apply the subscript operator [] on the int32_t * (which is a pointer). 由于在int32_t * (这是一个指针)上应用下标运算符 [] ,因此表达式的计算结果为int32_t

On the other hand &S_LEN is accepted, beause it yields the address of the int32_t you get by applying [] . 另一方面, &S_LEN被接受,因为它产生通过应用[]获得的int32_t的地址。

Also note that S_LEN relates to the memory location 4*0xFF+0x1800 which is 0x1BFC . 另请注意, S_LEN与存储位置4*0xFF+0x1800 ,即0x1BFC (It is 255 int32_t s after 0x1800 ). 0x1800之后为255 int32_t s)。

In your code, S_LEN does the following: 在您的代码中, S_LEN执行以下操作:

  1. it convert RAM_START to a pointer of type int32_t * 它将RAM_START转换为int32_t *类型的指针int32_t *
  2. subscription of a pointer of type int32_t * should be a int32_t number 订阅类型为int32_t *的指针应为int32_t

But write need first argument to be of type int32_t * , so &S_LEN is right, cause the address of an int32_t returned by S_LEN is of type int32_t * 但是write第一个参数必须是int32_t *类型,所以&S_LEN是正确的,因为S_LEN返回的int32_t的地址是int32_t *类型

The output from OSX 10.11: OSX 10.11的输出:

[oxnz@rmbp:tmp:0]$ cc     a.c   -o a
a.c:14:8: warning: incompatible integer to pointer conversion passing 'int32_t'
      (aka 'int') to parameter of type 'int32_t *' (aka 'int *'); take the
      address with & [-Wint-conversion]
        write(S_LEN,0);
              ^~~~~
              &(   )
a.c:5:16: note: expanded from macro 'S_LEN'
#define S_LEN  (((int32_t *)(RAM_START))[0xFF])
               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.c:6:24: note: passing argument to parameter 'dest_ptr' here
int32_t write(int32_t *dest_ptr, int32_t src)
                       ^
1 warning generated.

[oxnz@rmbp:tmp:0]$ cat a.c
#include <stdio.h>
#include <stdlib.h>

#define RAM_START 0x1800
#define S_LEN  (((int32_t *)(RAM_START))[0xFF])
int32_t write(int32_t *dest_ptr, int32_t src)
{
    *dest_ptr = src;
    return 0;
}

int main()
{
    write(S_LEN,0);
}

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

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