Overwrite (programming)

from Wikipedia, the free encyclopedia

In computer science , overwriting ( clobbering ) means overwriting a file or overwriting memory, which is usually unintentional. The word overwrite is seldom used when a passage in the source code accidentally corrupts memory.

Overwriting is often unintentional, e.g. B. by calling the output redirection operator ( >). To avoid this, various means are used, such as: B. set -o noclobberto set the shell parameter ( bash , ksh ) or set noclobber( csh , tcsh ). This prevents >overwriting by showing an error message :

$ echo "Hello, world" >file.txt
$ echo "This will overwrite the first greeting." >file.txt
$ set -o noclobber
$ echo "Can we overwrite it again?" >file.txt
-bash: file.txt: cannot overwrite existing file
$ echo "But we can use the >| operator ignore the noclobber." >|file.txt
$ # Successfully overwrote the contents of file.txt using the >| operator
$ set +o noclobber # Changes setting back

In higher programming languages such as B. C , is usually not referred to the concept of overwriting a memory location. In order to prevent a memory location from being overwritten, keywords are usually available for the compiler . In the C programming language, the keyword const is intended for this purpose, but it must not be confused with the preprocessor directive #define . If the compiler determines during the semantic analysis that a variable should be overwritten despite an agreement, an error is usually returned and the compilation process is aborted.

In the following example code, the variable a is declared as a constant with the keyword const . If you gcctry to translate it with the compiler, an error message is displayed.

int main(void)
{
    const int a = 3;
    a = 4;
    return 0;
}
test.c: In function ‘main’:
test.c:7:2: error: assignment of read-only variable ‘a’

See also

Individual evidence

  1. ^ "Unix Power Tools," by Shelley Powers, Jerry Peek, Tim O'Reilly, Mike Loukides, p. 892.