Read and write to a memory location

2020-03-04 02:36发布

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

#include <stdio.h>

int main()
{
    int val;
    char *a = (char*) 0x1000;
    *a = 20;
    val = *a;
    printf("%d", val);
}

But it is throwing a run time error, at *a = 20.

Then how can I write and read a specific memory location?

Please help me

标签: c codeblocks
9条回答
小情绪 Triste *
2楼-- · 2020-03-04 03:13

You can write to a specific memory location.

But only when you have the rights to write to that location.

What you are getting, is the operating system forbidding you to write to 0x1000.

查看更多
看我几分像从前
3楼-- · 2020-03-04 03:19

You can't just write at any random address. You can only modify the contents of memory where your program can write.

If you need to modify contents of some variable, thats why pointers are for.

char a = 'x';
char* ptr = &a; // stored at some 0x....
*ptr = 'b'; // you just wrote at 0x....
查看更多
爱情/是我丢掉的垃圾
4楼-- · 2020-03-04 03:21

You are doing it except on your system you cannot write to this memory causing a segmentation fault.

A segmentation fault (often shortened to segfault), bus error or access violation is generally an attempt to access memory that the CPU cannot physically address. It occurs when the hardware notifies an operating system about a memory access violation. The OS kernel then sends a signal to the process which caused the exception. By default, the process receiving the signal dumps core and terminates. The default signal handler can also be overridden to customize how the signal is handled.

If you are interested in knowing more look up MMU on wikipedia.

Here is how to legally request memory from the heap. The malloc() function takes a number of bytes to allocate as a parameter. Please note that every malloc() should be matched by a free() call to that same memory after you are done using it. The free() call should normally be in the same function as where you called malloc().

#include <stdio.h>
int main()
{
    int val;
    char *a;

    a = (char*)malloc(sizeof(char) * 1);

    *a = 20;
    val = (int)*a;
    printf("%d", val);

    free(a);

    return 0;
}

You can also allocate memory on the stack in a very simple way like so:

#include <stdio.h>
int main()
{
    int val;
    char *a;
    char b;

    a = &b;
    *a = 20;
    val = (int)*a;

    printf("%d", val);

    return 0;
}
查看更多
Deceive 欺骗
5楼-- · 2020-03-04 03:21

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.

查看更多
小情绪 Triste *
6楼-- · 2020-03-04 03:24

First you need to be sure about memory location where you want to write into. Then, check if you have enough permission to write or not.

查看更多
The star\"
7楼-- · 2020-03-04 03:28

This is throwing a segment violation (SEGFAULT), as it should, as you don't know what is put in that address. Most likely, that is kernel space, and the hosting environment doesn't want you willy-nilly writing to another application's memory. You should only ever write to memory that you KNOW your program has access to, or you will have inexplicable crashes at runtime.

查看更多
登录 后发表回答