Sunday, December 29, 2019

Example of Intrusive Singly Linked List

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>



typedef struct list_item {
    int val;
    void *next;
} list_item;


typedef struct List {
    list_item *head;
    list_item *prev;
} List;


list_item *create_list_item(int val)
{
    list_item *p = (list_item *)malloc(sizeof(list_item));
    if (p)
    {
        p->val = val;
        p->next = NULL;
    }
    return p;
}


void add_list_item_into_node(List *list, int val)
{
    list_item *newlist_item = create_list_item(val);
    if (list->prev != NULL)
    {
        list->prev->next = newlist_item;
        list->prev = newlist_item;
    }
    else
    {
        list->head = newlist_item;
        list->prev = newlist_item;
        printf("FIRST! head==prev ? %d, %p\n", list->head==list->prev, list->head);
    }

    printf("%p added; head=%p, prev=%p\n", newlist_item, list->head, list->prev);
}


void init_list(List *l)
{
    l->head = l->prev = NULL;
}


void clear_list(List *l)
{
    list_item *p = l->head;
    while (p != NULL)
    {
        list_item *pn = (list_item *)p->next; 
        printf("Freeing %p, next p is=%p\n", p, pn);
        free(p);
        p = pn;
    }
    l->head = l->prev = NULL;
}


typedef void (*callback_t)(list_item *);

void print(list_item *p)
{
    printf("p=%p: val=%d\n", p, p->val);
}

void iterate(List *list, callback_t cbk)
{
    list_item *p = list->head;
    while (p)
    {
        cbk(p);
        p = p->next;
    }
}

int main()
{
    List mylist;
    init_list(&mylist);

    for(int i=0; i<10; ++i)
    {
        add_list_item_into_node(&mylist, i*2+1);
    }

   iterate(&mylist, print);

    // cleanup; starting from head
    printf("Cleaning up!\n");
    clear_list(&mylist);
    iterate(&mylist, print);

    printf("head = %p, prev=%p\n", mylist.head, mylist.prev);
}

Friday, September 20, 2019

Unique Pointer (Smart Pointer)

The following example shows how to use unique_ptr which semantically 'moves' the data during assignment:


#include <iostream>
#include <memory>


using namespace std;


class MyClass
{
public:

#if COMPILE_ERROR
    MyClass
    (
        std::unique_ptr<int> const &  pOpt = std::unique_ptr<int>(nullptr) 
    ) : m_ptr(std::move(pOpt))
    {
    }
#else
    explicit MyClass
    ( 
        std::unique_ptr<int> pOpt = std::unique_ptr<int>(nullptr)
    ) : m_ptr(std::move(pOpt))
    {
        std::cout << __FUNCTION__ << "() : m_ptr=" << m_ptr.get() << std::endl;
    }

    MyClass() = delete;

#endif
    friend ostream& operator<<(ostream& os, const MyClass& c);

private:
    std::unique_ptr<int>  m_ptr;
};


template<typename T>
std::unique_ptr<T>& pass_through(std::unique_ptr<T>& p)
{
    if (p != nullptr)
        std::cout << *p << std::endl;
    else
        std::cout << "nullptr" << std::endl;
    return p;
}


ostream& operator<<(ostream& os, const MyClass& c)
{
    if (c.m_ptr != nullptr)
        return os << __FUNCTION__ << "(): " <<  *c.m_ptr;
    else
        return os << __FUNCTION__ << "(): nullptr";
}


int main()
{
    std::unique_ptr<int> pInt(new int(15));

    MyClass data();
    MyClass data2(std::move(pInt));

    pass_through(pInt);
    
    std::cout << "data = " << data << std::endl;
    std::cout << "data2 = " << data2 << std::endl;
}


MyClass() : m_ptr=0x7430d0

nullptr

data = 1

data2 = operator<<(): 15




Wednesday, March 13, 2019

Callback via C++ template

The following code would make a callback via template:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include <iostream>


template <typename F>
void call(F func)
{
    func();
}


void print()
{
    std::cout << __FUNCTION__ << "()" << std::endl;
}

int main()
{
    call(print);
}