Guard Byte - Principle

Principle

When a program is compiled for debugging, all memory allocations are prefixed and postfixed by guard bytes. Special memory allocation routines may then perform additional tasks to determine unwanted read and write attempts outside the allocated memory. These extra bytes help to detect that the program is writing into (or even reading from) inappropriate memory areas, potentially causing buffer overflows. In case of accessing these bytes by the program's algorithm, the programmer is warned with information assisting him/her to locate the problem.

Checking for the inappropriate access to the guard bytes may be done in two ways:

  • by setting a memory breakpoint on a condition of write and/or read to those bytes, or
  • by pre-initializing the guard bytes with specific values and checking the values upon deallocation.

The first way is possible only with a debugger that handles such breakpoints, but significantly increases the chance of locating the problem. The second way does not require any debuggers or special environments and can be done even on other computers, but the programmer is alerted about the overflow only upon the deallocation, which is sometimes quite late.

Because guard bytes require additional code to be executed and additional memory to be allocated, they are used only when the program is compiled for debugging. When compiled as a release, guard bytes are not used at all, neither the routines working with them.

Read more about this topic:  Guard Byte

Famous quotes containing the word principle:

    Now, what I want is, Facts. Teach these boys and girls nothing but Facts. Facts alone are wanted in life. Plant nothing else, and root out everything else. You can only form the minds of reasoning animals upon Facts: nothing else will ever be of any service to them. This is the principle on which I bring up my own children, and this is the principle on which I bring up these children. Stick to Facts, sir!
    Charles Dickens (1812–1870)

    You may call a jay a bird. Well, so he is, in a measure—because he’s got feathers on him, and don’t belong to no church, perhaps; but otherwise he is just as much a human as you be. And I’ll tell you for why. A jay’s gifts and instincts, and feelings, and interests, cover the whole ground. A jay hasn’t got any more principle than a Congressman.
    Mark Twain [Samuel Langhorne Clemens] (1835–1910)

    To invent without scruple a new principle to every new phenomenon, instead of adapting it to the old; to overload our hypothesis with a variety of this kind, are certain proofs that none of these principles is the just one, and that we only desire, by a number of falsehoods, to cover our ignorance of the truth.
    David Hume (1711–1776)