Blocks, Memory and the Aliasing I Had to Give Up

Deleting rubbish

OK, I had a previous post that started this discussion but it was not very complete. It is about the heap, the block it hands out, and the cast I relied on to get between them. So I deleted that post. This is the new version of that discussion. Design story is then continued, so see the next post about text and subtext and how those structures also work together.

So, how to get the best out of a heap delivering a classic block of three members, a pointer, a size and a capacity, and how it then get used by various typed data structures.

The structures below are what I ended up with, but the full design process is outlined in full after that. To be honest the full journey is written up here so I can remember the pros and cons of each solution I tried. Note that the journey was done with Claude (Opus 5.5 Medium) pointing out the pros and cons of each option and helping with refactoring.

TLDR: The pointer type is set by a template, so a utf8block_s points at char8_t and a utf32block_s points at u32. The heap does not care about the type, only the item size. So it hands out memory_s, a block whose pointer is just u8(*)[_itemBytes_].

template <typename _item_t_, u32 _itemBytes_ = sizeof(_item_t_)>
struct _block_s
{
    _item_t_* pointer{nullptr};
    u32       size{0};
    u32       capacity{0};
};

template <u32 _itemBytes_>
struct _block_s<void, _itemBytes_>
{
    u8(*pointer)[_itemBytes_] { nullptr };
    u32 size{0};
    u32 capacity{0};
};

template <u32 _itemBytes_>
using memory_s = _block_s<void, _itemBytes_>;

Same three members, same 16 bytes. The heap takes and returns memory_s, and my code wants typed blocks. Originally the call sites just cast their way to the new type:

utf32Block_s utf32;
memory_s& utf32AsMemory = *(memory_s*)&utf32;
utf32AsMemory = u32Heap_.allocate(utf32.size);
defer_(u32Heap_.deallocate(utf32AsMemory));

It worked, but every function that touched the heap had this type casting. One even used the same memory as two text types at once, with the comment “Subtle: Abusing utf7 as the same memory as the final utf8.” This is clearly messy even if it does do exactly what I wanted. A new typed variable name that was the same structure the the heap gave back.

The conversion operators, and the aliasing

I tried to make it less messy by putting the casts into _block_s itself. My commit note said “Much cleaner to add the conversions to the block class than litter the main code.” A typed block could now turn itself into a memory_s reference whenever the heap wanted one:

// Auto conversion to this block type.
// Cast the pointer to avoid triggering the reference conversion operator
inline operator _block_s<void, _itemBytes_>& () { return *(_block_s<void, _itemBytes_>*)this; }
inline operator const _block_s<void, _itemBytes_>& () const { return *(const _block_s<void, _itemBytes_>*)this; }

That second comment is its own small story. The first version wrote (_block_s<void, _itemBytes_>&)*this. A C style cast to a reference type will happily use the conversion operator you are in the middle of writing. So it called itself. Casting the pointer and then dereferencing it does not go near the operator.

Now deallocate(utf8) just worked, and the call sites were clean. I knew it was not strictly legal, so text_c.h got a guard at the top:

#if defined(__GNUC__) || defined(__clang__)
#error "STRICT ALIASING VIOLATION: ... You must compile with -fno-strict-aliasing or rewrite the cast using std::bit_cast."
#endif

The problem is the strict aliasing rule. C++ says an object may only be read or written through its own type, plus char and a few others. A utf8block_s and a memory_s are two different types, however identical their bytes. So the optimiser may assume a write through one can never change the other. It can keep utf8.pointer in a register while deallocate() sets the memory_s pointer to nullptr, and then use the old value.

MSVC does not optimise on that rule, which is why I never saw a problem. GCC and Clang do. So the code was correct on one compiler, by luck, and I wanted it to be correct even though the plan is to only have the code on Windows. My commit notes: “I still think C++ is stupid here. MSVC did not alias but other compilers did. Sigh.”

The ways out I did not like

I already had two possible fixes. Neither is good, and the rest are not much better.

Compile with -fno-strict-aliasing

Tell GCC and Clang to behave like MSVC and stop assuming different types never overlap.

Blocked by: the code is still not C++. It is correct only under a build switch, and a switch does not travel with a header. Anyone who drops block_s.h into their project has to know. It also turns the optimisation off for the whole build, not just my two operators.

std::bit_cast

The legal way to see the bytes of one type as another.

Blocked by: it makes a copy. bit_cast returns a new value, so you can read a block as memory but you cannot write back through it. deallocate() has to clear the caller’s block, and allocate() has to fill it. It is also a std header in a library whose whole point is no CRT. This is another C++ gem really. What does bit_cast mean to you? Cast or Copy (double sigh).

memcpy in and out

Copy the block into a memory_s, call the heap, copy it back. That is legal and the optimiser removes most of it.

Blocked by: The code was changed to memcpy instead of a cast. Every call site, two extra lines. It is blocked because its ugly and dumb.

A union of the two block types

Put the typed block and the memory_s in a union and use whichever one the call needs.

Blocked by: I had been here already with the slice. Reading the member you did not write last is fine in C and undefined in C++. Same problem as aliasing, just a different spelling. It is also a warning to have nameless unions.

A move assignment from memory

This one was also prototyped. Give the typed block an operator= that takes a memory_s&&, takes its pointer and zeroes the source.

_block_s& operator=(_block_s<void, _itemBytes_>&& memory) noexcept
{
    pointer = (_item_t_*)memory.pointer;
    size = memory.size;
    capacity = memory.capacity;

    memory.pointer = nullptr;
    memory.size = 0;
    memory.capacity = 0;

    return *this;
}

Blocked by: two things. Declaring an operator= cost me the plain {pointer, size, capacity} brace initialiser, so I had to write a constructor to put it back. And it only goes one way. The block coming back out of allocate() was fine, but deallocate(), copy() and clear() still wanted a memory_s&, so I still needed the cast the other way. It sat commented out in block_s.h for a while.

AttemptBlocked by
Casts at every call sitemessy; strict aliasing
Reference conversion operatorsstrict aliasing on GCC and Clang
-fno-strict-aliasingstill not C++; a flag does not travel with a header
std::bit_casta copy, so no write back; std in a no CRT library
memcpy in and outthe call site mess again
Unionundefined behaviour in C++
Move assignment from memoryone way only; lost the brace initialiser

Conversion during copy assignment

The fix was to stop pretending one object is two types. Each direction gets its own answer.

From the heap to my code, memory becomes a block by copying it into a new one. That is a constructor, not a cast:

// Conversion during copy assignment.
inline constexpr _block_s(const _block_s<void, _itemBytes_>& memory) : 
    pointer{(_item_t_*)memory.pointer}, size{memory.size}, capacity{memory.capacity} {}
inline constexpr _block_s(_block_s<void, _itemBytes_>& memory) : 
    pointer{(_item_t_*)memory.pointer}, size{memory.size}, capacity{memory.capacity} {}

So utf8block_s output = utf8heap_.allocate(size); builds a brand new utf8block_s from the returned memory_s. Two objects, each read through its own type. The pointer member is cast, and that is fine: casting a pointer value is not aliasing, reading an object through the wrong type is. The copy is three members and, critically, gets optimised away.

Once the block declares its own constructors it is no longer an aggregate, so the plain {pointer, size, capacity} has to be written out as a constructor too. That is the “Put back list initialiser” line in block_s.h. I added one for borrowed data at the same time, which sets the capacity to zero:

inline constexpr _block_s(_item_t_* pointer, u32 size, u32 capacity) : pointer{pointer}, size{size}, capacity{capacity} {}
inline constexpr _block_s(const _item_t_* pointer, u32 size) : pointer{(_item_t_*)pointer}, size{size}, capacity{0} {}

The memory_s side got locked down. It can not be copied, only moved:

_block_s(const _block_s&) = delete;
_block_s& operator=(const _block_s&) = delete;
_block_s& operator=(_block_s&&) = default;

Raw memory is the one thing I do not want two copies of lying around. It comes out of allocate() and goes straight into a typed block, and that is all it is for.

The other direction, a typed block going back to the heap, is the one the cast was really for. There I gave up on conversion altogether.

Templating the heap functions

Instead of turning a block into memory, the heap learnt to take any block whose items are the right size. The heap is already templated on _itemBytes_, so the functions that take a block back are templated on the item type as well:

template <typename _item_t_>
void deallocate(_block_s<_item_t_, _itemBytes_>& oldBlock);

template <typename _item_t_>
void shrink(_block_s<_item_t_, _itemBytes_>& block);

template <typename _item_t_>
void clear(_block_s<_item_t_, _itemBytes_>& block) const;

template <typename _itemTo_t_, typename _itemFrom_t_>
void copy(_block_s<_itemTo_t_, _itemBytes_>& to, const _block_s<_itemFrom_t_, _itemBytes_>& from) const;

Now utf8heap_.deallocate(work) gets the caller’s actual utf8block_s by reference, through its own type. It clears the pointer, size and capacity in place. No second type anywhere, so nothing to alias.

The safety I wanted from the size check originally is still there, just in a better place. _itemBytes_ is fixed by the heap, so a utf32block_s simply does not match a 1 byte heap. And the block checks itself:

static_assert(sizeof(_item_t_) == _itemBytes_, "Block item size does not match the type.");

The two copy() types are separate on purpose, so a u8block_s can copy into a utf8block_s without anyone casting. Same item size, so memcpy is fine.

I had resisted templating these because it felt like cheating, and because of code size. In practice each one is a few lines, inlined, and there are only a handful of item types per heap. The difference is that GCC and Clang are now allowed to compile it. The #error came out a few days later, when text_c.h stopped inheriting from the block.

Then spans

Continuing the design process made the read-only source look iffy. copy() takes a const _block_s&, so copying from a borrowed string meant building a block around a const pointer with its const cast away. A _span_s is the honest type for that: a const pointer, a size and a zero capacity. So copy() got an overload that reads from one:

template <typename _itemTo_t_, typename _itemFrom_t_>
void copy(_block_s<_itemTo_t_, _itemBytes_>& to, const _span_s<_itemFrom_t_>& from) const
{
    static_assert(sizeof(_itemFrom_t_) == _itemBytes_, "Span item size does not match the heap.");
    _assert(from.size <= to.capacity);
    memcpy(to.pointer, from.pointer, (size_t)from.size * _itemBytes);
    to.size = from.size;
}

Same rule as the blocks: a new overload for a new type, not a cast to an old one.

Where that leaves it

The rule I came out with is simple. Moving data between two types is a copy into a new object, and a function that has to write into the caller’s object is templated on its type. No reference casts between structs, however identical they look.

The commented-out operators are still in block_s.h, with a note saying why, so I do not put them back in a weak moment.

That rule is also exactly where text and subtext ran into trouble. A text_c and a subtext_c are the same 16 bytes too, and I wanted one function to take either. I tried most of these ideas again before I accepted the same answer: copy, or template. That is the next post, Text, Subtext and the Sixteen Byte Problem.

Text, Subtext and the Sixteen Byte Problem

The desire

This is the follow on from my post on conversions when inherited and a constructor. The desire has not changed. I want a small text class that can be moved around by value, and I want one set of functions to work on it whether the text is owned or borrowed.

In my library there are now two types. A text_c owns its bytes on the UTF-8 heap. A subtext_c is a slice of a text, or a borrowed u8"" literal, and owns nothing. Both are a pointer, a size and a capacity, 16 bytes, and both are always NFC.

class text_c final
{
public:
    const char8_t* const    pointer;
    const u32               size;
    const u32               capacity;
    ...
};

class subtext_c
{
public:
    const char8_t* const    pointer;
    const u32               size;
    const u32               capacity{0};
    ...
};

The same bytes, the same layout. So surely writeUpper() or find() can take either one without me writing everything twice? That is the question this post follows, each attempt, with the issue that blocked each one. This was done with Claude (Opus 5.5 Medium) pointing out the pros and cons of each option.

Moving the data with casts

The first instinct was the C one. The bytes are the same, so just look at them as the other type.

My earlier post goes through some of this, using a union of span and block types. This used non standard extensions: nameless struct/union. The next was a conversion operator that returns a reference. No copy at all, just the address of the object seen as the other type. This leads to memory aliasing and while MSVC does not actually alias other compilers did.

Go for simple

I wrote down some options:

  1. Template all the functions. Cute, but increases compile time and code size.
  2. Use a struct conversion operator. Not allowed by the standard; you can only do struct pointers.
  3. A union, an anonymous struct and a conversion operator. Works, but not legal C++17, and it ties the heap block to the same layout.
  4. Give the heap block, text_c and subtext_c the same three members. A slice carries a capacity of zero, as it owns nothing.
  5. Move the data around between text and subtext. This seems very wasteful.

So I chose 4, as it is the simplest code and explored the impacts of that., Each class declares its own read functions, and they all call the same internal helpers on (utf8span_s)*this. A slice is 16 bytes, not 12, and I can live with that.

Blocked by: nothing, but it left a gap. The functions that take a second text still only took const text_c&: the find() needle, writeUpper(), writeLower(), writeTitle(), writeFold() and equalsIgnoringCase(). And text_c == subtext_c did not compile.

Make text to subtext implicit

There is already an explicit operator subtext_c() on text_c. Drop the explicit, make every read-only parameter a subtext_c, and a text just converts.

Blocked by: a pile of small surprises that Claude helped point out:

  • A slice taken from a temporary text dangles the moment the statement ends.
  • A literal passed for a text parameter used to be imported and normalised. It is now a subtext it is borrowed, so a non-NFC literal becomes a fault instead.
  • Hidden friend == lookup and overloads that take both types start to clash or go ambiguous.
  • You can no longer see at the call whether something owns its bytes.

Pass the subtext by value

My thinking was that copying a pointer and a size is free. Claude first said coping the subtext was the same cost as a reference. I did not buy it, and I was right.

On Windows x64 a struct is only passed in a register when it is 1, 2, 4 or 8 bytes. A 16 byte subtext_c is copied by the caller into a temporary on its stack, and the address of that copy is passed. A const text_c& passes the address of the text itself.

CallerCallee
const text_c&address in a registerreads [rcx] and [rcx+8]
subtext_c by valuethree stores to a stack copy, then its addressreads [rcx] and [rcx+8]

Blocked by: it is strictly more work than the reference. Small, but it is in the wrong direction. Taking a const subtext_c& does not help either, as a text still has to be turned into a temporary subtext for the reference to point at.

Hand back the text as a subtext reference

The layouts match, so the conversion could return the text itself:

operator const subtext_c&() const { return *(const subtext_c*)this; }

Blocked by: this is the reference cast from before. Strict aliasing again. I am not going full circle.

Two versions of each function and a core

One overload takes const text_c&, one takes subtext_c, and both call a core in core.cpp that takes the pointer and size as two plain parameters, so they go in registers.

Blocked by: literals. Both text_c and subtext_c can be built from a const char8_t* without asking, so writeUpper(u8"abc") matches both overloads and is ambiguous. Two argument functions like equalsIgnoringCase() also explode into four, and more with literals.

An internal view struct

Claude also suggested a private _textView_s of just pointer and size, with implicit conversions from a text and a subtext. One function per name, and the implicit conversion is hidden inside a type nobody stores.

Blocked by: it is still 16 bytes, so it still goes through a stack copy unless inlined. And a literal can no longer be imported: literal to text_c to view is two user defined conversions, which C++ does not allow, and a view cannot own an imported text anyway. It is also adding too much complexity. I did not like this one at all.

The scorecard

AttemptBlocked by
Union with an anonymous structnot legal C++; undefined behaviour
Conversion operator returning a referencestrict aliasing on other compilers
Same three-member layoutworked; left text-only parameters
Implicit text to subtextdangling slices, literal faults, ambiguity
Subtext by valuestack copy on Windows x64
Aliasing reference againstrict aliasing, again
Overloads and a coreliteral ambiguity, overload explosion
Internal view structstill 16 bytes; cannot import a literal

Back to templates

Option 1, the one I called cute, turned out to be the answer. Not templating the work, just a thin inline wrapper over one core. The wrapper is inlined at the call, so it costs nothing, and it reads the pointer and size straight out of whatever it was given.

What counts as a text argument

The trick that makes it safe is a forwarding reference. With T&&, const survives deduction, so after removeReference_ a u8"abc" is a const char8_t[4] and a buffer you filled is a char8_t[256]. Only the first is a literal.

template <typename type_t> inline constexpr b8 _isLiteralType = false;
template <u32 countBytes> inline constexpr b8 _isLiteralType<const char8_t[countBytes]> = true;
template <typename type_t> inline constexpr b8 _isTextType = false;
template <> inline constexpr b8 _isTextType<text_c> = true;
template <> inline constexpr b8 _isTextType<const text_c> = true;
template <> inline constexpr b8 _isTextType<subtext_c> = true;
template <> inline constexpr b8 _isTextType<const subtext_c> = true;
template <typename argument_t> inline constexpr b8 _isLiteral = _isLiteralType<typename removeReference_<argument_t>::type>;
template <typename argument_t> inline constexpr b8 _isTextArgument = (_isLiteral<argument_t> || _isTextType<typename removeReference_<argument_t>::type>);

Literals are borrowed, sized when it compiles

The subtext_c pointer constructor is gone. In its place is one that only takes a literal, and knows its size from the array, so there is no lstrlenA().

template <u32 countBytes> subtext_c(const char8_t (&literal)[countBytes]) : subtext_c{_fromLiteral(literal, countBytes - 1)} {}
template <u32 countBytes> subtext_c(char8_t (&buffer)[countBytes]) = delete;

If you made the string yourself, you import it with text_c{buffer}. A borrow can not normalise, as it can not own anything, so runtime data has to come in through the import.

One wrapper per function

template <utf8case_e mapping, typename source_t>
inline text_c _writeCaseOf(source_t&& source)
{
    if constexpr (_isLiteral<source_t>)
    {
        subtext_c literal{source};
        return text_c::_writeCase(literal.pointer, literal.size, mapping);
    }
    else
    {
        return text_c::_writeCase(source.pointer, source.size, mapping);
    }
}

template <typename source_t> requires _isTextArgument<source_t>
inline text_c writeUpper(source_t&& source) { return _writeCaseOf<utf8case_e::upper>(source); }

The core function are more explicit and take the pointer and size as two parameters. Not a span, as a span is 16 bytes and would bring the stack copy straight back. I had forgotten that was the reason, which is partly why this post exists.

equalsIgnoringCase() did not even need a core. writeFold() already takes any of the three:

template <typename a_t, typename b_t> requires (_isTextArgument<a_t> && _isTextArgument<b_t>)
inline b8 equalsIgnoringCase(a_t&& a, b_t&& b) { return (writeFold(a) == writeFold(b)); }

== written out by hand

A template == would join in on far more comparisons than I want, so it is the one exception. C++20 tries b == a as well as a == b, so five overloads cover every pair: text and text, subtext and subtext, text and subtext, text and literal, subtext and literal. The literal ones are not optional. Without them text == u8"x" is ambiguous between importing and borrowing.

What the compiler now refuses

CallError
find() or writeUpper() with a buffer or a pointerno matching overload
subtext_c{buffer}deleted constructor
slice == buffer, either way rounddeleted constructor
slice == pointerno operator found
text == bufferambiguous

What MSVC actually made

I asked Claude to compare the asm, which I would normally do, but I take ages to do so.

The old writeUpper(const text_c&) was two calls deep:

; master: probeUpperText
call    ?writeUpper@@YA?AVtext_c@@AEBV1@@Z    ; writeUpper, which then calls _writeCase

The template is inlined, so a text goes straight to the internal function with two loads and no stores. A subtext is exactly the same code:

; prototype: probeUpperText and probeUpperSubtext
mov     r8d, DWORD PTR [rdx+8]                 ; size
xor     r9d, r9d                               ; utf8case_e::upper
mov     rdx, QWORD PTR [rdx]                   ; pointer
mov     rbx, rcx
call    ?_writeCase@text_c@@SA?AV1@PEB_QIW4utf8case_e@@@Z
CallBeforeAfter
writeUpper(text)call writeUpper, which calls _writeCasetwo loads, one call to _writeCase
writeUpper(subtext)did not compilesame as a text
writeUpper(u8"abc")construct a text_c (allocate, import, NFC), call, destroy_fromLiteral with size 3 as a constant, then _writeCase
find(text)6 stores for two span copies, call _findfour loads to registers, 2 stores, call _find
find(subtext)did not compilesame as a text
find(u8"abc")construct, 6 stores, call, destroy_fromLiteral, 2 stores, call

The surprise was find(). _find is marked inline but MSVC chose not to inline it, so on the first build both its span arguments still went through stack copies. That is the 16 byte problem again, one level down, and it was there before I started. So _find now takes pointers and sizes too. Only four arguments fit in registers on Windows x64, so the needle size and the start letter still go on the stack, but that is 2 stores instead of 6. The body of _find came out 11 instructions shorter as well.

The literal check also still runs every call. ASCII is a quick scan, but a non-ASCII literal allocates scratch space to prove it is NFC.

Where that leaves it

It is not perfect, but it is a good solution and any remaining downsides I think I can accept. There is no aliasing, no implicit conversion in the public interface, one function per name, and literals are cheaper than they were. The call to an internal function is now a couple of register loads.

What is left in the downsides list:

  • text == pointer still compiles, as the text_c(const char8_t*) import is still implicit. Making it explicit would also break text + u8"...". (This was fixed in later iterations. Using explicit means text_c example = u8"not OK" will fail, but that is better than allowing the silent import for f(u8"copy this now")).
  • operator+ still takes two texts. (This was fixed in later iterations).
  • _find still builds a 16 byte span for every call to utf8letterBytes_() inside its loop. Same problem, one more level down, in utf8.h.
  • A non-ASCII literal is checked for NFC on every call.

The lesson for me is the same one as the last post. C++ will not let you say “these bytes are also that type”, however true it is. So you either copy, or you template. Copying 16 bytes turned out to be a real cost on Windows x64, so templates it is, kept as thin as I can make them.

Multi Dimensional Dynamically Allocated Arrays

There is a nice trick to get a dynamically backed contiguous amount of memory to index like a static array. This is easy to find on the internet, however even thought the C++ standard talks about how to do a 3D version I could not find any example code.

From the C++ standard page (https://en.cppreference.com/w/cpp/language/operators)

“operator[] can only take one subscript. In order to provide multidimensional array access semantics, e.g. to implement a 3D array access a[i][j][k] = x;, operator[] has to return a reference to a 2D plane, which has to have its own operator[] which returns a reference to a 1D row, which has to have operator[] which returns a reference to the element. To avoid this complexity, some libraries opt for overloading operator() instead, so that 3D access expressions have the Fortran-like syntax a(i, j, k) = x;”

To be clear, this is what we are trying to obtain:

    array a;
    a.setDimensions(3, 4, 5);
    a[2][0][4] = 42;
    // ... vs ...
    a(2, 0, 4) = 42;

So here it is how the C syntax style can be done for 3D.

First the version that explains in detail what is going on. Here you can see the plane object talked about in the standard. This does not return a reference as the size is small enough.

struct array
{
	struct row
	{
		row(array* array, int i, int j) 
			: _array{array}, _i{i}, _j{j} {};
		inline int &operator[](int k) 
		{ 
			assert(k < _array->_kSize);
			int offset = 
				(_i * _array->_kSize * _array->_jSize) +
				(_j * _array->_kSize) +
				(k);
			return *(_array->_data + offset); 
		}
		int		_i;
		int		_j;
		array*	_array;
	};
	struct plane
	{
		plane(array* array, int i) 
			: _array{array}, _i{i} {};
		inline row operator[](int j) 
		{ 
			assert(j < _array->_jSize); 
			row newRow(_array , _i, j); 
			return newRow;
		}
		int	_i;
		array*	_array;
	};
	inline plane operator[](int i)
	{
		assert(i < this->_iSize);
		plane newPlane(this, i); 
		return newPlane;
	}
	void setDimensions(int x, int y, int z)
	{
		_iSize = x;
		_jSize = y;
		_kSize = z;
1	}
	int		_data[24];
	int		_iSize{1};
	int		_jSize{1};
	int		_kSize{1};
};

This is a faster version that uses the typical approach where the returned value is a pointer to the objects so that the last [ ] addressing is done ‘for free’.

struct arrayFast
{
	struct plane
	{
		plane(int* data, int kSize) 
			: _data{data}, _kSize{kSize} {};
		inline int* operator[](int j)
		{
			return _data + j * _kSize;
		}
		int*	_data;
		int	_kSize;
	};
	inline plane operator[](int i)
	{
		int offset = i * _jSize * _kSize;
		plane newPlane(_data + offset, _kSize);
		return newPlane;
	}
	void setDimensions(int x, int y, int z)
	{
		_iSize = x;
		_jSize = y;
		_kSize = z;
	}
	int		_data[24];
	int		_kSize{1};
	int		_jSize{1};
	int		_iSize{1};
};

Finally the recommended solution of using operator() instead.

struct arrayFaster
{
	inline int& operator()(int i, int j, int k)
	{
		int offset = (i * _kSize * _jSize) + (j * _kSize) + (k);
		return *(_data + offset);
	}
	void setDimensions(int x, int y, int z)
	{
		_kSize = z;
		_jSize = y;
		_iSize = x;
	}
	int		_data[24];
	int		_iSize{1};
	int		_jSize{1};
	int		_kSize{1};
};

This last one looks much easier to understand and you would expect it to be the fastest solution as well. The code is direct and the maths in one place.

I did a quick check on Godbolt (https://godbolt.org/z/EP36qooaE) and it looks like the 2nd and last options are very similar in instructions. I did not speed test these, but it is interesting enough that for now I will be using the traditional C syntax solution.

Using assert calls to check coverage

I heard a great development process idea from Tom Forsyth where he explained that he litters his code with assert(true) to make sure that he hits all the key code paths during early testing.

This got me thinking about asserts and code coverage.

Code coverage is an evil that kills the sole of testers. It is treated as an absolute and they are forced to try to hit 100%. A much more efficient approach is to have humans spend real thinking power about where and what needs to have code coverage checked. Sure this is not black box testing, but a clever mix of black box, API, edge case, and coverage testing is a much better approach.

Continue reading “Using assert calls to check coverage”

Compile time functions

While C++ lags behind new languages for support at compile time, there is some ability to get some of the advantages.

A simple case is to create a allow a runtime function that is simple enough to be run at compile time. This has the nice feature that if the function is not able to be used at compile time it will still work at runtime. Thus this can be done for many functions:

static constexpr u32 function(const char* const int)

However this flexibility also has the problem that you cant be sure it will be run at run time.

Continue reading “Compile time functions”

Never Nester, Goto, RAII, and Defer

Coding when you are using an OS or library that requires a series of calls that build on each other to get the final result requires a set of checks to make sure each stage is successful. If some of the stages also require it partner call to free/close/destroy the items, then the programming logic requires you to be careful in how you tear down the items if there is an error in the middle of the process.

This can be coded several ways, and this post discusses a series of approaches in C++. The code selected for the example is performing Windows OS calls. These are done at the lowest API level without any other library or framework. The code has three places where tear down is required. The flow is complex enough to stop a simple reorganisation of the code to perform the same task. There is no addition error management present so make it easier to read. It is a fully working program and it gets a list of USB devices that the program is allowed access to.

Continue reading “Never Nester, Goto, RAII, and Defer”

Reasons for Using C++ 17

Using the logic that we should use tools that are as simple as possible and only adopt complexity where it is absolutely necessary, it feels like I could use a very early version of C++. What follows are the reasons why I have increased that to C++17. The idea will be to only use these feature and no more. Any new use will be add to this page.

#if !((defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) || __cplusplus >= 201703L)
    #error Requires C++17 or higher
#endif
Continue reading “Reasons for Using C++ 17”

Screen Find – Seeing the Wood in the Trees

I hope you have a nice big screen – or several of them. Nice huh?

Well all this real estate comes with a price. Some times the screens can hold so much information it is hard to find what you are looking for. Would it not be nice to be able to search for the word you are looking for in a huge list of dense text?

Sure almost every app has search, but can you search the dialogue boxes, the drop down menus, the image of a poster?

So why not have a system wide special key combo that triggered a find dialogue box. Type in ‘comment’ and the screens are captured, OCR’ed and then if the text is found an overlay highlight shows you where the word is.

Testing

C++ Coding Standards

The following is not intended to generate a holy war, but merely a place for me to remind myself of the decisions I took along the way to get to a consistent style for any new code that I write.

#pragma warning( push, 3 )
#include <stdio.h>
#pragma warning( pop )

// /Wall Used. Compiler warnings turned off.
#pragma warning (disable :  5045) // Spectre code insertion warning.

class entity
{
    static constexpr size_t     _maxEntities = 1000;
    entity(char* name)
    {
        _name = name;
    };
    void set()
    char    _name[8];
};

inline float _floor(const float& a)
{
    if (a == 0.0f) return 0.0f;
    return floorf(a);
}
Continue reading “C++ Coding Standards”

C Strings in C++ and Deep Copies

I was told on Stack Overflow:

“A pointer is just a pointer.”

And that is true of course. To go beyond that we have structures and classes which have explicit construction and operators. However, C++ has inherited C strings which are just a pointer to the first char, but they also have a long history where the construction and operations are well known. So, when a C String is given to a map or vector, it may be thought that it could be doing a deep copy. After all, it is known (by implicit rules) how this could be completed. The size is defined. It is just not stored. Also, the storage is clear – just not managed.

Continue reading “C Strings in C++ and Deep Copies”