News for hackers. Stuff that matters.
Re:Ask HN: Favorite pointer tricks in C? by tptacek on Saturday November 20, 2010 @06:12PM (#1925661)
* Using pointer offsets to get to the stack frame pointer, and then walking the frame pointer backwards to get the call stack.

* Using && to take the address of a jump label.

* Casting a u_int32_t over a 4-byte string (like an SMTP verb) to get a value you can switch() on.

Re:Ask HN: Favorite pointer tricks in C? by defen on Sunday November 21, 2010 @12:08AM (#1926365)
* Using && to take the address of a jump label.

Had to look that one up...turns out it's a GCC trick that allows you store the address of a jump label into a pointer to void. Later on, you can do "goto *ptr" to jump back to that address. Neat. See http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html

(You obviously already know this...just putting it here in case anyone else hasn't heard of it and is curious)

Re:Ask HN: Favorite pointer tricks in C? by mansr on Sunday November 21, 2010 @07:29AM (#1926964)
Being a gcc extension to the C language, I recommend this construct never be used and certainly not taught.
Re:Ask HN: Favorite pointer tricks in C? by brl on Saturday November 20, 2010 @08:19PM (#1925893)
> Casting a u_int32_t over a 4-byte string (like an SMTP verb) to get a value you can switch() on.

This is awesome beyond words. If I stumbled across this in the wild I would flip flop between awe and disgust until my head exploded.

Let me guess, "SMTP verb" is not a hypothetical example?

Re:Ask HN: Favorite pointer tricks in C? by mansr on Sunday November 21, 2010 @07:34AM (#1926969)
There are at least three common mistakes involving tricks like this:

1. Alignment. Many CPUs will trap (or worse) if accessing a 32-bit quantity at a non-aligned address.

2. Endianness. Needless to say, the 32-bit value read for a given string depends on the machine endianness.

3. Aliasing. Casting between different pointer types can result in a violation of the C aliasing rules and, with a little bad luck, incorrect results.

Re:Ask HN: Favorite pointer tricks in C? by jallmann on Sunday November 21, 2010 @12:11PM (#1927338)
Good to see you on hn.
Re:Ask HN: Favorite pointer tricks in C? by jallmann on Saturday November 20, 2010 @09:16PM (#1925992)
This is also the basis for the fourcc's used in multimedia. Checking them only requires an integer compare.
Re:Ask HN: Favorite pointer tricks in C? by defen on Sunday November 21, 2010 @12:14AM (#1926380)
Nginx does this too under certain circumstances...check out the ngx_strN_cmp macros in ngx_http_parse.c. (where N is an integer from 3..9)
Re:Ask HN: Favorite pointer tricks in C? by ay on Sunday November 21, 2010 @10:47AM (#1927214)
> Casting a u_int32_t over a 4-byte string (like an SMTP verb) to get a value you can switch() on.

How do you ensure your code is portable between the architectures with different endianness ? One trick may be using htonl on the u_int32_t to get it to a canonical format, but probably there would be better approaches ?