Nested functions in GCC

GCC supports “nested functions” using the -fnested-functions flag. When I first saw this, I was excited: closures in C! In the famous words of Admiral Ackbar, “it’s a trap!”

  1. #include <stdio.h>
  2.  
  3. typedef int (*fptr)(int);
  4.  
  5. fptr f(int arg) {
  6.   int nested_function(int nested_arg) {
  7.     return arg + nested_arg;
  8.   }
  9.  
  10.   return &nested_function;
  11. }
  12.  
  13. void smash(int arg) {
  14.   return;
  15. }
  16.  
  17. int main(void) {
  18.   fptr g = f(10);
  19.   printf("%d\n", (*g)(5));
  20.   smash(12);
  21.   // printf("%d\n", (*g)(5));
  22.   fptr h = f(12);
  23.   printf("%d\n", (*g)(5));
  24.   printf("%d\n", (*h)(5));
  25.  
  26.   return 0;
  27. }

Try compiling (gcc -fnested-functions). What does the second call to g produce—15 or 17? Try uncommenting line 21. What happens? Does commenting out line 20 affect this? What if line 19 is commented out, but lines 20 and 21 are uncommented?

I’m not sure this feature is worth it.

* Filed by Michael Greenberg on 2010-03-03 at 11:06am under Blog,Programming Languages,Software
* 5 Comments

a weasel in a hat

Type theory comes of age

The current issue of the CACM has an article, Type theory comes of age. Not only is my advisor prominently depicted, but they also mention my area of research!

Thanks to Nate Foster for pointing this out to me.

* Filed by Michael Greenberg on 2010-02-12 at 10:18am under Blog
* 1 Comment

a weasel in a hat

Gravatar support

I’ve added gravatar support to the website. The gravatar system is a clever way to create identity on-line: e-mails are associated with images. Critically, the URL associated with an e-mail doesn’t include the e-mail itself, but instead an MD5 hash — this way, spammers can’t harvest your address.

I was disappointed to discover that Google Chat doesn’t have support for gravatars — a Google labs feature I would use.

* Filed by Michael Greenberg on 2009-05-31 at 1:40pm under Blog
No Comments

a weasel in a hat

The University of Pennsylvania

Last week I accepted the University of Pennsylvania‘s offer to study for a PhD (in PL). A few cool people there:

I’m really excited!

* Filed by Michael Greenberg on 2007-04-16 at 5:27pm under Blog
* 3 Comments

a weasel in a hat

A New Term

A new term starts on Wednesday, but the following are all on the list for discussion:

  1. Dan Friedman, et al. The Reasoned Schemer
  2. Flapjax: lifting and a few growing pains
  3. My thesis

* Filed by Michael Greenberg on 2007-01-22 at 11:59am under Blog
No Comments

a weasel in a hat

Weaselhat?

Harking to Mark Liberman of Language Log‘s call for science blogs, I’ve started up this bit of silliness. I’m a computer science student, focusing on verification, temporal logic, and programming language theory. You know, the usual.

The name is random, left over from software I used to write. I have my own image, but try to free associate the two: weasel, hat; hat, weasel. Yes, yes, that’s good.

* Filed by Michael Greenberg on 2006-06-21 at 7:07pm under Blog
* 1 Comment

a weasel in a hat