What are the barriers to understanding pointers?
type
THouse = class
private
FName : array[0..9] of Char;
public
constructor Create(name: PChar);
end;
---[ttttNNNNNNNNNN]--- ^ ^ | | | +- the FName array | +- overhead
THouse.Create("My house");
---[ttttNNNNNNNNNN]--- 1234My house
var
h: THouse;
begin
h := THouse.Create("My house");
...
h v ---[ttttNNNNNNNNNN]--- 1234My house
var
h1, h2: THouse;
begin
h1 := THouse.Create("My house");
h2 := h1; // copies the address, not the house
...
h1 v ---[ttttNNNNNNNNNN]--- 1234My house ^ h2
var
h: THouse;
begin
h := THouse.Create("My house");
...
h.Free;
h := nil;
h <--+ v +- before free ---[ttttNNNNNNNNNN]--- | 1234My house <--+ h (now points nowhere) <--+ +- after free ---------------------- | (note, memory might still xx34My house <--+ contain some data)
var
h: THouse;
begin
h := THouse.Create("My house");
...
h.Free;
... // forgot to clear h here
h.OpenFrontDoor; // will most likely fail
h <--+ v +- before free ---[ttttNNNNNNNNNN]--- | 1234My house <--+ h <--+ v +- after free ---------------------- | xx34My house <--+
var
h: THouse;
begin
h := THouse.Create("My house");
h := THouse.Create("My house"); // uh-oh, what happened to our first house?
...
h.Free;
h := nil;
h v ---[ttttNNNNNNNNNN]--- 1234My house
h v ---[ttttNNNNNNNNNN]---[ttttNNNNNNNNNN] 1234My house 5678My house
procedure OpenTheFrontDoorOfANewHouse;
var
h: THouse;
begin
h := THouse.Create("My house");
h.OpenFrontDoor;
// uh-oh, no .Free here, where does the address go?
end;
h <--+ v +- before losing pointer ---[ttttNNNNNNNNNN]--- | 1234My house <--+ h (now points nowhere) <--+ +- after losing pointer ---[ttttNNNNNNNNNN]--- | 1234My house <--+
var
h1, h2: THouse;
begin
h1 := THouse.Create("My house");
h2 := h1; // copies the address, not the house
...
h1.Free;
h1 := nil;
h2.OpenFrontDoor; // uh-oh, what happened to our house?
var
h1, h2: THouse;
begin
h1 := THouse.Create("My house");
h2 := THouse.Create("My other house somewhere");
^-----------------------^
longer than 10 characters
0123456789 <-- 10 characters
h1 v -----------------------[ttttNNNNNNNNNN] 5678My house
h2 h1 v v ---[ttttNNNNNNNNNN]----[ttttNNNNNNNNNN] 1234My other house somewhereouse ^---+--^ | +- overwritten
var
h1, h2: THouse;
begin
h1 := THouse.Create("Home");
h2 := THouse.Create("Cabin");
h1.NextHouse := h2;
var
h1, h2: THouse;
h: THouse;
begin
h1 := THouse.Create("Home");
h2 := THouse.Create("Cabin");
h1.NextHouse := h2;
...
h := h1;
while h <> nil do
begin
h.LockAllDoors;
h.CloseAllWindows;
h := h.NextHouse;
end;
h1 h2 v v ---[ttttNNNNNNNNNNLLLL]----[ttttNNNNNNNNNNLLLL] 1234Home + 5678Cabin + | ^ | +--------+ * (no link)
h1 h2 v v ---[ttttNNNNNNNNNN]---[ttttNNNNNNNNNN] 1234My house 5678My house
h1 (=4) h2 (=28) v v ---[ttttNNNNNNNNNNLLLL]----[ttttNNNNNNNNNNLLLL] 1234Home 0028 5678Cabin 0000 | ^ | +--------+ * (no link)
Source: By David McGraw as answer to the question
This code snippet was collected from stackoverflow, and is licensed under CC BY-SA 2.5
Related code-snippets:
- How do I use the C socket API in C++ on Linux?
- What's the API for GTK messagebox?
- Why am I getting double free error when I call realloc()?
- How do I map Streams in C#. Mapping Streams to Data Structures in C#: Can I understand the structure of Streams?
- How do you format an unsigned long int using printf?
- As function arguments, passing multidimensional arrays in C. In C, passing multidimensional arrays are no longer used and can be used as function arguments (again in a code library) in C. The examples below illustrate this.
- How do you create a sparse array?
- String.indexOf in C is the most important function. It is useful for any user of C with little knowledge about C syntax.
- Is lazy construction of singleton a thread safe?
- How to avoid redefining VERSION, PACKAGE, etc. How to avoid redefining VERSION, PACKAGE, etc.?
- How do I make a GUI?
- Of Memory Management, Heap Corruption, and C++. Of Memory Management, C++.
- Testing a function that throws on failure.
- What is the difference between shift operators (, >>) and logical operators in C?
- How can I redirect stderr from calls to fprintf?