将内存地址直接指向指针 [英] direct memory address to a pointer

查看:91
本文介绍了将内存地址直接指向指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我对指针有疑问,
我们可以直接将地址给指针吗?
像这样的代码:

int *p2;
p2=0x22fefc;

解决方案

您可以做到:

  int  * p2 = reinterpret_cast< int *>(0x22fefc);

//  C样式转换:
 int  * p1 =( int  *)0x22fefc;

// 易于检查:p1 == p2,但是取消引用它们可能是一个问题:-) 



结果取决于平台.在大多数现代平台上,它看起来像是一些随机地址;并且您很可能会遇到常规保护故障"异常.在某些平台上,如果地址有意义,则可能会起作用.例如,使用MS-DOS,您可以通过这种方式访问​​视频卡或其他一些物理内存缓冲区. (但是,在Intel CPU的实模式下,该地址将是segment:offset对,因此该代码将无法正常工作.)

—SA


是的.但是,当您尝试使用它时,很有可能会完全失败-程序地址的内存空间中不太可能出现随机地址,并且操作系统会发现访问尝试并杀死您的应用程序.

是的,这是在C中为指针分配地址的一种非常有效的方法.
写入地址时,必须确保已设置内存
相应地...通过链接程序指令文件.

我宁愿这样做,如下所示:

#define SOME_ADDRESS (0x22fefc)
int *p2 = (int*)SOME_ADDRESS;



以下是用于Power PC处理器的链接器指令文件的示例:

-sec
{
    .PPC.EMB.sdata0 0xffff8000 ABS:
    .PPC.EMB.sbss0清除ABS:

    .text 0x10000:
    .syscall:
    .secinfo:
    .rodata:
    .sdata2:
    .fixaddr:
    .fixtype:
    .ROM.data ROM(.data):
    .ROM.sdata ROM(.sdata):
    .sdabase align( 8 ):
    .sdata:
    .sbss:
    .数据 :
    .bss:
    .heap align( 16 )pad(0x100000):
    .stack align( 16 )pad(0x80000):

    SOME_ADDRESS 0x0022fefc MIN_SIZE(0x4):
} 


hi i have a question about pointers,
can we give address directly to a pointer?
like this code:

int *p2;
p2=0x22fefc;

解决方案

You can do it:

int * p2 = reinterpret_cast<int*>(0x22fefc);

//C-style cast:
int * p1 = (int*)0x22fefc;

//easy to check-up: p1==p2, but de-referencing them could be a problem :-)



The result depends on the platform. On most modern platforms is would looks like some random address; and you will very likely get General Protection Fault exception. In some platforms it may work if the address makes sense. For example, with MS-DOS you could access video card this way or some other physical memory buffer. (However, in real-mode of Intel CPU the address would be the segment:offset pair, so this code won''t work anyway.)

—SA


Yes. But, there is a very good chance of a complete failure when you try to use it - it is unlikely that a random address will be within your programs memory space, and the OS will spot the access attempt and kill your app.


yes, that is very much a valid way of assigning an address to a pointer in C.
When writing to the address you''d have to ensure that that your memory is set up
accordingly though... e.g. though a linker directive file.

I would rather do it as follows though:

#define SOME_ADDRESS (0x22fefc)
int *p2 = (int*)SOME_ADDRESS;



Below is an example of a linker directive file for a power pc processor:

-sec
{
    .PPC.EMB.sdata0 0xffff8000  ABS :
    .PPC.EMB.sbss0          CLEAR ABS :

    .text 0x10000 :
    .syscall :
    .secinfo :
    .rodata :
    .sdata2 :
    .fixaddr :
    .fixtype :
    .ROM.data ROM(.data) :
    .ROM.sdata ROM(.sdata) :
    .sdabase align(8) :
    .sdata :
    .sbss :
    .data :
    .bss :
    .heap align(16) pad(0x100000) :
    .stack align(16) pad(0x80000) :

    SOME_ADDRESS 0x0022fefc MIN_SIZE(0x4) :
}


这篇关于将内存地址直接指向指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆