简体   繁体   English

在 c 程序中静态分配一个 memory 地址

[英]Statically assign a memory address in c program

I'm building very small test program and I wanted to have the program access the same memory address every time(I know its not a good practice) to simulate some behaviors.我正在构建非常小的测试程序,我想让程序每次都访问相同的 memory 地址(我知道这不是一个好习惯)来模拟一些行为。 How can I just pick a memory address to hard code in the program an try it out?我怎样才能选择一个 memory 地址在程序中进行硬编码并尝试一下? Is there a way to see unused blocks of memory addresses and just block them?有没有办法查看 memory 地址的未使用块并阻止它们?

I totally understand that this might create unwanted conditions/situation.我完全理解这可能会造成不必要的条件/情况。

You can use ampersand operator (&) to point a pointer to a specific memory address.您可以使用与号ampersand operator (&)将指针指向特定的 memory 地址。 However, your program must be able to able to legally access that address which is decided by what address range your OS has assigned to your program otherwise you will a segmentation fault .但是,您的程序必须能够合法地访问该地址,该地址由您的操作系统分配给您的程序的地址范围决定,否则您将出现segmentation fault

Sample code:示例代码:

void * p1 = (void *)0x28ff44;

Or if you want it as a char pointer:或者,如果您希望它作为 char 指针:

char * p2 = (char *)0x28ff44;

PS附言

You can find out the address allocated to your program and take one of the addresses from it into your program.您可以找出分配给您的程序的地址,并将其中的一个地址放入您的程序中。 For a single run, your program will access the same memory location but for another run, it will be different one assigned to your process but same for that run.对于单次运行,您的程序将访问相同的 memory 位置,但对于另一次运行,分配给您的进程的位置将不同,但该运行的位置相同。

You can refer here to check how you can read memory address assigned to your process.您可以参考此处查看如何读取分配给您的进程的 memory 地址。 You can take input at runtime to provide your process id to get the filepath .您可以在运行时获取输入以提供您的process id以获取文件路径

Work around解决方法

Since you mentioned it is small test program , you can also save yourself your efforts by just disabling randomization of memory addresses by disabling ASLR for your testing your program, you just disable ASLR in linux using由于您提到它是small test program ,您还可以通过禁用ASLR测试您的程序来禁用randomization of memory addresses ,您只需使用 linux 禁用 ASLR

echo 0 > /proc/sys/kernel/randomize_va_space

and then run your program, declare and initialize a variable, print its address and then hardcode that address in your program.然后运行你的程序,声明和初始化一个变量,打印它的地址,然后在你的程序中硬编码这个地址。 Bingo.!答对了。! Everytime that address will be used untill you enable ASLR again.每次都将使用该地址,直到您再次启用 ASLR。

However it is not secure to turn off ASLR and after testing you should enable ASLR again by但是关闭 ASLR 并不安全,测试后您应该再次启用 ASLR

echo 1 > /proc/sys/kernel/randomize_va_space

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

相关问题 将argv给定的内存地址分配给c中的指针 - assign a memory address given by argv to a pointer in c 需要说明此C程序中的内存地址如何工作 - need explanation of how memory address work in this C program 无法访问特定的内存地址。 在C程序上 - Cant access a specific memory address. on C program 为什么我的 C 程序 output 是 memory 地址而不是值? - Why does my C program output the memory address instead of the value? 是否可以在屏幕上在C程序中输出某个内存地址的内容? - Is it possible to output on screen the content of a certain memory address in a C program? 如何检查程序是否在C中静态编译 - How to check if a program was statically compiled in c 在我的C程序中静态链接OpenSSL - Statically Link OpenSSL in my c program 如何使用malloc获取c中指针的内存地址,然后在该地址分配char数组? - How to use malloc to get memory address for pointer in c, then assign char array at that address? 如何在C / C ++中将二维数组的指针分配给内存地址 - How to assign pointer of two dimensional array to memory address in C/C++ C 程序是否可以访问和更改分配给另一个程序的堆中的内存地址? - Is it possible for a C program to access and changing a memory address in the heap allocated to another program?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM