Read and write to a memory location

前端 未结 9 1399
忘掉有多难
忘掉有多难 2021-01-05 05:40

After doing lot of research in Google, I found below program

#include 

int main()
{
    int val;
    char *a = (char*) 0x1000;
          


        
9条回答
  •  猫巷女王i
    2021-01-05 06:07

    You cannot randomly pick a memory location and write to. The memory location must be allocated to you and must be writable.

    Generally speaking, you can get the reference/address of a variable with & and write data on it. Or you can use malloc() to ask for space on heap to write data to.

    This answer only covers how to write and read data on memory. But I don't cover how to do it in the correct way, so that the program functions normally. Other answer probably covers this better than mine.

提交回复
热议问题