Live data from Hacker News

Ask HN: Favorite pointer tricks in C?

news.ycombinator.com

51–60 of 81 posts

Re: Ask HN: Favorite pointer tricks in C?

#51
post #5

What I think takes the cake is this: array[index] == index[array] Not that you would actually use this, but it gave me a lot of insight into how addressing and stuff works inside the compiler. Also from this example, there's the implicit suggestion that an array can be treated as a pointer. So that leads into pointer arithmetic which can be very useful.

This is a fantastic way to show younger or beginning programmers to think beyond what the code is supposed to denote and consider how it works in reality--in other words, the hacker's perspective. A naive programmer takes array[index] at face value. The hacker gets the idea that array[index] is just *(array + index) deeply enough to make a perverse joke out of it.

Re: Ask HN: Favorite pointer tricks in C?

#52
post #21

Regarding function pointer arrays, I'm doing something in labrea ( http://github.com/dustin/labrea ) where I need to make what is effectively an FFI call from C to C where I know the number of arguments, and their types, but I have them in the form of an array of unions. For example, if I need to call read, it's basically an invocation of a function with a 4 byte, 8 byte, and then 4 byte argument (on a 64-bit system)…

This isn't terribly portable FWIW. Not all calling conventions massage data into either a 32-bit or 64-bit argument. In particular, passing structs by value has some interesting rules even in the common calling conventions.

It will certainly work for 90% of the common function types out there and is a pretty common trick.

Re: Ask HN: Favorite pointer tricks in C?

#54
post #4

Would you care to share the learning objectives you are fulfilling with pointer tricks in C? Or if it's just some extracurricular entertainment?

Pointer tricks are dirty and scare the elders. cjtenny is playing the childless uncle who talks about things no parent would. Kids that age will eat it up :-)

Re: Ask HN: Favorite pointer tricks in C?

#55

Copy-free contiguous subsets of arrays are fairly simple but often convenient. If you want elements 5 through 33 of big_array, you just get a pointer to element 5, and keep track separately of the length. A common case is where you split an array into two non-overlapping subparts, in which case, if you no longer need the original, you can treat each subpart as if it were a separate array. Saves the work of allocating…

Also useful for storing parse results:

    ""

gets modified during parsing to become:

    ""


Your parse tree result result can then just contain pointers to "a\0", "href\0", and "blah\0" rather than doing any copying.

Re: Ask HN: Favorite pointer tricks in C?

#57
post #25

* 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.

* 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?

#58
A trick to save memory:

If you have a struct/class with a lot of members that are usually set to zero or some other initial value, you can store them in a "lookaside" structure that is hung off a global hash table with the pointer of the original object as the hashtable key. You can then use a bitfield to keep track of which members actually have interesting data.

So -- accessing the member would look something like this:

  int MyClass::get_foo() {
    if (foo_set_)
      return global_lookaside[this].foo;
    return 0;
  }

Re: Ask HN: Favorite pointer tricks in C?

#59
post #36
post #25

* 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.

> 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?

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)
Post reply on HN