perjantai 25. marraskuuta 2016

C: sizeof(pointer) is ...?


Pointers in C are one of the biggest traps for programmers, novices and experienced alike. If you are working with modern PCs they are fairly simple to use - a linear 32- or 64-bit address, handily the same size of native word size of architecture so you "can" cast it to integer and do other things with it (but you really shouldn't, unless you really, really know what you are doing).

Too often people seem to make assumption that sizeof(int*) == sizeof (int). For many architectures this is so - but not for all, and not even for all x86-based architectures. Because of this you really shouldn't do that casting thing I mentioned above (for example, for backwards compatibility 64-bit linux [amd64 ABI] has 32-bit int.)

And when you get to older architectures it gets even weirder.

Let's take original 16-bit x86 for example. It was, of course, 16-bit architecture, but with 1MB of memory total. Memory was addressed with segment:offset system; both being 16-bit values, the pointer total was 32 bits (I won't go into the actual addressing details here as it's irrelevant for the discussion.)
But wait, there's more! This was the "far" pointer, that could point to any address of the system (no memory protection back then.)  Then there was also "near" pointer that could only access memory within a segment with only the 16-bit offset value. And then there was apparently "huge" pointer which I've never encountered myself.

So here even sizeof(int*) isn't necessarily sizeof(int*), if those happen to be "near" and "far" respectively. Confusing already?

How about embedded world then?

I used to work with C51-based devices. This is old architecture, introduced back in early 80s, but is still going strong as modern variants. Different variants are easily and cheaply available from several manufacturers, and many (old-school) programmers are familiar with it, so they are very popular in embedded world.
These devices are 8-bit MCUs (int typically is 16-bit) and have four different memory access modes (and three completely separate memories);
  • DATA, directly accessible RAM, 128 bytes.  
  • IDATA, indirectly accessible RAM, 256 bytes total. This overlaps DATA for first 128 bytes.
  • XDATA, external RAM, up to 64kbytes. These days this is not really external but built in the chip itself. Being external there is no overlap with DATA or IDATA.
  • CODE, being code memory, I've used devices with up to 8kbytes of flash built in.
So here pointer must identify which memory is accessed *and* up to 16-bit address within it. So commonly pointer is three bytes; one byte to specify which memory is accessed, and two to specify offset (and yes, other compilers may have used other methods, this is one I am familiar with.)

Separate memory spaces for code and data is known as Harvard architecture, and it is common even these days; many Atmel chips use it - and my extension, many Arduino devices. I am not familar with those (aside the code/data separation) and how C compilers there handle pointers, but I wouldn't be surprised if similar methods as with C51 would apply.

C is full of traps, especially when you start mixing several architectures in same project, and it's kinda okay to play fast and loose iff you know damn well what you are doing. If you don't (even I don't, not always), one should play safe. Don't assume you know sizes, and don't do any wild castings. It'll come back to bite you.

I should know, I've been there, many, many times over the years.






maanantai 14. marraskuuta 2016

Voltage detector - with a leak within


Some time ago I bought a cheap contactless voltage tester. This is a device that indicates presence of high voltages (AC mains mainly) near the probe with a sound and is powered with two AAA batteries.

I do not need it often, but like so many devices, when I do need it it is really useful for diagnosing power faults (for example: this device seems to be receiving mains voltage so fault very likely is inside it.)  And just as a reminder: even if these don't indicate voltage presence, you should always treat mains wiring as it were powered!

Now, this specific device unfortunately does not have any kind of power switch for detection (it has a flashlight at the other end which does have button, yes, but that does not turn the voltage detection off). And since it takes only one time forgetting to remote the batteries, the end result is inevitable:


No, despite colors this device has nothing to do with Fluke.

Surprisingly, it still worked when in this condition, both detection and LED flashlight. The physical damage to the battery from trying to remove it forcefully - other battery is still deep inside and I don't even bother trying to get it out. I only noticed the situation since there was this white crap around the battery cap.

As my personal tool-buying rules say, I've bought cheap, found it useful, so now it is time to get a better one. And this time it absolutely must have a physical power switch.




torstai 10. marraskuuta 2016

Intermission: Culture and politics


Now that a bit of time has passed, I'll open up my previous short post a bit. Just politics here so if you don't want to read about that, move on.


Back in 80s, when I was growing up, USA seemed like a heaven based on everything I heard from there. Cheap soda, cheaper gas, advanced technology, rich people, good food, space exploration, world leader, etc etc.

As time as passed the reality has crept in (and maybe I've grown older and maybe a bit more world-wiser too), piece by piece, and the more I hear about conditions in the USA the less I nowadays want to live there. Two big things being political system and medical care. Both of which are so important for people and so horribly broken and there is (at the moment) no hope of either being ever fixed.

These days I've given up of even visiting the country as a tourist since border control seems to have taken openly hostile attitude towards anyone (their own citizen included) entering the country. As long as the situation remains like this I take my tourism money elsewhere. Like Colombia (not very likely, but they certainly are at the moment way above USA on my mental list of countries I'd like to visit some time.)

But back to today's main topic. Let's get real now - when electing the president, if your only real choices are "completely horrible person A" and "totally horrible person B", something is seriously fucked up somewhere. And this election was exactly like that. But hey, "Don't blame me, I voted for Kodos." Right?

I do hope that Trump will turn out to be something else than what he has presented himself to be during the campaign. But I won't be holding my breath in the meantime.