?

Log in

No account? Create an account
So, I've got this coworker. Maybe he's an idiot. I want to say the… - Blather, Rinse, Repeat
July 31st, 2007
09:50 pm

[Link]

Previous Entry Share Next Entry


So, I've got this coworker. Maybe he's an idiot. I want to say the jury's still out.

We're using this super brittle compiler for our current project, and we bump into behavior that's summed up by a tiny test case here. Consider this code as an entire project:

extern void bar();

void foo()
{
  bar();
}

int main()
{
  return 0;
}


In my coworker's estimation, this code should not link. You've got foo calling an undefined extern, bam, it's no good. In my opinion, nobody's calling foo, nobody's calling bar, it's just fine.

At one point, he's trying to convince me of his point of view, and he says that no reputable compiler would behave the way I describe. Microsoft's compiler doesn't. GNU's compiler doesn't. I say that it seems fine. Microsoft plays fast and loose with language specifications, so I don't consider them a standard to judge by.

Turns out, the guy's wrong - MS and GNU behave differently. And our flaky new compiler works the same way GNU's compiler does.

"no reputable compiler". Please, let's not stoop to insults.


(4 comments | Leave a comment)

Comments
 
[User Picture]
From:sboyko
Date:August 1st, 2007 09:33 pm (UTC)
(Link)
I guess it depends on what the compiler's optimization does. If it recognizes that foo() never gets called, then who cares about bar()? The linker wouldn't even see it.

Jeff Foxworthy should start a new line. "You might be a C++ idiot..."

[User Picture]
From:tsmaster
Date:August 1st, 2007 09:40 pm (UTC)
(Link)
I almost hesitate to call that an optimization - sure, you could link all sorts of cruft into the final EXE, but surely only putting in the stuff that you need is a perfectly reasonable decision.

I suspect that several linkers out there do put everything in and then go through a pruning phase to pull stuff out that can be identified as unused, but it strikes me that going the other way might make just as much sense, and only putting in the stuff that can be verified as actually being used.


In any case, I totally agree - who cares about bar()? My coworker seemed to be pretty excited, but I think he's nuts.
[User Picture]
From:sboyko
Date:August 1st, 2007 09:54 pm (UTC)
(Link)
It's been a long time since my compiler class, but I agree it makes a lot more sense to toss the cruft at the compiler stage and let the linker deal with only the "good stuff". Since modern compilers do all the good stuff like inline functions when they think it will help, it totally makes sense to me they would optimize a function away if it is never used.

In any case, the extern is just an advisement to the compiler what the type of this potential external variable is, not a command to go forth and look for it.
[User Picture]
From:tsmaster
Date:August 1st, 2007 10:08 pm (UTC)
(Link)
it makes a lot more sense to toss the cruft at the compiler stage

True, do as much as early as possible sounds good.

But what I was thinking was about the linker - do you start with everything, and look for stuff to prune out, or do you start with main() and look for things that you have to add in?

I suspect there's a marginal gain to be had going the second way, much in the same way that naive garbage collection algorithms are confused by reference loops.
My Website Powered by LiveJournal.com