Click here to Skip to main content
16,006,535 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Pointer Validation dynamic_cast<> Pin
Swinefeaster28-Jan-02 9:32
Swinefeaster28-Jan-02 9:32 
GeneralRe: Pointer Validation dynamic_cast<> Pin
Matt Gullett26-Jan-02 14:25
Matt Gullett26-Jan-02 14:25 
GeneralRe: Pointer Validation dynamic_cast<> Pin
Swinefeaster26-Jan-02 16:05
Swinefeaster26-Jan-02 16:05 
GeneralRe: Pointer Validation dynamic_cast<> Pin
26-Jan-02 16:45
suss26-Jan-02 16:45 
GeneralRe: Pointer Validation dynamic_cast<> Pin
Swinefeaster26-Jan-02 16:54
Swinefeaster26-Jan-02 16:54 
GeneralRe: Pointer Validation dynamic_cast<> Pin
Tim Smith26-Jan-02 17:13
Tim Smith26-Jan-02 17:13 
GeneralRe: Pointer Validation dynamic_cast<> Pin
Tim Smith26-Jan-02 17:17
Tim Smith26-Jan-02 17:17 
GeneralRe: Pointer Validation dynamic_cast<> Pin
Tim Smith26-Jan-02 17:09
Tim Smith26-Jan-02 17:09 
This works great excluding one VERY minor point. There is a chance that a later created object could have the same address as one just freed. Thus a cUser object could be using an old pointer which just happens to match a new one. (I have no idea how rare this might be.)

We got around this problem by using an unique index system. Here is the basic layout.

1. Assume you have a table going from 1 to 4096 (4096 is just for example and the table size can grow if needed.) Each element of this table consists of the handle match value and a pointer to the real objects.

2. The handle is generated by taking the index in the table and oring it with a counter specific to that index. For example, let us say the handle was a DWORD. The low word is the index into the table. The high word is the unique counter for that index. Every time the index is allocated for an object, the unique counter is incremented. Thus, the first time index 20 is allocated, the handle would be 0x00010014. The next time it is allocated it would be 0x00020014. When a handle is allocated, the generated handle should be stored in the handle match value for that index.

3. When a handle is freed, it is placed at the end of a deleted list. Thus, all indexes are used prior to an index getting reused. This helps to make sure that a long period of time goes by before a handle is reused.

4. The table need not be a fixed size. If you start with 4096 entries and use all but 256 up, you can double the size of the table. There are two reasons to do this. First, you don't run out of indexes. Second, it helps to make sure handles don't get reused to quickly. If you had 4095 handles allocated and then allocated and freed the last one 65536 times quickly, it would reuse a handle quickly.

5. To look up an object from the handle. Take the handle and AND off the unquie counter. Locate the index in the object table. Compare the full handle against the handle match value for that index. If they do not match, then someone is using an old handle. If they do match, return the pointer.

6. This index system is actually faster than maps.

7. HOWEVER, 9 times out of 10 a map is a better solution because there is little worry about reusing the same object address as a handle.

(Hmm, maybe I should submit my handle to object class to CP.)

Tim Smith
Descartes Systems Sciences, Inc.
GeneralRe: Pointer Validation dynamic_cast<> Pin
Swinefeaster26-Jan-02 20:26
Swinefeaster26-Jan-02 20:26 
GeneralRe: Pointer Validation dynamic_cast<> Pin
Swinefeaster26-Jan-02 21:32
Swinefeaster26-Jan-02 21:32 
GeneralRe: Pointer Validation dynamic_cast&lt;&gt; Pin
Swinefeaster17-Jul-03 10:05
Swinefeaster17-Jul-03 10:05 
GeneralWin98 vs WinNT Pin
Jim Crafton26-Jan-02 12:30
Jim Crafton26-Jan-02 12:30 
GeneralRe: Win98 vs WinNT Pin
567890123427-Jan-02 3:13
567890123427-Jan-02 3:13 
GeneralCode optimizations - script or compiled Pin
alex.barylski26-Jan-02 11:26
alex.barylski26-Jan-02 11:26 
GeneralRe: Code optimizations - script or compiled Pin
Rick York26-Jan-02 12:30
mveRick York26-Jan-02 12:30 
GeneralRe: Code optimizations - script or compiled Pin
alex.barylski26-Jan-02 13:01
alex.barylski26-Jan-02 13:01 
GeneralRe: Code optimizations - script or compiled Pin
markkuk27-Jan-02 19:56
markkuk27-Jan-02 19:56 
GeneralAccess violated Pin
Stephen Caldwell26-Jan-02 11:12
Stephen Caldwell26-Jan-02 11:12 
GeneralRe: Access violated Pin
Swinefeaster26-Jan-02 12:01
Swinefeaster26-Jan-02 12:01 
Generallocalhost Pin
Rickard Andersson2026-Jan-02 8:12
Rickard Andersson2026-Jan-02 8:12 
Generalcount words in file Pin
tbbooher26-Jan-02 8:04
tbbooher26-Jan-02 8:04 
GeneralRe: count words in file Pin
Jon Sagara26-Jan-02 8:17
Jon Sagara26-Jan-02 8:17 
GeneralRe: count words in file Pin
tbbooher26-Jan-02 12:33
tbbooher26-Jan-02 12:33 
GeneralRe: count words in file Pin
pba_28-Jan-02 8:48
pba_28-Jan-02 8:48 
GeneralCRecordset question Pin
jafrazee26-Jan-02 7:23
jafrazee26-Jan-02 7:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.