Skip to content

Instantly share code, notes, and snippets.

@c0ldlimit
Last active August 29, 2015 14:16
Show Gist options
  • Select an option

  • Save c0ldlimit/cf97521568c97fa0f52e to your computer and use it in GitHub Desktop.

Select an option

Save c0ldlimit/cf97521568c97fa0f52e to your computer and use it in GitHub Desktop.

Revisions

  1. c0ldlimit revised this gist Mar 6, 2015. 2 changed files with 17 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions tuples.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    auto di = make_tuple(2.5, 3, 'c');
    cout << get<0>(di) // prints 2.5
    cout << get<char>(di); // prints 'c' (C++14)
    int three = get<1>(di);
    // ??? any reason tuples doesn't support [] indexing??
    12 changes: 12 additions & 0 deletions zoo.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    vector<unique_ptr<Animal>> zoo;
    zoo.emplace_back(new Elephant);
    zoo.emplace_back(new Zebra);
    zoo.emplace_back(new Bear);
    cout << "Feading time (f) or Bedtime (b)?";
    char c;
    cin >> c;
    void (Animal::*ap) () = c == 'f' ? &Animal::eat : &Animal::sleep;

    for (auto it = zoo.begin(); it != zoo.end(); it++) {
    ((**it).*ap)(); // can't use '->' with unique ptrs
    }
  2. c0ldlimit revised this gist Mar 6, 2015. 1 changed file with 13 additions and 0 deletions.
    13 changes: 13 additions & 0 deletions pointers.cpp
    Original file line number Diff line number Diff line change
    @@ -52,3 +52,16 @@ fp(2, 3); // language dereference fp for you - no need to dereference with '*'
    // the following line sonly works without captures
    fp = [](int i, int j) { return i + j; }



    // POINTER TO MEMBERS

    // more like an offset into A objects
    int A::*aip = &A::i; // not an address in the conventional sense
    void (A::*afp)(double) = &A::foo;
    A *ap = new A;
    A a;
    // combine the pointer to the ap
    ap->*aip = 3; // set ap->i to 3 -- could have been pointing to j but was defined to point to i
    (a.*afp)(3.141592); //??? why do you use '.' and not '->'

  3. c0ldlimit revised this gist Mar 6, 2015. 1 changed file with 6 additions and 3 deletions.
    9 changes: 6 additions & 3 deletions pointers.cpp
    Original file line number Diff line number Diff line change
    @@ -45,7 +45,10 @@ int (*fp)(int, int); // *fp can be called with 2 ints

    // let's show fp in action
    int f(int i, int j) { ... }
    fp = &f;
    fp(2, 3);
    fp = &f; // can also say
    fp = f; // same thing - assigning 'f' gives the address of the function
    auto fp = &f;
    fp(2, 3); // language dereference fp for you - no need to dereference with '*'
    // the following line sonly works without captures
    fp = [](int i, int j) { return i + j; }
    fp = [](int i, int j) { return i + j; }

  4. c0ldlimit revised this gist Mar 6, 2015. 1 changed file with 51 additions and 0 deletions.
    51 changes: 51 additions & 0 deletions pointers.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    // How to transfer ownership into and out of a unique_ptr
    unique_ptr<A> ap(new A); // ap owns the new A
    ap.reset(new A); // delete old A and own new A

    void f1(A *); // f1 will not del - not interested in managing the lifetime of the object
    void f2(A *); // f2 should delete - supposed f2 takes over ownership of the object - it's a transfer of ownership


    f1(ap.get()); // pass the pointer ????? doesn't this violate the unique pointer being the only object
    // that holds the pointer
    f2(ap.release()); // pass ptr and release ownership - will set ap to store nullptr after releasing

    // Funcs returning new obj should return a unique_ptr
    // because someone needs to own the object after it's created
    unique_ptr<A> Afactory();
    ap = Afactory();
    // you can't copy unique pointers because that would create ambiguous ownership.
    // you move them instead
    unique_ptr<A> ap2 = move(ap); // basically a promise that you do with what you want with ap

    // 'new' still returns a raw pointer to an object - this is bad practice to have an object who's owned
    // by not anything that has a destructor that will delete it
    auto ap = make_unique<A>(1, 2, 3); // new's the object with constructor argument - returns a unique pointer
    // same as
    unique_ptr<A> ap(new A(1, 2, 3));
    // or use make_shared

    // for an array of 10 A elements
    auto ap = make_unique<A[]>(10);

    // a raw pointer means you are using the object - you are not managing the object
    // down the road there will be a observerpointer


    // shared_ptr
    // only once the last user of the object goes away does the reference count go to zero and object goes away
    // a difference from unique_ptr - can copy
    // don't create two shared_ptr's from the same object

    // one time you are stuck using raw pointers - which is the 'this' pointer

    // POINTERS TO FUNCTIONS
    // point to the code to that function - so that the compiler can generate a call to that function
    int (*fp)(int, int); // *fp can be called with 2 ints

    // let's show fp in action
    int f(int i, int j) { ... }
    fp = &f;
    fp(2, 3);
    // the following line sonly works without captures
    fp = [](int i, int j) { return i + j; }
  5. c0ldlimit created this gist Mar 5, 2015.
    12 changes: 12 additions & 0 deletions lockfreestack.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    void
    Stack::push(int val)
    {
    StackHead expected = head.load();
    StackItem *newItem = new StackItem(val);
    StackHead newHead;
    newHead.link = newItem;
    do {
    newItem->next = expected.link;
    newHead.count = expected.count + 1;
    } while (!head.compare_exchange_weak(expected, newHead));
    }