Friday, 5 February 2021

Print a Char as a Number

 When you are interested in the number of bits in your  integer such as in embedded programming, you often find yourself trying to print an 8 bit byte and it appears as the character rather than the number.


uint8_t byValue{65};
std::cout << "Value:" << byValue << std::endl;
// Result
//     Value:A

This occurs because uint8_t acts like a C-style char.


You can overcome this by casting it to a larger integer type:
uint8_t byValue{65};
std::cout << "Value:" << static_cast<uint32_t>(byValue);
// Result
// 
    Value:65

But that isn't so readable.


Alternatively, you can promote it to an int value by using unary addition, add 0 to keep the same value.

uint8_t byValue{65};
std::cout << "Value:" << byValue + 0 << std::endl;
// Result
//     Value:65 

I find this easier to read.


Tuesday, 8 December 2020

Iterating through std::maps

 Map Iterators in C++17

I'm not going to comment on whether it is right or wrong to iterate through a map but bear in mind that there are other containers that are faster to iterate through.

This snippet shows the 3 ways I know to iterate through a map and the syntax to use the data. Note that the named pair is only available since C++17.

class MyClass
{
public:
    std::map<intint> m_map;
    MyClass():
        m_map{}
    {
    }
    void Print(const int a, const int b)
    {
        std::cout << "\ta:" << a << " b:" << b << std::endl;
    }
    void Run()
    {
        m_map[1]=2;
        m_map[2]=4;

        std::cout << "Iterator:\n";
        for (auto it = m_map.begin(); it != m_map.end(); it++)
        {
            Print(it->first, it->second);
        }

        std::cout << "Auto pair:\n";
        for (auto pr : m_map)
        {
            Print(pr.first, pr.second);
        }

        std::cout << "Named pair:\n";
        for (auto& [a,b] : m_map)
        {
            Print(a, b);
        }
    }
    /* prints:
        Iterator:
            a:1 b:2
            a:2 b:4
        Auto pair:
            a:1 b:2
            a:2 b:4
        Named pair:
            a:1 b:2
            a:2 b:4
    */
};

Wednesday, 2 December 2020

Planning a Day's Coding

 Planning a Day's Coding

There is a lot of advice and are a lot of tools online to plan your work but for short term planning as a busy software engineer, the following works for me.

  1. Write down on a piece of A4 all the jobs you think you need/want to do.
  2. Assign priorities based on the following:
Priority 1: Add essential functionality.
Priority 2: Add expected functionality.
Priority 3: Same functionality but tidier or improved code.
Priority 4: Documentation.
Priority 5: Optimisation.

Then, obviously work through jobs in priority order.

But also, spend some small amount of time working on something for which there is no allocated time.  This may be a little utility to help you build the code, speed backup, reorganise your desk or updating a blog ;-)

Friday, 9 December 2011

Using Boost Assign to initialise a list of a list.


std::list<std::list<int> > listOfListOfInts = list_of(list_of<int>(1)(2))
                                                     (list_of<int>(1)(2));
assert(listOfListOfInts.front().front() == 1);

Note that the inner lists need to the type hint. Visual Studio 2008 caused a compiler crash without it!

keywords for search: nest nested std stl list boost assign list_of initialise

Note: modern C++ has initialisation built in now.  Use {} for all initialisation.

Friday, 15 April 2011

The application failed to initialize properly 0x0000007b

Thanks Microsoft - what a helpful error message.

My little test application consisted of 3 projects one of which had the /clr switch. I removed that and it ran.

So if you get this error message I suggest you check all settings match.  If you get this error message in someone else's code then you probably have the wrong DLL and I can't help further.