To decipher weird c declarations go to http://cdecl.org/ and type your type. It works for most cases... good luck trying to figure out templates, though, for template metaprogramming you are on your own.
Tuesday, 12 November 2013
Human friendly c declarations
Tuesday, 22 October 2013
Some gratuitous MSVC bashing
I can only imagine how incredibly ugly their lexer must be to say it's not a fixable problem.
Thursday, 3 January 2013
"The VM session was closed before any attempt to power it on"
Tuesday, 15 February 2011
Vim Tip: Vigor
Well, Vim and Vigor have a different meaning in Linux. Just do an apt-get install vigor, then run it. You'll have lots of fun with Vim's evil cousing, I promise.
Thursday, 18 November 2010
Brillant corporate inteligence
Don't worry though. Now it'll be renamed to #define ORACLE.
PS: Kind of related is the story about Oracle breaking everything after a %s/Sun/Oracle in the JVM [1], though in that case I'm more inclined to blame sloppy programmers.
[1] http://it.slashdot.org/story/10/07/28/2121259/Oracles-Java-Company-Change-Breaks-Eclipse
Tuesday, 7 September 2010
C++ linking WTF
It is a commonly accepted fact that a succesfuly compiled application serves as enough proof of its correctness, but common wisdom doesn't say a thing about linking. If you like linker WTF moments, you'll love this snippet. Can you guess why won't it compile?
struct Foo {
static const int x = 0;
static const int y = 1;
int z(bool x){
return (x)? Foo::x : Foo::y;
}
};
int main() {
Foo z;
std::cout << z.z(true);
return 0;
}
Well, it does compile (gotcha!) but it just won't link. Yet it seems so simple... let's add some more mistery to this WTF moment, try this change:
int z(bool x){
int t = Foo::x;
return (x)? t : Foo::y;
}
Holy shit, now it compiles? WTF? Some more strangeness:
int z(bool){
return (true)? Foo::x : Foo::y;
}
And again, now it compiles. WTF? I'll make a final change, this one should give you a clue about why it won't compile. Revert all changes back to the original code but add this two lines after Foo:
const int Foo::x;
const int Foo::y;
Though weird at first, now you should have a clear picture:
- The first case doesn't compiles: x and y are declared in struct Foo, yet the linker doesn't know in which translation unit they should be allocated.
- The second and third cases... well I'm not sure why does this compiles but it's probably because the linker can asume in which translation unit x and y should be allocated. I'm to lazy to check.
- In the last case we explicitly say where should x and y be. According to standard, this is how these two ints should be declared.
So, some linker strangeness. Beware, it's easy to get trapped by this one.
Thursday, 2 September 2010
Vim Sexual Care
Thursday, 26 August 2010
Date time WTF
Tuesday, 8 June 2010
Just WTF
CREATE PROC shutdown10
AS
EXEC xp_cmdshell 'net send /domain:SQL_USERS ''SQL Server
shutting down in 10 minutes. No more connections
allowed.', no_output
EXEC xp_cmdshell 'net pause sqlserver'
WAITFOR DELAY '00:05:00'
EXEC xp_cmdshell 'net send /domain: SQL_USERS ''SQL Server
shutting down in 5 minutes.', no_output
WAITFOR DELAY '00:04:00'
EXEC xp_cmdshell 'net send /domain:SQL_USERS ''SQL Server
shutting down in 1 minute. Log off now.', no_output
WAITFOR DELAY '00:01:00'
EXEC xp_cmdshell 'net stop sqlserver', no_output
Monday, 10 May 2010
Random WTFs
Random WTF 1
Note: Use dapper instead of edgy to use Ubuntu Dapper
Random WTF 2
I've been working with a big-co supplied ACS server, but it's IE only. WTF!? Aren't ACS all XMLy so they can work everywhere? I hate you all.
Tuesday, 4 May 2010
Ubuntu: Sound still FUBAR'd
Remember my problems with dual screen support in Ubuntu? Well, I still love bashing Ubuntu, and the sound system in Linux is certainly a topic to rant a lot. Making the sound work fine in Ubuntu is an odyssey in pain and frustration, unless it works fine out of the box. And even if it does, it may still have it's kirks. Lots of them.
In my case the sound starts in mute. I know it's a problem with pulse (which is a WTF in itself) and alsa, I don't really care what's the problem though, I just want to play my mp3s collection without having to carefully turn the knobs up to eleven in alsamixer.
After trying a lot of the "solutions" found on the internets I've decided the best thing to do, short of switching back to windows me, is adding the following to my "fix_ubuntu_fuckups.sh" start script, which already contains my dual-screen pseudofix:
amixer -c0 -- sset Master playback -0dB unmute
amixer -c0 -- sset Headphone playback 0dB unmute
amixer -c0 -- sset Front playback 0dB unmute
amixer -c0 -- sset PCM playback -16dB unmute
This sets alsamixer to normal volume levels. As for the real fix, I'll wait till the next Ubuntu version. I wonder which sound subsystem will they chose next time.
Monday, 29 March 2010
Operator sizeof (AKA Reading Berkeley's FM, take II)
Last time I told you about an evil snipet I found on Oracle Berkeley DB's manual:
skey->size = sizeof((struct student_record *)pdata->data)->last_name;
And we concluded it's trying to... well, dereference a number. And yet it compiles. What the hell is going on there?
The answer here is in the subtleties of the sizeof operator. That's right, operator, not function. Plus is an operator. Less is an operator. * is a (unary) operator. sizeof is a unary operator too. The relevance of this is that operators can behave in more bizzare ways than functions do. In this case there's a difference between this two lines:
MyClass x;
int a = sizeof(MyClass);
int b = sizeof(x);
A very subtle difference. Can you spot it? a and b will have the exact same value, rest assured. The difference is in the operator itself: sizeof MUST have parenthesis when applied to a type name, yet parenthesis are optional when applied to an instance of a datatype, so this code is legal:
MyClass x;
int a = sizeof(MyClass);
int b = sizeof x;
Oh, wait, the fun doesn't stop there: sizeof also has bizarre precedence order, meaning it won't get applied as you expect it. So, this is valid too:
struct MyClass { int y; } x;
int b = sizeof x->y;
Can you see where we are going? Knowing that sizeof will be applied last lets you write something like this too:
void *ptr = ...
int b = sizeof((X*)ptr)->y;
Which means nothing else than "store in b the size of member y in struct X. It should be easy to see why BDB's example does compile, and why did I spend half an hour trying to understand the reason it compiled fine.
By using some more casts and a clever arangement of parenthesis you can come up with a great job security device.
Friday, 26 March 2010
Reading Berkeley's FM
I got this from Oracle Berkely DB's FM:
skey->size = sizeof((struct student_record *)pdata->data)->last_name;
Take a good look at that pice of code:
a_number = sizeof((T*)pdata->data)->last_name;
Again:
a_number = sizeof(Whatever)->field;
Wait a minute. typeof(sizeof(x)) == const unsigned int. Right? So, again:
a_number = 42->field;
There's no way that first line can compile. Go and check it (in the example, not the last line please). I'll wait. Done? Yeap, I was surprised to, it does indeed compile. Mi first reaction towards this discovery went something like this:
What is going on there? It took me a while to figure out how evil Berkely 's manual can be. The answer next time.
Tuesday, 23 March 2010
You know you're a geek...
... when you try to log in to your homebanking account using admin:admin (*) (**)
(*) Alt take: a geek with weak passwords, yeah. My pin is 1234 and I'll never change it.
(**) Replace homebanking with gmail, linkedin and $LATEST_NETWORKING_FAD and you're most likely mental. (***)
(***) Replace mental with (*) to obtain a Moebious post.
/Delirious posting mode, deactivate!
Thursday, 25 February 2010
C++: Composite objects and throwing constructors
Check out the following code:
struct Foo {
struct Foobar{ Foobar(){ throw "foobar'd"; } };
Foobar baz;
Foo() try : baz() {
cout << "Ctr 1n";
}catch(...){
cout << "Ctr 2n";
}
};
int main() {
Foo bar;
cout << "End!n";
return 0;
}
Nice, isn't it? Without using a compiler answer the following questions:
- Does it compile?
- If it does, does it abort or does it return 0?
- What does it print?
Did you think about it? Come on, I'll wait... ready? OK, lets go.
First we should think about something else: what the fuck is that thing? I would surely be horrified if I found something like that was lurking in one of my projects' code. It's hideous. And it's called a "Constructor function-try-block" (yes, answering the first question, it does compile and it indeed is valid C++ code).
A constructor function-try-block exists only to catch exceptions thrown while constructing a composite object. This may tempt us to answer the second question: it should return 0, as we're catching Foobar's exception on Foo's ctr. But that's not how these things work , otherwise this would have been a very boring entry: the program aborts.
To understand why does this program abort you should think what does it mean to have a composite object; there is no meaning in having a Foo without a Foobar. baz, of type Foobar is a part of Foo, just like a head is a part of a person and there's no person without a head (though many act as if they didn't have a working head, but that is a topic for another day).
Now, what does it mean to throw (regardless wheter this is a good or a bad practice) in a constructor? It is like saying "There's been a horrible error and I have no idea how to recover. Man, I give up". Throwing in a ctr means there's no point in trying to fully construct that object: it leaves a half-built thing. What can you do with that half object? Nothing, throw it away.
Now that we know what does it mean to throw in a constructor we can answer why does the program abort: there is no point in building a composite object when one of its constituting parts can NOT be constructed, thus it must throw as well.
The last point, why do we have function try blocks if we can't catch exceptions? Well... you can't catch it but you may rethrow a different one, or use the constructor to clean up the mess you would otherwise leave behind. Or you could write a snippet of code, post it on your blog and confuse a whole lot of people (all 3 of them reading this block. Hi grandma).
Oh, we had a third question, but you should be able to answer that one yourself.
PS: You may get a better explanation at GotW #66: Constructor Failures
Tuesday, 27 October 2009
D.A.T.A.E
Thursday, 15 October 2009
Write in C
When I find my code in tons of trouble, Friends and colleagues come to me, Speaking words of wisdom: "Write in C." As the deadline fast approaches, And bugs are all that I can see, Somewhere, someone whispers: "Write in C." Write in C, write in C, Write in C, oh, write in C. LISP is dead and buried, Write in C. I used to write a lot of FORTRAN, For science it worked flawlessly. Try using it for graphics! Write in C. If you've just spent nearly 30 hours Debugging some assembly, Soon you will be glad to Write in C. Write in C, write in C, Write in C, yeah, write in C. BASIC is for wimps. Write in C. Write in C, write in C, Write in C, oh, write in C. Pascal won't quite cut it. Write in C. Write in C, write in C, Write in C, yeah, write in C. Don't even mention COBOL. Write in C. And when the screen is fuzzy, And the editor is bugging me. I'm sick of ones and zeros, Write in C. A thousand people swear that T.P. Seven is the one for me. I hate the word PROCEDURE, Write in C. Write in C, write in C, Write in C, yeah, write in C. PL1 is '80s, Write in C. Write in C, write in C, Write in C, yeah, write in C. The government loves ADA, Write in C. Write in C, write in C, Write in C, yeah, write in C. Java's not quite there yet, Write in C.
Wednesday, 14 October 2009
Lighty WTF
Seen @ Lighttpd
242: srv->srvconf.reject_expect_100_with_417 = 1;
That line should totally be "= 42"