code-tutorial.com

The best resources for developers

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)

Tags: c c++ pointers

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: