Re: sizeof pointer vs sizeof struct

Jay Vosburgh <[email protected]> Thu, 29 Feb 2024 18:10:30 -0800
Newsgroups org.kernel.vger.xdp-newbies
Message-ID <24438.1709259030@famine>
Logan B <[email protected]> wrote:

>Hello,
>I was recently working through the xdp tutorial and in the
>packet01-parsing lesson the sizeof a pointer to the ethernet header
>struct is used, not the sizeof the struct itself[0]. I peeked and the
>solution for this section also still uses the sizeof a pointer and not
>the struct so this isn't part of the tutorial and I was wondering what
>is going on? I don't think the verifier is re-writing these addresses,
>only those for the memory access into the packet data.

	The referenced code at [0] is:

	struct ethhdr *eth = nh->pos;
	int hdrsize = sizeof(*eth);

	"*eth" means "what eth points to," so this is indeed taking the
sizeof struct ethhdr.

	I suspect you missed the "*" in your reading of the code; in
this context, "*" is the indirection operator, per K&R 2, (The C
Programming Language, 2nd Edition), Appendix A 7.4.3.

	-J

>
>#include <stdio.h>
>#include <linux/if_ether.h>
>
>int main(void)
>
>       {
>
>                           struct ethhdr normal = {0};
>    struct ethhdr *eth_hdr_ptr;
>
>     printf("Size of struct %lu\n",sizeof(normal)); // prints 14
>    printf("Size of struct pointer %lu\n",sizeof(eth_hdr_ptr)); //
>prints 8
>    return 0;
>}
>
>
>[0]https://github.com/xdp-project/xdp-tutorial/blob/master/packet01-parsing/xdp_prog_kern.c#L34
>--
>Logan
>

---
	-Jay Vosburgh, [email protected]