Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

2STUPID4C

Name: Anonymous 2016-12-29 5:44

I have smoked a significant amount of 420 but still can't understand how this macro function works:
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})


https://github.com/torvalds/linux/blob/master/include/linux/kernel.h

Name: Anonymous 2016-12-29 7:16

>>1
As experienced developer of void.h this macro is a piece of cake.
offsetof gets you offset struct_type<>member distance,
when you apply offset to the ptr, you get to offset 0 which is the address of container of the ptr(all ptr are assumed to be member type).
In pseudocode: container_ptr=ptr - offset_from_containertype_to_member.

Name: Anonymous 2016-12-29 16:33

>>2
Oh I understand now, thanks >>2-sama!

Name: Anonymous 2016-12-29 17:33

Oh wow so you don't need to store any kind of parent information as a member. That's hot shit.

Name: Anonymous 2016-12-29 18:29

>>4
Correct, that's for instance how Linux's list ADT (struct list_head) works.

Name: Anonymous 2016-12-29 20:57

JACKSON 5 GET

Name: Anonymous 2016-12-30 1:02

>>6
lmao fail

Name: Anonymous 2016-12-30 7:59

>>2
What's the point of the typeof stuff on the first line? It's cast to char * for byte pointer arithmetic anyway, so wouldn't char *__mptr = (char *)(ptr); work fine?

Name: Anonymous 2016-12-30 10:06

>>8
I presume it's for rudimentary typechecking: the compiler will complain if ptr is not, in fact, a pointer to a value of type.member's type.

Name: Anonymous 2016-12-31 22:10

>>8
I'm not sure if that works on ARM...

Name: Anonymous 2016-12-31 23:53

#include <stdio.h>
#include <stddef.h>

// creates a struct ptr from a struct embedded as a member of a containing struct; see the example I made below
// the first line is instantiating a properly typed pointer, which is used in the second step
// to do pointer arithmetic to place the input ptr in the appropriate location for the "new" struct
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
// ^^ byte difference between start of the struct, [type], and [member]
// subtracting this from [ptr] yields a valid pointer to the start of [type]
// see this diagram of offsets:
// struct X {
// ... 0
// ... 4
// *__mptr 8
// }
// __mptr - 8 == (struct X*)...

struct weight_t {
int pounds;
};

struct mom_t {
struct weight_t weight;
};

int main (int argc, char** argv) {
struct weight_t your_moms_weight = { 400 };
printf("Your mom's weight: %d\n",
your_moms_weight.pounds);
struct mom_t* your_mom = container_of(&your_moms_weight,
struct mom_t,
weight);
printf("Your mom's weight: %d\n",
your_mom->weight.pounds);

return 0;
}

Name: Anonymous 2017-01-01 0:12

Another clear demonstration of which is the superiour language out of C and Haskell.

Name: sage 2017-01-01 8:57

>>1
I have smoked a significant amount of 420
i am 14 and i smonk ilegal marijuana cigatret fux da police

Name: sage 2017-01-01 9:02

smokes a joi nt

woah what if goneverment is bad

Name: Anonymous 2017-01-01 9:47

>>12
Haskell, obviously.

Name: Anonymous 2017-01-04 1:28

>>15
you mena haskal

Name: Anonymous 2017-01-05 11:47

mfw Haskell actually has raw pointers in Foreign.Marshal.Alloc

Name: Anonymous 2017-01-05 15:45

>>17
who r u /g/roting gro?

Name: Anonymous 2017-01-05 16:33

>>18 it implies container_of will also work in haskell(with plain structs of course) since Haskell has full access to raw pointers with IO and malloc. i.e. you can create a C struct, allocate it and get the container_member offset 0 (the parent struct) just like in C

Name: Anonymous 2017-01-05 17:58

It's a disgusting violation of type safety, which is not allowed on good hardware. C and Linux wouldn't even run on it.

Name: Anonymous 2017-01-05 18:20

Imperative patriarchy intruding into "safe typespaces"
https://github.com/fpco/inline-c

Name: Anonymous 2017-01-05 18:49

>>20
Where would one obtain this ``good hardware" that C and Linux wouldn't run on?

also check em

Name: Anonymous 2017-01-05 19:53

>>22
Its a secret kept from imperative patriarchy so they'll not port any Cee compiler for it.

Name: Anonymous 2017-01-06 18:25

>>23
Cee
LLLLLLLLLEEEEEEEEEEEEEELLLLLLLLLLLLLL E/C2/IN MEME /C2/RO XDDDDDDDDDDDDDDDDDDDDDDDDDDD

Name: Anonymous 2017-01-06 20:27

>>24
nice shit

Name: Anonymous 2017-01-07 0:50

E//prog//IN POST />>24/-KUN!

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List