factory returning smart ptr^pbclone #Sutter2013

Background — This is one example of “too many variations”. A simple idea become popular… People start mix-n-match it with other ideas and create many, many interesting questions. In practice we should stick to one or two best practices and avoid the other 50 variations.

Update: https://herbsutter.com/2013/05/30/gotw-90-solution-factories/ is a little too dogmatic as an article. A few take-aways:

  • returning raw ptr can indeed leak in real life. I would still do this in simple projects.
  • For a polymorphic type, return a unique_ptr by default. See also [[effModernC++]]. (Return shared_ptr only if the factory saves it in some internal state, but who does?)
  • For a non-polymorphic type, return a clone. Safely assume it’s copyable or movable in c++11.

I think it’s quite common (see [[safe c++]] P51) to have a factory function creating a heap “resource”. Given it’s on heap, factory must return the object by pointer. Easy to hit memory leak. Standard solution is return by ref count smart ptr.

Q: what if I forget to destruct the obj returned?
A: I feel it’s uncommon and unnatural. (More common mistake is to forget deletion.) Often the object returned (i.e. the smart pointer object) is on the stack – no way to “forget”.

Note if a (64-bit) raw pointer variable is on the stack, then the stack unwinding will deallocate/reclaim the 64-bit, but won’t call delete on it!

Q: what if the object returned is saved as a field like myUmbrella.resource1
A: I feel this is fine because the myUmbrella object will be destructed by someone’s responsibility. That would automatically destruct the smart pointer field this->resource1.

Alternative to the ref count smart ptr, we can also return a (boost) scoped pointer. See [[safe c++]].

Q: what if factory need to return a pointer to stack?
A: I don’t think so. Pointee goes out of scope….
A: I feel most “resources” are represented by a heapy thingy. Alternatively, it’s also conceivable to hold a “permanent” resource like globally. Memory management is non-issue.

 

 

By the way, [[safe c++]] also mentions the __common__ pattern of a (non-factory) function returning a pointer to an existing object, rather than a “new SomeClass”. The challenges and solutions are different.

Advertisements

%%strength: GTD with non-trivial research/innovation

If the research is too easy, then other developers can also do it, often faster.

If the research takes years, then it’s really for a researcher. I’m not a researcher (yet)

The project could have high value!

eg: Barcap proj
eg: b2bTradingEngine
eg: bloomberg adapter in GMDS
eg: home-made web server using WCF

[17] %%c++IV weaker cf java

https://bintanvictor.wordpress.com/2017/04/02/c-and-java-iv-tend-to-beat-us-in-2-ways-high-end/ shows my self-rating in job interviews for c++ vs java. (For real projects, I think the gap between my c++ and java is slightly smaller.)

I did spend lots of time reading and blogging about c++, not less than java, so

Q: why the persistent gap in my QQ IV performance?

  • –A #1: biggest reason — I feel a disproportionate number of my c++ interviews come from high end teams such as HFT shops. They go slightly lower-level than my java interviews. Overall, java is a slightly less specialist, less elitist, more commodity skill. Imagine if I get lower-level java interviews from the likes of Millennium, HSBC … I feel most (like 80%) of the java jobs in finance are “regular”; for Singapore c++, the percentage is below 50%. See the post on the c++ job offers I received.
    • my c++ interviews have slightly more IDE coding tests than my java interviews
    • If I avoid the high-end jobs, then my batting average will probably increase significantly.
  • –A #2: [QQ] Some of these interviewers basically treat c++ as a wrapper over C. In contrast, Java insulates you in a pure-java world on the Virtual Machine, so you don’t need to deal with the messy “borders” as c# and c++ developers do. If I could avoid these topics, then my c++ low-level tech IV performance is closer to Java performance. A lot of the tough C++ IV questions are about
    • linux, kernel tuning, system calls, OS performance monitoring, …
    • [C] *alloc family of functions, memory leak detectors
    • shared lib, linker, compiler,
    • [C] other command line developer tools … mostly designed for the “system programmer”.
    • [C] sockets, IPC
  • –A: [QQ] significantly simpler in terms of QQ — java as a language, when we ignore the add-on packages.
  • –A #4: cod`IV java^c++: threading is one reason my java coding IV is stronger than c++
  • –A: [QQ] STL containers — is much more complicated than java containers in job interviews, even if we ignore iterators, stl algorithms.
    • I spent more time studying STL than java collections but always failed to impress interviewers.
    • eg: in java container of T is always implicitly container of well-managed pointer  to T
  • –A: [QQ] halos — halos in my “java show”:  threading, collections, GC tuning … which make up for my java weaknesses elsewhere. No such halo in my “c++ show” I guess some people have a halo in templates, memory mgmt, shared_ptr, threading…
  • –A: unable to describe c++ projects.
  • Suggestion: Start from this blog and my books. Focus on a small subset of core topics in c++, /burn through/ the essential literature and develop a first halo —
    1. smart pointers (beyond shared_ptr)
      1. usage in big5
      2. usage in containers
    2. data structures in STL and boost, including c-str life-cycle management excluding the str* functions
    3. traditional big 3(dtor,op=, copier) but not rvr and move-semantic
    4. pbclone^pbref but not return-value-optimization
    5. const
    6. vptr, slicing, dynamic_cast
  • (Note this is all about QQ book knowledge, not coding skill!)
  • suggestion: secondary focus topics —
    1. c++11 threading
    2. heap memory management;
    3. socket tweaking;
    4. interface classes, pure virtual;
    5. design patterns
    6. shared memory
  • suggestion: continue to ignore some of the high complexity low leverage topics such as move semantics; iterators; const-correctedness; singleton; design patterns; operator overload … and many [[effC++]] topics

retreat to raw ptr from smart ptr ASAP

Raw ptr is in the fabric of C. Raw pointers interact/integrate with countless parts of the language in complex ways. Smart pointers are advertised as drop-in replacements but that advertisement may not cover all of those “interactions”:

  • double ptr
  • new/delete/free
  • ptr/ref layering
  • ptr to function
  • ptr to field
  • 2D array
  • array of ptr
  • ptr arithmetics
  • compare to NULL
  • ptr to const — smart ptr to const should be fine
  • “this” ptr
  • factory returning ptr — can it return a smart ptr?
  • address of ptr object

Personal suggestion (unconventional) — stick to the known best practices of smart ptr (such as storing them in containers). In all other situations, do not treat them as drop-in replacements but retrieve and use the raw ptr.

win10 system+user settings GUI

I prefer the controlPanel rather than the Settings screen… ControlPanel can show all the apps without the messy categories. Also, the winKey->search is unreliable when searching for a controlPanel app. Once again, Microsoft did a sloppy job.

To change any setting, I believe we always need one of those apps. Windows GUI try to hide the boundaries between the apps, and make it very very confusing.

waterContainer aka max product (j-i) * min(a[i],a[j]) streaming mode #Ashish/CSY

In the static, non-streaming context, the optimal solution is perhaps in my gmail (need to check). Now I feel this is identical to the  “medium” leetcode water-container problem:

Q: Given an array of size N (>=2) of non-negative numbers representing N walls, find a pair of walls that can hold the most water.

https://github.com/tiger40490/repo1/blob/cpp1/cpp/array/rainfallMaxContainer.cpp is my solution + an equivalent but much shorter solution by CSY, basically same as the idea below !

The brute force would evaluate (N-1)*N/2 pairs. We can reduce that significantly. Start with 1st/last, which is possibly the final winner. Save this as maxProductOfLoopA and evaluate a[0] vs a[9 i.e. last].

  • Suppose a[0] > a[9], then 1/9 , 2/9 , 3/9 etc are automatically eliminated.
  • Suppose a[0] < a[9], then 0/8, 0/7, 0/6 etc are automatically eliminated.
  • if a[0] == a[9], then 0/8, 0/7 etc and 1/9, 2/9 etc are automatically eliminated

You can visualize it as removing an outer layer from a NxN matrix. Note the matrix is triangular and has exactly one outer row and one outer column at any moment. In the first step, you either remove the outer row or outer column, or both.

Supposed you removed the “*/9” column. In 2nd step, we compute 2nd product at 0/8, and 2nd evaluation of a[0] vs a[8] and remove another outer layer.

In about N steps we should reduce the matrix to 1 remaining cell. This cell could be the final winner so we must evaluate it.

—-

Hi Ashish,

Let me change your question slightly. Input numbers come individually in a stream. Among all possible pairs of numbers, we want to compute and publish the latest maximum product:

    A(i, j) * B(i, j)

, where A(i, j) is the time lag j-i and B(i, j) is minimum(arr[i], arr[j])

NB: We will need to keep all numbers seen so far, since the the winning pair might include the first elements.

At any time there’s a current max. When we receive the 89th element, enter loop A:

compute the product for 1st/89th i.e. arr[0]/arr[88]
* If arr[0] > arr[88], then just exit the loop with this product as maxProductOfLoopA. No need to try 2nd/89th or 3rd/89th, as all those products are all smaller than 1st/89th. (This is the genius of the solution you told me.)
* otherwise, compute the product for 2nd/89th. If it exceeds maxProductOfLoopA, then update maxProductOfLoopA. Now check if arr[1] > arr[88]. If yes just exit loop A with maxProductOfLoopA
* otherwise compute the product for 3rd/89th….

Once we exit loopA, update the current max product using maxProductOfLoopA.

For streaming situation, I think this is one good solution, if not the optimal solution.

##[18]tips: increase lazer@localSys #esp.SG

  • volunteer to take on more prod support, selectively.
  • accumulate some email threads and go through some relevant ones with a buddy once a week, but need to be strategic and get manager buy-in
  • reduce distraction — muscle-building. Tough but doable.
  • reduce distraction — FSM. Rationalize time spent on FSM, until I’m over the hump
  • reduce distraction — kids. Rationalize time spent on boy, since it didn’t work well. I think I can bring him out less often, since outing is way too costly in terms of my time.
  • camp out and focus on 1) project delivery 2) localSys
  • absorbency — when you are in the mood to learn local sys, increase your absorbency capacity with good food, exercise etc
  • sunshine — is needed on localSys tough effort. My son’s effort need the same sunshine

python class^module(singleton)

Both modu5 and class2 are based on a idic. Both can contain “system-wide” i.e. global variables, effectively singleton objects.

A module is like a singleton class, without constructor or inheritance.

Global variables and singletons — I figured these out because 2nd time you import a module, the module-level objects aren’t created again! You can safely construct objects at the module level and all of them become global variables. Is this similar to c++ namespace variables?

Calling a method on a class goes through __getattr__ hook. Probably no such thing in a module?

Importing a regular module actually executes the *.py file – no such feature with a class.

Modules (not classes) are hooked into the (non-trivial) import mechanism.

Module functions vs Methods have differences. All module-level methods are like classmethods, so a module can be simpler to use if you want a simple singleton class.

See also
https://learnpythonthehardway.org/book/ex40.html
http://stackoverflow.com/questions/600190/python-choosing-between-modules-and-classes