如何在C语言中实现只读存储器?

I heard that in C, if I do

char *s = "hello world". 

the "hello world" is actually stored in read-only memory.

I am not so clear about read-only memory. What is the explanation? Is that like a flag to the compiler that tells the compiler to do not write into that section?

#0

That's not a feature of the C language but a feature of the compiler/linker and the operating system working together.

When you compile such code the following happens:

  • The compiler will put the string into a read-only data-section.

  • The linker collects all the data in such read-only sections and puts them into a single segment. This segment resides in the executable file and is flagged with a "read only"-attribute.

  • Now comes the operating system executable loader. It loads the executable (or maps it into memory to be more exact). Once this is done, the loader walks the sections and sets access-permissions for each segment. For a read-only data segment it will most likely disable code-execute and write access. Code (for example, your functions) gets execute rights but no write access. Ordinary data like static variables gets read and write access and so on...

That's how modern operating systems do it.

As said, it's not a feature of the C language. If you compile the same problem for DOS, for example, the program will run but no write protection would be possible, because the DOS-loader does not know about read-only sections.

#1

Executables contain two parts: a .data section, containing global variables, and a .text section, containing the actual machine code.

Strings are placed into the .data section. What C does when it sees "Hello world" is it puts the string "Hello world" into the executable itself, and replaces instance of "Hello world" in the program with the address where that string ends up being loaded.

Having said that, I'm not sure why it's read-only - theoretically a program should be able to modify its own memory..

#2

True read-only memory is implemented by the memory subsystem of the OS. The OS can mark certain pages as read-only.

In the binary, the compiler can tell the OS which parts of the executable should be placed in read-only vs read-write memory pages.

#3

An example of how to do this in Linux is on page 179 of Advanced Linux Programming by Mark Mitchell, Jeffrey Olham, and Alex Samuel.

#4

When you write char s[10]="sneha"; you are allocating 10 bytes of storage space(not memory, memory comes into picture only when u r executing your program) in your object file. This is static allocation of memory( at compile time).

But when you write char *s="sneha"; you are not allocating any storage space to store "sneha". It will get stored in READ ONLY section. But the pointer s is stored in different section based on where it is declared. But it is pointing to the READ ONLY DATA "sneha". So if you try to write on it you will get segmentation fault.

For example:

char *s = "sneha";
s[1] = 'N'; 
printf("%s",s);  // you expecting output sNeha, 
                 // but you get a seg fault since it is READ ONLY DATA 

#5

As other folks have mentioned, whether the contents of constant strings are stored in read-only memory is determined by the operating system, compiler, and chip architecture.

More precisely, the C standard specifies that the quoted strings are considered to have "const char[]" type (or words to that effect, I don't have the standard at hand).

Any code that attempts to modify the contents of such a string is invoking undefined behavior. That means that literally anything can happen at that point, and the provider of the compiler isn't even required to document what can happen.

In practice, this means that a C or C++ program that wants to be portable has to avoid modifying constant strings.

In general, the compiler will not allow you to modify the contents of of "const" variables, so you can consider "const" to mean "read only" in most cases. Unfortunately, there's a special exception for char * and const char *, largely for historical reasons. That means that code like this:

char *x = "Hello, World";
*x = 'h';

will compile without error or warning, even though it invokes undefined behavior.

#6

You could try something like

s[4] = '0';

and see if it says "hello w0rld" when you call

puts(s);

If it causes an immediate Segmentation Fault or a Data Execution Prevention exception then it is probably read only. (If the system lets you get away with it, that doesn't make it a good idea.)

推荐文章

Android后台服务

Android后台服务

推荐文章

对角列标题

对角列标题

推荐文章

这种类型的节点模块的正确名称是什么?

这种类型的节点模块的正确名称是什么?

推荐文章

如何在iphone的UITableView中获得每个单元格的中心位置?

如何在iphone的UITableView中获得每个单元格的中心位置?

推荐文章

什么导致页面错误?

什么导致页面错误?

推荐文章

找到给定向量的k近邻?

找到给定向量的k近邻?

推荐文章

在XCode中链接到C库(.a)的正确方法是什么?

在XCode中链接到C库(.a)的正确方法是什么?

推荐文章

尝试修复AVAudioPlayer在初次使用时的延迟

尝试修复AVAudioPlayer在初次使用时的延迟

推荐文章

如何使网页与各种屏幕大小兼容

如何使网页与各种屏幕大小兼容

推荐文章

简单、兼容的二维/三维动画软件

简单、兼容的二维/三维动画软件

推荐文章

Android屏幕激活

Android屏幕激活

推荐文章

用于Windows的Cobol模拟器

用于Windows的Cobol模拟器

推荐文章

从已启动的广播接收器启动PhoneStateListener时出错

从已启动的广播接收器启动PhoneStateListener时出错

推荐文章

RoundOffTest()似乎上下取整

RoundOffTest()似乎上下取整

推荐文章

PHP编码错误

PHP编码错误

推荐文章

用javascript格式化小数

用javascript格式化小数