Articles

Previous page  12 

News 12 years Ago
 
Published by packi on

Looking at at the following lines of C++ code:

#include <iostream>

using namespace std;

class A {
public:
  A() {};
  virtual ~A() {};
};

class B : public A {
};

class C : public A {
};

class F {
};

class D : public C,F {
};

For the C++ impaired, it defines a superclass A that has two descendants B and C. C itself has another descendant D. D has another superclass F.

If you’ve got an instance inst that’s a derivative of A you can use dynamic_cast<X*>(inst) to determine if inst is an instance of X.

I wrote a test function to determine if that’s really the case:

void test( A* _c ) {
  if( dynamic_cast<B*>(_c) ) {
    cout < "is B" < endl;
  }
  if( dynamic_cast<C*>(_c) ) {
    cout < "is C" < endl;
  }
  if( dynamic_cast<F*>(_c) ) {
    cout < "is F" < endl;
  }
}

int main() {
  cout < "creating B" < endl;
  test( new B() );
  cout < "creating C" < endl;
  test( new C() );
  cout < "creating D" < endl;
  test( new D() );
  return 0;
}

After compiling the file with g++ test.cpp and running the resulting executable I’ve got a somehow unexpected result:

patrick@pentagon ~/test $ ./a.out
creating B
is B
creating C
is C
creating D
is C
patrick@pentagon ~/test $

It is apparently a common mistake to read:

[…]
class D : public C,F {
[…]

as:

[…]
class D : public C,  public F {
[…]

when it really means:

[…]
class D : public C,  private F {
[…]

Defining the inheritance of F as public finally yielded the expected result:

patrick@pentagon ~/test $ ./a.out
creating B
is B
creating C
is C
creating D
is C
is F
patrick@pentagon ~/test $

You can download the full source for this test here.

 
Published by packi on

As a sysadmin I’ve come in touch with programming languages (counting scripting languages in) that you probably wouldn’t use for everydays work. The other day I had to deal with Python.

What’s the difference between

def filter_reviewers( usernames, recipients, 
    recipients_with_fullnames, users, paths, change ):
[…]
  for line in os.popen( p4 + ' describe -s ' + change ):
    # skip everything before Affected files …
    if ready == 0:
      if re.search( r'^Affected files \.\.\.', line ) != None:
        ready = 1
[…]

and

def filter_reviewers( usernames, recipients, 
    recipients_with_fullnames, users, paths, change ):
[…]
  for line in os.popen( p4 + ' describe -s ' + change ):
    # skip everything before Affected files …
    if ready == 0:
      if re.search( r'^Affected files \.\.\.', line ) != None:
	ready = 1
[…]

Apparently nothing. But a closer look reveals that the second sample has tabs in it. Since Python uses spaces to keep code-blocks together you shouldn’t use tabs (or rather your editor shouldn’t as in my example).

The code above is copied from my additions to p4review.py which should any time now be in the Perforce’s p4 opensource-repository.

 
Published by packi on

Just stumbled upon a FAQ that I consider a must read for any serious C++ programmer:

http://www.parashift.com/c++-faq-lite/

Given that I’ve been using C++ for several years, I’ve learned quite a bit reading that FAQ.

 
News 13 years Ago
 
Published by packi on

What’s the value of a “ten-pack” beer? The following lines:

...
while(1){
  switchByteLow = IN_PORTB(ButtonPort);
  switchByteLow = switchByteLow;
  curStat = (switchByteLow & Dreh_Mask) >> 4;
  if( curStat == lastStat ) {
    stableFor++; 
  } else {
    stableFor = 0;
  }

  if( stableFor == 50 ) {
    if( (lastStable == 15) && (curStat == 0 ) ) {
      numRevolutions++;	
    } else if( (lastStable == 0) && (curStat == 15 ) ) {
      numRevolutions--;
    }
    lastStable = curStat;
  }
				
  lastStat = curStat;

  OUT_PORTB(LedPortLow, ~lastStable);
  OUT_PORTB(LedPortHigh, ~numRevolutions );
}
...

The assignement was to read the value of a rotary switch and output the number of revolutions. That and to find out why the code you just wrote gives incorrect results.
Being a pain in the ass I’ve asked what could go wrong, partly knowing the answer. The teacher replied, ommiting the answer: “If somebody gets it right the first time he earns himself a beer crate”

Since rotary switches are prone to have bouncing outputs you better make sure to debounce them before accepting the values as stable.
Betting on somebody not knowing something is never good advice.

 
Published by packi on

Since I’m a part of a team consisting of some techies trying to compete with art-students on a short-film-marathon (title: budget 0, time 48h) I had to boot my Windows installation on my amd64 box. The cutting will take place in Adobe Premiere and Sony Vegas.

The Install of Adobe Premiere went smooth as silk, just double-clicking the installer (well almost, the installer tried to install an old version of DirectX), but the Sony Vegas installer was asking for the “.NET Framework v1.1”. I had to manually download the version 1 and after that the SP1.
Ever heard of bundling or an intelligent installer who gets the packages of the internet. Maybe I’m a little spoiled because every linux package-manager would have known the dependency on another package. As a positive point for Microsoft, installing the .NET Framework went smooth.

Now even the installer starts “Yay!”. But on the second or third page of the install wizard it asks for the DirectX 9.0c Runtime. So I went to windowsupdate.microsoft.com to get me my updates, thats an easy one (thats what I thought first, click here, click there, click accept, done).

I was greeted by a “We have to validate your Windows™ bla blubb”. Since I’ve legally purchased my WinXP Pro Edition I shouldn’t have to worry about this, should I (Even if I had a pirated version of Windows there are ways (no linke there) around it).
Yes, I know you shouldn’t spend too much money on Microsoft products, but as a software developer I fell an obligation to actually buy software to support my fellow software developers even if they have sold their souls to the Microsoft Goons.
Installing the “Genuine Advantage Validation Tool” was as easy as clicking “yes” three times but validating was everything but.

 Clicking (this is my first article describing a Windows installation, count the clicks in this text) “Validate me or send me to hell” button tried to go to some validation website. The website was supposed to load (this is all my imagination) the validation plugin but failed to do so. Instead I was seeing an empty page with a weird location string in it. I actually had to reload it only to to download yet another tool (see picture on the left). After the tool was sure my Windows copy was in fact legally purchased (whatever happened to “in dubio pro reo”) I had to choose “Windows Update” from the “Start Menu” (involving at least two clicks) to finally get my updates.

This is to all the “but window is sooo much better on the usability ladder and every user can use it” people. In your face!

Oh, breaking news: I’m on Update 9 of 15. This doesn’t mean a thing since DirectX 9.0c is not one of the selected updates…. *ahrg*

 
Published by packi on

It seems that Thursday mornings and GDB don’t mix that well:

*** glibc detected *** double free or corruption (out): 0x0000000000530a20 ***

Program received signal SIGABRT, Aborted.
[Switching to Thread 98311 (LWP 3468)]

Well that wouldn’t be that dramatic since we got valgrind to find memoverwrites, right? Wrong:

**3502** new/new[] failed and should throw an exception, but Valgrind
   cannot throw exceptions and so is aborting instead.  Sorry.
==3502==    at 0x11A17958: VALGRIND_PRINTF_BACKTRACE (valgrind.h:321)
==3502==    by 0x11B1A281: operator new(unsigned long) (vg_replace_malloc.c:168)
==3502==    by 0x123DC304: (within /usr/lib/gcc-lib/x86_64-pc-linux-gnu/3.3.4/libstdc++.so.5.0.6)
==3502==    by 0x123DBA92: std::__default_alloc_template<true, 0>::allocate(unsigned long) 
(in /usr/lib/gcc-lib/x86_64-pc-linux-gnu/3.3.4/libstdc++.so.5.0.6)
…

It seems that valgrind didn’t detect any mem overwrites or other anamolies related to these issues. Since I’ve been hunting that “bug” since 4:00am I’ll skip breakfast and go straight to bed.

 
Published by packi on

It seems that GDB has some issues with optimised glibc builds. This wouldn’t be that drastic, but it makes KDevelop unusable for debugging! And debugging something at 3:00 am with GDB is not very productive ;)
The problem with GDB seems to be that it doesn’t find the right stackframes in the binary and begins to print an endless stream of

…
#7  0x00000000 in ?? ()
#8  0x00000000 in ?? ()
#9  0x00000000 in ?? ()
#10 0x00000000 in ?? ()
#11 0x00000000 in ?? ()
#12 0x00000000 in ?? ()
#13 0x00000000 in ?? ()
#14 0x00000000 in ?? ()
#15 0x00000000 in ?? ()
…

This confuses KDevelop (not surprising at all, since its really an endless stream) and even after killing the debugger it refuses to restart the debugging process.

Now I’m tired and on the way to my bed. Class begins at 10:00am this morning, who needs sleep anyway ;)

 
News 14 years Ago
 
Published by packi on
WebKit?

The WebKit contains the rendering engine (based on the KHTML-engine) used in Safari, Mail, the Dashboard and many other projects to display HTML content.

 With all the news about Apple moving to the x86-platform I’ve missed the news about the WebKit becoming opensource.

It seems that the requests from the KHTML develpers have been heard, they “complained” that they were not granted access to Apples bugtracking-system even with an NDA now they can even checkout the source from their cvs server. You may have noted the quotes arround “complained” that is, because they never have, it was just that they were tired of people demanding features ported back from the Safari fork, saying that “It’s the same codebase, right?”. Well, it’s the same code base but it diverged rather badly and changes in the Safari-tree are not that trivial to integrate in the KHTML-tree.

Anyway, since I couldn’t resist to see the ACID2 test running on my machine, I had to stop by at opendarwin for instructions on how to build and run Safari with the latest WebKit:

Bootstrap it via CVS (password: “anonymous”):

cvs -d :pserver:anonymous@anoncvs.opensource.apple.com:/cvs/root login
cvs -d :pserver:anonymous@anoncvs.opensource.apple.com:/cvs/root co -P WebKitTools

Run the update-script (fetches the sources) and build it:

WebKitTools/checkout
WebKitTools/Scripts/build-webkit

With the checkin restrictions the project has in place, it’s very likely that the build has succeeded, lets run Safari with the cvs-version of the source!

WebKitTools/Scripts/run-safari

As you can see on the top of this article, the ACID2 enabled build is up and running ;-)

 
Published by packi on

Now this is a cool project! As seen on this slashdot post, its a LED floor, controlled by a laptop running Linux and it even has a xmms-plugin to sync it to the sound. ;)

The resolution is really bad though, I think I’ll stick to my video projector ;)

 
Published by packi on

 After my failed attempt to repair my broken Archos player (scratching a +5V cable over the board isn’t very healthy) and as a “Mac mini” user I felt the urge to get me an iPod.
Fortunately our store at school has iPods at very moderate prices which took another obstacle out of my way, I’ve ordered a silver one. If I had ordered the pink one, I might just have gotten it by now, but apple seems to have a shortage of silver iPods ;-P. You’ll hear again from me, when I get it…

 
Published by packi on

PBS has an interesting article about four incidents of technical failures or human errors that may have led to a nuclear war, if it wasn’t for the “professional military officers” who decided against a “retaliatory” strike.

 
Published by packi on

…when I’ve posted the last article. Or how I’ve managed to “trash” the RAID at work. Unplugging a SCSI device on a running system didn’t seem all that bad to me, since it was the only device on this controller (the server I was working on, has two).

Somehow the controller managing our raid must have gotten a message from the linux kernel and went into a “something bad happened” mode and refused to work. The marvelous (attention, irony inside) SCSI-BIOS gave me the choice of

  1. Create
  2. Delete
  3. Rebuild

At 23:30 you’d like a menu entry that says: “Check consistency”, which is excactly what “Rebuild” does, fortunately. We were up and running again at 01:30… (yeah thats ‘am’ for you non europeans ;)

Oh, and I’ve got me a mac mini… ;)

 
Published by packi on

 They did it again. Now that they’ve proven that a MP3-player priced at $400 gets bought by numerous peoples, they have introduced a Mac called “Mac mini” for $499. Including neither a keyboard nor a display device and being smaller (and certainly better looking) than the ordinary MiniITX, I’m tempted to say this is the “switcher”-Mac.
If you bought a PC 3 years ago and have to replace it now, you could either go for a cheap, bulky PC or a neat little Mac mini at the same price. The hardcore gamer won’t get himself a Mac anyway but I don’t think Apple targets that market (yet), but all standard office tasks can be done using a Mac. As you they could be using Linux, but using Linux as a “User” is not really an option if you don’t have a sysadmin nearby installing/maintaining the box. With all the nasty Spyware flying around the ‘net the office-type users would at least not turn their computers in massmailing-machines distributing spam and virii by using a Mac.
Being a software developer I’m always curious what it’s like to develop on other platforms and it’s said that the OS X has an excellent API and everything just looks good™ even if you’re very talented in making bad looking GUIs (but I’m convinced that I could design a GUI that look bad even on OS X ;).
Since my birthday is just around the corner and they’re not available in Switzerland for another two Weeks, I just might get me one as a present to myself (and canceling the order for the GF6800U which was ordered in June 2004!). I haven’t even mentioned the gadget-factor that this piece of hardware has (I feel my power rising ;-)

And if the OS proves to be programmer incompatible it would still make a nice paperweight ;-).

 
Published by packi on

Ever needed to include a circuit diagram into your LaTeX-document? The Circuit macros will help you getting through this venture:

“This is a set of macros for drawing high-quality line diagrams to include in TeX, LaTeX, or similar documents. The macros were originally intended for electric circuit diagrams containing fundamental elements, amplifiers, transistors, and basic logic gates, but several tools and examples of other types of diagrams are also included. Elements can be scaled or drawn in any orientation, and are easy to modify. The advantages and disadvantages of such a system are similar to those of TeX itself, which is macro-based and non-wysiwyg, with ordinary character input.”

This way of creating circuit drawings may not appeal to all people, but I (who would have guessed) like it. It works on Linux/tetex and Windows/Cygwin (with the LaTeX package provided by cygwin, others from my class had trouble rendering the diagrams with MikTeX).

If you’re not using LaTeX to gerate your documents you can of course just include the images generated by LaTeX in your favourite wordprocessor.

 
Published by packi on

While installing a package on my gentoo machine, a neat little signal/slot library caught my eye:

http://libsigc.sourceforge.net/

For those (I’m pretending somone reads this stuff but, can tell from the server-logs only google does) who don’t know what signal/slots are, lets put it like this: among other things, they save you from giving away your soul and do some pagan vodoo with function-pointers.

Just to scare you away:

char** (CMyClass::*pointerToMyClassType)( void* )  
         =  &CMyInstance::ReturnPPCharAndTakeVoidP

The above translates to: Pointer named pointerToMyClassType to a member-function of CMyClass which takes a void* parameter and returns a pointer to a pointer of character.
I think I’ll spare you the invocation part ;)

Acoording to their benchmarks, they’re even faster than the Qt™ implementation from TrollTech.

Previous page  12