Monday, July 2, 2012

3 meanings of "POINTER" (+ tip on delete-this)

(headfirst means the post on the book [[headfirst C]])

When people say "receive a pointer", "wild pointer", "delete a pointer", "compare two pointers", "include two pointer members"... a pointer can mean 3 slight different things. A lot of fundamental issues lie in those subtle differences. (To keep things simple, let's assume a 32 bit box.)

1a) 32bit Object -- a pointer can occupy (heap, stack, global) memory and therefore (by ARM) qualify as a 32bit Object.
** For example, if a pointer is a field in a C struct or C# struct/class, then it must be allocated 32 bits when the struct is allocated.
** For example, if a double pointer p5 points to a pointer p8. Since p8 has an address it must occupy memory so p8 is by definition an object. But p5 may not be an object if without an address.

1b/2b) address -- in some contexts, a "pointer" can mean an address. An address is pure Information without physical form, with zero memory footprint. When you pass myfloatPtr + 2 to function(), you are passing an address into function(). This address may not be saved in any 32-bit object. I suspect compiler often uses registers (not memory) to hold such addresses.
** For example, in C An array name is always a const-pointer (not ptr-to-const) to an already-allocated array in memory.  For an array of 10 doubles, 640 bits are already allocated. However, compiler may not need to allocate 32 bits to hold the array name. The array name like "myArray" is pure address not an object.
** For example, in C if I save the address-of-something in a transient, temp variable, compiler may optimize away the stack-allocation of that variable.
** see also headfirst

Fundamentally, if a symbolic name is permanently attached to an Address (permanent alias?), then compiler need not allocate 32bit of heap/stack for the symbolic name. Compiler can simply translate the name into the address. On the other hand, Compiler may need to allocate 32 bits if the symbolic name can rebind to a 2nd address.

2) nickname -- Most commonly, a pointer means a pointer variable as in (2a) below. Remember a nickname exists in source code as a memorable nickname for something but binding is impermanent. When we look at code snippets, we may not know whether a variable is a

** 2a) nick name for a 32-bit pointer object -- most common
** 2b/1b) or nick name for a pure address. An array-name like myArray in source code is such a nickname -- rather confusing. Note there's no 32-bit pointer object in myArray
** See also headfirst

A 32-bit pointer Object often has multiple nick names. In general, any object can have multiple nick names. If a nickname is transient or never refers to a 2nd Object, then compiler can optimize it into 2b.

---- some implications ----
"delete this" ----- When you delete a pointer, you invoke delete() on an Address, including the controversial "delete this" -- The entire Object is bulldozed but "delete this" is fine because "this" is treated as an Address. However, the method that performs "delete this" must ensure the object is not allocated by malloc() and not a nonref field (resident object) "embedded in the real estate" of another heapy thingy object. In the case of multiple inheritance, it must not be embedded in the real estate of a derived class instance. See

reference counting ---- In reference counting for, say, a shared data structure, we count "handles" on our pointee. Do we count pointer Variables or pointer Objects? I'd say neither -- we count Address usage. If a thread has a function1() in its top "frame" and function1() is using our target data structure we must count it and not de-allocate it with a bulldozer. This function1() does not necessarily use any 32-bit pointer object to hold this Address. It might make do without a pointer Variable if it gets this address returned from another function.

vptr and "this" pointers ---- The vptr is always a pointer Object taking up 32 bit real estate in each instance (java/c# classes and Polymorphic C++ classes). How about the "this" pointer? Probably a pointer Variable (nickname) -- See

pointer comparison ---- When you compare 2 pointers, you compare the addresses represented by 2 pointer variables. Apart from the 2 pointee objects A and B, there may not be two 32bit pointer Objects in memory whose 32 bits hold A's or B's addresses.

smart pointer ---- When you pass a raw pointer to a smart pointer, you pass an Address, not necessarily a pointer Object or pointer Variable. A smart pointer is always an Object, never a pure Address.

wild pointer ---- is a pointer Address, but a dangerous address. Object at the address is now bulldozed/reclaimed. SomeLibrary.getResource() may return an Address which becomes wild before you use it. If you don't give a nick name to that returned value, it remains a wild pointer Address.
** now I feel even a stack object can be bulldozed, creating a wild pointer. getResource() may mistakenly return a pointer to an auto variable i.e. stack object

Most of the time we aren't sure if a nickname refers to a 32-bit pointer object or pure information. The prime example of a 32-bit pointer object is a pointer field in a struct.

No comments:

Total Pageviews

my favorite topics (labels)

_fuxi (302) _misLabel (13) _orig? (3) _rm (2) _vague (2) clarified (58) cpp (39) cpp_const (22) cpp_real (76) cpp/java/c# (101) cppBig4 (54) cppSmartPtr (35) cppSTL (33) cppSTL_itr (27) cppSTL_real (26) cppTemplate (28) creditMkt (14) db (65) db_sybase (43) deepUnder (31) dotnet (20) ECN (27) econ/bank` (36) fin/sys_misc (43) finGreek (34) finReal (45) finRisk (30) finTechDesign (46) finTechMisc (32) finVol (66) FixedIncom (28) fMath (7) fMathOption (33) fMathStoch (67) forex (39) gr8IV_Q (46) GTD_skill (15) GUI_event (30) inMemDB (42) intuit_math (41) intuitFinance (57) javaMisc (68) javaServerSide (13) lambda/delegate (22) marketData (28) math (10) mathStat (55) memIssue (8) memMgmt (66) metaProgram` (6) OO_Design (84) original_content (749) polymorphic/vptr (40) productive (21) ptr/ref (48) py (28) reflect (8) script`/unix (82) socket/stream (39) subquery/join (30) subvert (13) swing/wpf (9) sysProgram` (16) thread (164) thread_CAS (15) thread_cpp (28) Thread* (22) timeSaver (80) transactional (23) tune (24) tuneDB (40) tuneLatency (30) z_ajax (9) z_algoDataStruct (41) z_arch (26) z_arch_job (27) z_automateTest (17) z_autoTrad` (19) z_bestPractice (39) z_bold (83) z_bondMath (35) z_book (18) z_boost (19) z_byRef^Val (32) z_c#GUI (43) z_c#misc (80) z_cast/convert (28) z_container (67) z_cStr/arr (39) z_Favorite* (8) z_FIX (15) z_forex (48) z_fwd_Deal (18) z_gz=job (33) z_gzBig20 (13) z_gzMgr (13) z_gzPain (20) z_gzThreat (19) z_hib (19) z_IDE (52) z_ikm (5) z_IR_misc (36) z_IRS (26) z_javaWeb (28) z_jdbc (10) z_jobFinTech (46) z_jobHunt (20) z_jobRealXp (10) z_jobStrength (15) z_jobUS^asia (27) z_letter (42) z_linq (10) z_memberHid` (11) z_MOM (54) z_nestedClass (5) z_oq (24) z_PCP (12) z_pearl (1) z_php (20) z_prodSupport (7) z_py (31) z_quant (14) z_regex (8) z_rv (38) z_skillist (48) z_slic`Problem (6) z_SOA (14) z_spring (25) z_src_code (8) z_swingMisc (50) z_swingTable (26) z_unpublish (2) z_VBA/Excel (8) z_windoz (17) z_wpfCommand (9)

About Me

New York (Time Square), NY, United States