dlopen^LoadLibrary # plugin

Windows doesn’t have the dlopen API, but many techniques are similar on Windows LoadLibrary API.


  • UNIX-like operating systems such as macOS, Linux … uses dlopen(), dlsym() etc
  • Windows uses LoadLibrary() and GetProcAddress() etc

I was never asked about this in interviews, and never needed this feature. Useful knowledge for a c++ veteran though.


##10 c++coding habits to optimize perf

Many of these suggestions are based on [[optimized c++]]

· #1 Habit – in c++ at least, ++counter performance is strictly “better or equal to” counter++. If there’s no compelling reason, I would prefer the former.

· #2 Habit – in a for loop, one of the beginning and ending values is more expensive to evaluate. Choose the more expensive one as the beginning value, so you don’t evaluate it over and over. Some people object that compiler can cache the more expensive end value, but 2016 tests show otherwise.

· If a method can be static, then make it static. Good for performance and semantics.

· For a small if-else-if block, put the most likely scenario first. May affect readability. Worthwhile only in a hot spot.

· For a long if-elif-elif-elif-elif block, a switch statement performance is strictly “greater or equal”

· For-loop starts by checking the condition (2nd component in header). If this initial check is redundant (as often is), then use a do-while loop

· Call a loop in a function, rather than call a function in a loop. Another micro-optimization.

##boost modules USED ]finance n asked]IV

#1) shared_ptr (+ intrusive_ptr) — I feel more than half  (70%?) of the “finance” boost usage is here. I feel every architect who chooses boost will use shared_ptr
#2) boost thread
#3) hash tables

Morgan uses Function, Tuple, Any, TypeTraits, mpl

Gregory (RTS) said bbg used shared_ptr, hash tables and function binders from boost

Overall, I feel these topics are mostly QQ types, frequently asked in IV (esp. those in bold) but not central to the language. I would put only smart pointers in my Tier 1 or Tier 2.

—— other modules used in my systems
* tuple
* regex
* noncopyable — for singletons
** private derivation
** prevents synthesized {op= and copier}, since base class is noncopyable.

* polymorphic_cast
* numeric_cast — mostly int types, also float types
* bind?
* scoped_ptr — as non-copyable [1] stackVar,
[1] different from auto_ptr

##some benefits@learning c++, even if no salary increase

  1. After learning c++, i am fairly confident I could if I must pick up c# in a few (4?) months and start passing interviews. C++ is inherently tougher than java and C#. Java and C# both have large libraries, but the core languages are significantly simpler/cleaner than c++.
  2. After learning C++, i have found python and perl easier to understand and master since both are written in C/C++. I now believe some people who claim they could pick up a new language in a few months. Those languages have their roots in C/C++.
    • The basic challenges of scope+namespace, object lifetime, heap/stack, pointers, memory allocation, object construction, pass-by-ref/value, arrays, function pointer, exceptions, nested struct+array+pointer… are faced by every language designer. Many of these challenges depend on basic library, which is invariably C.
    • The common OO challenges of inheritance, virtual, static/non-static, HAS-A/IS-A, constructor, downcast, … are faced by every OO language designer. Many of them borrow from java, which borrows from C++ and smalltalk
  3. threading — java remains the gold standard but c++ currency support is more complex, harder to understand and offers some low-level insight
  4. memory management — c++ offers insight into JVM and CLR
  5. c++ gave me other insight into java, esp. GC, JVM, overriding, references, heap/stack, sizeof, …

##GTD topics for c++ coding drill

After I gain confidence with the basic tasks and move on to higher tasks, i frequently found gaping holes in my foundation. I then face a scary choice. If I admit and shift to the lower gear, I subconsciously feel fake and deeply disappointed with my progress — “after so many years, still at Level 1”. Well, truth is, even after 10 years of java (or c#, SQL or perl) most developers would lack some of the Level 1 skills, because they didn’t need to use them. Basics such as equals/hashCode, wait/notify, interrupt, most basic operations on threads, bigO of collection operations (basic), overriding vs overloading…

— Here are some __coding__ practice areas for GTD (not IV) improvement in c++:
* basic array, pointer/reference, free functions, func pointers. No classes. Plain C
* basic class inheritance, virtual function, new/delete, big3
* string manipulation? understated in books but heavily quizzed.
* basic IO using console and files
* consumption of existing templates. Vast majority of Wall St c++ teams don’t create templates (or design thread constructs). That’s the job of specialists in the bank.
* STL – basic and advanced.
* basic macros including assertion
* a lot of multi-unit compilation topics — much trickier than you think
* static
———-See separate blog posts on the most essential skills with STL

** containers are the real reason to use STL. Iterators and algos are reluctantly adopted.
** remove isn’t common. Add, lookup, find and interation are popular.
** …

–less quizzed
* basic operator overloading
* basic smart pointers

[[linux programmer’s toolbox]]

MALLOC_CHECK_ is a glibc env var
–debugger on optimized code
P558 Sometimes without compiler optimization performance is unacceptable.

To prevent optimizer removing your variables, mark them volatile.

An inline function may not appear in call stack. Consider “-fno-inline”

–P569 double-free may not show issues until the next free() or malloc()

–P470 – 472 sar command 
can show per-process performance data

can monitor network devices

—P515 printf + macros for debugging

buffering behavior differs between terminal ^ log files

## loaded words in c++ literature

“safety” means exception …
“resource” is always represented by an object on heap (accessed via a pointer).
“life cycle” means destructor
“owner” means deleter of a heapy thingy. Non-heap objects need no owner. Owner has just one job — deletion.

“static” is often used without qualification and by default means the most common/popular static object — the locals
“functor” often includes function pointer, at least in some contexts
The “resource” in RAII means any memory allocated from the heap

##[12] some c++(mostly IV)questions

Q: what if I declare but not implement my dtor?
A: private copy ctor is often left that way. Linker will break.
Q: how to access a c++ api across network
A: corba?
Q: default behavior of the q(less) class-template
A: I have at least 2 posts on this. Basically it calls operator< in the target type (say Trade class). If missing, then compiler fails.
Q: how do you detect memory leak?
A: valgrind
%%A: customize op-new and op-delete to maintain counters?
Q: in java, i can bind a template type param — bound params. c++?
%%A: no, but I have a blog post on the alternative solutions
Q891: are assignment op usually marked const?
%%A: no it must modify the content.
Q891a: what if i override the default one with a non-const op=? will the default one still exist?
A: I think both will co-exist
Q: do people pass std::strings by ref? See Absolute C++
%%A: yes they should
Q: unwrap a null ptr
A: undefined behavior. Often termination.
Q: delete a null ptr
A: C++ guarantees that operator delete checks its argument for null-ness. If the argument is 0, the delete expression has no effect.
Q: comparing null pointers?
A: In C and C++ programming, two null pointers are guaranteed to compare equal
Q: use a ref to a reclaimed address?
A: Rare IV… undefined. May crash.
Q: can i declare a Vector to hold both Integers and Floats?
A: slicing. To get polymorphism, use ptr or ref.
Q: what op must NOT be overloaded as member func?
A: rare IV…… qq(.) is one of five.
Q: Can i declare an Insect variable to hold an Ant object, without using pointers? “Ant a; Insect i = a;”?
A: see post on slicing
Q: Without STL or malloc (both using heap), how does C handle large data structures.
%%A: I used to deal with large amounts of data without malloc. Global var, linked data structure, arrays
%%A: I can have a large array in main() or as a static variable, then use it as a “heap”

## c++11 features I understand as significant

Here are the subset of C++11 features I understand as significant. Some significant c++11 features I don’t appreciate, such as lambda.

#1 move semantics
– std::move and forward

#2 Lib: smart pointers
– make_shared() etc

#3 Lib: thread library
– atomic<int>


#4 type traits

## c++ features used on Wall St, again

STL, boost
array-pointer integration (A major strength in C)
dynamic cast, but not RTTI or vptrcustomized operator-new and operator-delete

–These features are less used
tricky operator overload
template class development
Now we know that on Wall Street, C++ OO designs are primitive compared to Java OO designs

master list — home grown c++ jargons

(keep in the this blog …)
* a dummy type name  or a type-param is the T in
* a CONCRETE-template-class is an INSTANCE of a template, with a defined actual type or TYPE-ARG

copy-controls — copier, assignment, destructors. [[c++ primer]]
nonref — a variable that’s not pointer or reference
primitives — primitive types, primitive vars ie non-class types
copier — copy constructor
noarg — the noarg constructor
heapObj — not “heapVar”
lval — lvalue
locals = stackvars — local auto vars
virtuals — virtual methods
pbclone, pbref
mutators, inspectors — non-const and const functions (including “methods”)
getters, setters — methods

## low-level c++(vs java) sys design considerations #1st take

Compared to java architect, a c++ architect needs to worry about more things —

* memory management — avoid double delete, dangling ptr access, leak…
* ptr ownership (who will delete) scheme
* smart ptr — when, where
* copy ctor, op=
* what objects to put on heap ^ stack ^ global
* choose methods ^ static methods ^ free functions
* when to use templates, with the benefit and complexity

##y c++has no Java features like …

* packages? There are. See the book on large scale c++ system
* interfaces? Well, the pure abstract base class in c++ is the best practice for MI.
* VM? i think c++ app runs in a runtime environment just like a Perl script.
* VM threads? i think only the standard OS threads are available? Wrong. See the c++ thread book.
* reflection? RTTI is limited in feature. Python has significantly richer reflection features than compiled languages.