Dance With Grenades

Dance With Grenades | Blog

A Couple of Things I Just Learned About C# - Part 1

or, What the Hell, Microsoft?!

(part 1)

1. You can't use structs to build linked lists.

Seriously, try it. It won't work. Why? Because you can't null the "nextNode" pointer by default, thus creating a circular reference, which would cause .NET to explode. It'd work just fine in unmanaged C, but the CLR apparently can't handle that shit.

2. There is no built-in function to flush stdin.

Seriously, this one is just so basic I can't really fathom why it wasn't included in the .NET runtime. I mean, I can understand having to manually flush the buffer in C, because it's C. But C#? C'mon, guys. Anyway, here's a simple function to remedy the problem, but it's retarded that I even have to do this.

static void ClearInputBuffer() {
    // This is retarded. I can't believe I have to flush stdin manually.
    while( Console.In.Peek() != -1 ) {
        Console.In.Read();
    }
}

3. Where The Hell Are My Pointers?!

So, C# supports pointers... but only in "unsafe" contexts. You have to mark each method you want to use a pointer in as "unsafe," which causes it to not be under full control of the CLR. Okay, that's fine. But, oh, now you have to tell the compiler that it's allowed to compile unsafe code with the /unsafe flag! (also selectable under project build options) What?! Sure, it works. But it's goddamn annoying. Another potential gotcha here is that you have to use a "fixed" block of code on many types of objects to prevent the garbage collector from moving those objects, thus invalidating your pointers. The two real gotchas here are:

  1. fixed has somewhat wonky syntax. You have to declare your pointer variable into a fixed block for this to actually work:
  2. int q = 10;
    fixed( int *x = &q ) {
      // do stuff with your pointer here
    }
    
  3. Some stuff is inherently fixed. Structs, for example. This will generate an error:
  4. struct demo {
        public int number;
    }
    
    demo x;
    
    fixed(demo *y = &x) {
        // Error	1	You cannot use the fixed statement to take the address of an already fixed expression
    
    }
    

So, it seems that while you can use pointers in a limited fashion with C#, it's probably to be avoided. Which is kind of a shame, but not really a deal breaker.

KingRadical | 09 Jun 2010 | Tags | 13 Comments

3 Bash Shortcuts To Make Life a Little Easier on Linux

I'm trying to keep up with my goal of "at least one blog post per week." I've decided, therefore, to give you all some fancy-pants Bash shortcuts for Linux to make your commandline lives a little easier.

1. GNU Find's Xargs Eliminator

Xargs is quite cool. For those who don't know, it'll take in a list of arguments (probably piped in from find, typically), and if it's quite a long list, it'll split up the list of arguments into appropriately smaller chunks so your next command can handle it. Typically, you might do something like this (for example):

find /srv/music -iname "*.mp3" -print0 | xargs -0 mp3gain -r -k

Now, you might immediately think, "Oh, I can get around that like so:"

find /srv/music -iname "*.mp3" -exec mp3gain -r -k {} \;

which works, except that you're calling mp3gain on every single file, which in some cases can add extra time if you have lots of arguments. So, instead, we'll use find's xargs eliminator (+ instead of \):

find /srv/music -iname "*.mp3" -exec mp3gain -r -k {} +;

2. Shorten Your Compound Pipes With Better Use of Awk

In many cases, you can eliminate one or more greps in your compound pipe commands which are followed by awks anyway. For instance. Let's say you're trying to kill a bunch of processes with similar names, that won't just die, because they keep respawning each other. Now, you could just manually "kill -9 list of pids" but sometimes that can be tedious at best. Besides, it's lame. Now, you might first think, "Oh, I can do it like this:"

ps aux | grep pname | awk '{print "kill -9 " $2}' | sh -v

or, a little more efficiently:

ps aux | grep pname | awk '{print $2}' | xargs kill -9

which would work, but that's a lot of pipes. Here are a couple of shorter ways to do it:

ps aux | awk '/pname/ {print "kill -9" $2}' | sh -v
ps aux | awk '/pname/ {print $2}' | xargs kill -9

Another bit of magic, for Debian/Ubuntu-based distros, can purge all removed packages that have left files behind:

dpkg -l | awk '/^rc/ { print $2 }' | xargs sudo dpkg --purge

3. Sed > Grep

Grep can a very useful tool. In some cases, though, sed can do it better. Consider:

grep -v "^#" /usr/lib/postfix/main.cf | grep -v "^$" > /usr/lib/postfix/main.cf.new

Now, without pipes, in sed:

sed -i -e '/^#/d' -e '/^$/d' /usr/lib/postfix/main.cf

The -i flag specifies to edit the file "in-place." You can specify multiple expressions with multiple -e flags, in this case, consolidating the two grep calls and the two pipes into a single command. It's faster, too:

mgreene@BBRodriguez:~$ time grep -v "^#" main.cf | grep -v "^$" > main.cf.new

real	0m0.038s
user	0m0.000s
sys	0m0.000s
mgreene@BBRodriguez:~$ time sed -i -e '/^#/d' -e '/^$/d' main.cf

real	0m0.021s
user	0m0.000s
sys	0m0.020s

KingRadical | 25 May 2010 | Tags | 49 Comments

OpenTK

I am beginning development of a top secret game project. I will be learning OpenTK, and using a combination of C# (which I'll be learning during this project), and Managed C++. It will use OpenGL and OpenAL for graphics and sound, respectively.

The game will be available on Windows and MacOS (and possibly Linux if I can figure a good way of distributing it) for a nominal fee. Game licenses will be crossplatform as well, so "buy once, play anywhere!"

As development continues, I'm sure I will be posting interesting code snippets. For now, I leave you with this test of my CodeHighlighter v0.1 plugin for Jaws/DWG, which uses Alex Gorbatchev's SyntaxHighlighter v2.1.364:

using ( Game game = new Game() ) {
    game.Run(60.0);
}

KingRadical | 22 May 2010 | Tags | 17 Comments
Page 3 of 3
Showing 11 - 13 of 13 Posts
Next >