Stale pointer bug: Difference between revisions
imported>Kvng foldoc attribution, see Template_talk:FOLDOC#It_doesn't_matter_whether_readers_care |
imported>Kvng |
||
| Line 1: | Line 1: | ||
{{Short description|Software programming error}} | |||
A '''stale pointer bug''', otherwise known as an '''aliasing bug''', is a class of subtle programming errors that can arise in code that does [[dynamic memory allocation]], especially via the [[malloc]] function or equivalent. | A '''stale pointer bug''', otherwise known as an '''aliasing bug''', is a class of subtle programming errors that can arise in code that does [[dynamic memory allocation]], especially via the [[malloc]] function or equivalent. | ||
If several [[pointer (computer programming)|pointer]]s address (are | If several [[pointer (computer programming)|pointer]]s address (are [[pointer aliasing|aliases]] for) a given chunk of storage, it may happen that the storage is freed or reallocated (and thus moved) through one alias and then referenced through another, which may lead to subtle (and possibly intermittent) errors depending on the state and the allocation history of the malloc arena. This bug can be avoided by never creating aliases for allocated memory,<ref name="FOLDOC">{{foldoc|stale+pointer+bug}}</ref> by controlling the dynamic [[scope (programming)|scope]] of references to the storage so that none can remain when it is freed, or by use of a [[garbage collection (computer science)|garbage collector]], in the form of an intelligent memory-allocation library or as provided by [[high-level programming language|higher-level languages]], such as [[Lisp programming language|Lisp]]. | ||
The term | The term ''aliasing bug'' is nowadays associated with [[C (programming language)|C]] programming, but it was already in use in a very similar sense in the [[ALGOL]] 60 and [[Fortran]] programming language communities in the 1960s.<ref name="FOLDOC" /> | ||
==See also== | ==See also== | ||
| Line 13: | Line 15: | ||
[[Category:Software bugs]] | [[Category:Software bugs]] | ||
[[Category:Software_anomalies]] | [[Category:Software_anomalies]] | ||
{{software-eng-stub}} | {{software-eng-stub}} | ||
Latest revision as of 16:09, 28 November 2025
A stale pointer bug, otherwise known as an aliasing bug, is a class of subtle programming errors that can arise in code that does dynamic memory allocation, especially via the malloc function or equivalent.
If several pointers address (are aliases for) a given chunk of storage, it may happen that the storage is freed or reallocated (and thus moved) through one alias and then referenced through another, which may lead to subtle (and possibly intermittent) errors depending on the state and the allocation history of the malloc arena. This bug can be avoided by never creating aliases for allocated memory,[1] by controlling the dynamic scope of references to the storage so that none can remain when it is freed, or by use of a garbage collector, in the form of an intelligent memory-allocation library or as provided by higher-level languages, such as Lisp.
The term aliasing bug is nowadays associated with C programming, but it was already in use in a very similar sense in the ALGOL 60 and Fortran programming language communities in the 1960s.[1]
See also
References
<templatestyles src="Reflist/styles.css" />
Script error: No such module "Check for unknown parameters".