Archive Page 2

C types 101

08Jun08
I was cleaning up the code of an application that I’m packaging for Fedora, and was Googling for information on size_t (in the code, a size_t variable was being printed as a normal integer (%d), which triggered a compiler warning, and I forgot what the relevant option is. Ended up finding it in printf’s manpage) when I discovered this rather well-written gem.
clipped from www.embedded.com

Using size_t appropriately can improve the portability, efficiency, or readability of your code. Maybe even all three.



Numerous functions in the Standard C library accept arguments or return values that represent object sizes in bytes. For example, the lone argument in malloc(n) specifies the size of the object to be allocated, and the last argument in memcpy(s1, s2, n) specifies the size of the object to be copied. The return value of strlen(s) yields the length of (the number of characters in) null-terminated character array s excluding the null character, which isn’t exactly the size of s, but it’s in the ballpark.



You might reasonably expect these parameters and return types that represent sizes to be declared with type int (possibly long and/or unsigned), but they aren’t. Rather, the C standard declares them as type size_t. According to the standard, the declaration for malloc should appear in <stdlib.h> as something equivalent to:



void *malloc(size_t n);
  blog it

Unlike the commercial OSes (and commercially-supported Linux distributions), community Linux distributions tend to have fast-paced release cycles. Notably, Fedora and Ubuntu releases every 6 months.

Every OS upgrade entails several decision: do you do a clean install, or upgrade your current installation? Do you start with a clean home directory, or re-use your previous one? Any combination works fine, though my personal preference is to do a clean install and use a clean home directory, having archived the older directory. I think of it as house-cleaning — and it’s nice to experience the desktop as it ships out of the box, before customization (naturally, I then restore my music database, my address books, browser and e-mail client profiles, etc. This is not Memento!)

So, now that Fedora 9 has been released, what needs to be added to / changed from the base setup? As it turns out, not that many:

Compositing
Some people swear by Compiz; I personally find Metacity much more usable (Compiz does not support cycling through all windows of a given application — Ctrl+F6 in Metacity; Cmd+~ in OS X). Metacity now has a compositing manager that’s turned off by default; turning it on involves either using gconftool-2 (only for advanced users) or gconf-editor, and setting the /apps/metacity/general/compositing_manager key to true.

The support in the stable version is a bit flaky still; the metacity package in Rawhide is much better behaved and appears quite stable. Upgrade by issuing yum –enablerepo=rawhide update metacity. As of the moment it does not pull in any other Rawhide package so you can rest easy.

Try pressing the volume up/down/mute keys on your keyboard (if you don’t have a multimedia keyboard, change the bindings in System->Preferences->Personal->Keyboard Shortcuts) and be amazed at the translucency coolness (no, this is not bling). The brightness pop-up windows have not been changed yet, alas.

Firefox
Ever cursed Firefox’s font rendering in silence? Type about:config in the address bar, and add the following boolean keys:

font.FreeType2.autohinted = true
font.FreeType2.enable = true

Keyboard
For the English-speakers among us specifically, and those who use the US keyboard layout in general (it’s the standard layout in Indonesia, for instance), the occasional times when one has to type an accented character is rather annoying.

There are various work-arounds — launch the character map (under Accessories), add the Character Palette applet to the panel (so that it consumes RAM even when you don’t use it!)…*or* you can just fix your keyboard layout. The die-hard command-line junkie would be able to tell you what option to pass to setxkbmap to achieve this. The rest of us can just use System->Preferences->Hardware->Keyboard. In the “Layouts” tab, select “Layout Options”. The option you want is “Compose key position”; I use Right Alt, but Caps Lock haters will rejoice to know that, yes, you can use that dreaded key as your compose key as well. To type an accented character, now the only thing you need to do is hit the Compose+accent followed by the letter you want to accent (using shift as necessary, e.g. for ^).

While you’re here, you might want to change the Alt/Win key behavior, and map either Meta, Super or Hyper to one of your Win-keys. The GNOME default is inexplicably for the Win-key to be a normal key and not a modifier (so it cannot be combined with other keys).

Coming up: Applications
Et voilà! You should have a nice-looking, and more importantly, functional desktop right now. In the next instalment, I’ll comment on the applications I use. Until then, à bientôt!


Some men see things as they are and say, “Why?” I dream of things that never were and say, “Why not?”
George Bernard Shaw

It is easy to be apathetically indifferent. The world has its problems, sure, but hey, life is short and one should take care of oneself first, no? Yet the danger is that there are a *lot* of issues on which the silent majority is not sufficiently aware to care one way or another, and a small but outspoken minority gets to drive their agenda, because the other people who know the first group is wrong find it too uncouth to engage them in debate. To spread the word to the masses. To do something.

People have various reasons for being apathetic, and most of them certainly make sense. At the time, anyway. Life is short. Or, I know this guy is deluded on some issues, but how much harm can he/she do anyway? Or, people will come to their senses (how, if the only passionate people are on two diametric opposites, the mainstream media too busy triangulating and the moderates too indifferent to make their voice heard?). Or they believe in the “great leader” myth: mere mortals can’t lead movements for change, let’s wait for an inspired leader to set things right.

The latter is a particularly pernicious argument. Just because what you do does not have a great impact, does it mean you should not do it? We too often underestimate our potential impact. Whether negative (how much can a single SUV affect the environment anyway? Or taking those cheap flights in Europe. Or watching movies that glorify senseless violence). You’re just an anonymous consumer, right? Yet collectively our negative contributions add up to something significant.

It is the same with the positive things we can do. When you buy food from a local farm, who knows how many people are affected by your example? If stores and supermarkets notice that their customers are starting to buy up local produce, market economics dictate that they will allocate more shelf space to these produces. It’s a virtuous cycle.

Perhaps the feeling might be that meaningful change cannot be achieved within one’s lifetime. Yet is that reason for apathy? If the suffragettes became disillusioned early on, would women have the right to vote in the Western world today? If black Americans get discouraged by the post-Reconstruction setbacks, would the civil rights movement come into being?

Change does not crystallize magically once the right leader emerges. Were Stalin to be struck by lightning and decide to democratize overnight, would the system he had a hand in building let him do that? Even the most tyrannical dictator relies on a support structure, and thus is constrained in his/her (mostly his) actions. Education can work, no matter how plodding, slow and fraught with setbacks the process might seem at times.

Moral of the story? Put your efforts behind what you believe in. No matter how futile the cause might be, nothing feels more emasculating than not even talking about an issue you care about deeply, let alone doing something about it.

Personally, my #1 bugbear is electoral reform. A healthy discussion of issues is impossible within a two-party system: they are either too polarized for their supporters to talk to each other (and the center too indifferent), or in their attempt to appeal to centrists, too indistinct and amorphous. Paradoxically, they can even swing between the two at short notice. Politicians pander to the base during primaries and swing to the center for the actual election. In the end, nobody’s happy.

Or take feminism. Or anti-racism. Indifference abounds here — just because our society does not discriminate openly, does not mean that institutional discrimination does not still occur. Most women are still unfairly expected to do double duty between house and work. Yet how many of us question the way the workplace is structured, leaving most men (the traditional breadwinner) no choice but to work long hours? In the end it’s mostly the women who are forced to sacrifice career once they start families.

Affirmative action programs are flawed. Too targeted at specific minorities, and thus open to attacks on the grounds (ironically) of equality. Yet the problem is real, and even conservatives like George Will agrees. Fairness demands that we help those born with socio-economical disadvantage. That we try and make sure everyone gets to pursue the American Dream (or the equivalent), rather than using the Dream as a deus ex machina (oh look, here’s another rags-to-riches success story! (s)he can do it, so if you can’t, you must be lazy and it’s your fault).

Magic wands do not exist in the real world. Not everyone will be fêted as heroes, and no problem will magically go away. Yet is being an unsung hero so bad?


From LWN:

“The acquisition of Trolltech will help Nokia accelerate its cross-platform software strategy for mobile devices and desktop applications, and develop its Internet services business…”

This is an interesting development. Presumably, given Maemo’s an entrenched investment in the GTK+/GNOME toolchain, Nokia will find itself in the same boat as Novell, supporting two directly competing platforms.

Though considering they are also heavily invested in Symbian, hopefully they can manage this properly.. unlike some other tech companies (hello, Compaq!)


Task: List the programming languages you consider important or interesting. Describe each of them in one sentence.

C High-level assembler
Lua Table-based, functional and embeddable
Python Great RAD language crippled by dogma
Scala Java done (almost) right, with Erlang- and Haskell-inspired features

Posting this from the new N810 using its built-in keyboard. Typing those tags was painful, and as a result, more languages will be added to the table tomorrow — from the workstation.