op-new: allocate^construct #placement #IV

Popular IV topic. P41 [[more effective c++]] has an excellent summary:

  1. to BOTH allocate (on heap) and call constructor, use regular q(new)
  2. to allocate Without construction, use q(operator new)
    1. You can also use malloc. See https://stackoverflow.com/questions/8959635/malloc-placement-new-vs-new
  3. to call constructor on heap storage already allocated, use placement-new, which invokes ctor

The book has examples of Case 2 and Case 3.

Note it’s common to directly call constructor on stack and in global area, but on the heap, placement-new is the only way.

Placement-new is a popular interview topic (Jump, DRW and more …), rarely used in common projects.

## xx technicalities for QQ IV

We often call these “obscure details”. At the same level they are a small subset of a large amount of details, so we can’t possibly remember them all. Surprisingly, interviewers show certain patterns when picking which technicality to ask. Perhaps these items aren’t at the same level as the other obscure details??

These topics are typical of QQ — tough topics for IV only, not tough topics for GTD.

  • archetypical: which socket syscalls are blocking and when
  • select() syscall
  • some of the hundreds of g++ options?
  • $LD_LIBRARY_PATH
  • iterator categories? Seldom quizzed
  • hash table theoretical details? too theoretical to be part of this discussion

t_ivTechnicality33 is the tag

reverse linked list !recursion

My own solution. The set-up in the beginning is useful in coding demos.

#include <iostream>
using namespace std;
struct Node{
  int val; //payload
  Node * next;
  Node (int p, Node* n=NULL): val(p), next(n){}
};
Node _1(1); //tail
Node _2(2, &_1);
Node _3(3, &_2);
Node _4(4, &_3);
Node _5(5, &_4);

// Above is a useful, simple set-up of linked list for coding interview

Node * head = &_5;

int main(int argc, char *argv[]) {
   Node * a=head;
   Node * b=a->next;
   Node * c=b->next;
   // marker variables on 3 consecutive nodes

   a->next=NULL; // first fix the head node

   for (;;){
        b->next = a; //fix the "b" node
        a=b; b=c; c = c->next; //shift down the 3 markers
        // now between a and b there's no link; a is fixed; b is intact

        if (c == NULL){
                b->next = a;
                head = b;
                cout<<"Reverse Completed!"<<endl; break; } } for (Node * tmp=head;tmp;tmp = tmp->next){
      cout<< tmp->val <<endl;
   }
}

q(nm) instrumentation #learning notes

When you want to reduce the opacity of the c++ compiled artifacts, q(nm) is instrumental. It is related to other instrumentation tools like

c++filt
objdump
q(strings -a)

Subset of noteworthy features:
–print-file-name
–print-armap? Tested with my *.a file. The filename printed is different from the above
–line-numbers? Tested
–no-sort
–demangle? Similar to c++filt
–dynamic? for “certain” types of shared libraries
–extern-only

My default command line is

nm –print-armap –print-file-name –line-numbers –demangle

q(g++ -g -O) together

https://linux.die.net/man/1/g++ has a section specifically on debugging. It says

GCC allows you to use -g with -O

I think -g adds additional debug info into the binary to help debuggers; -O turns on complier optimization.

By default, our binaries are compiled with “-g3 -O2”. When I debug these binaries, I can see variables but lines are rearranged in source code, causing minor problems. See my blog posts on gdb.