Re: sizeof pointer vs sizeof struct

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, Feb 29, 2024 at 9:10 PM Jay Vosburgh <jay.vosburgh@xxxxxxxxxxxxx> wrote:
>
> Logan B <mrbojangles3@xxxxxxxxx> 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
>
I did miss that. Thank you
Logan
> >
> >#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, jay.vosburgh@xxxxxxxxxxxxx



-- 
Logan





[Index of Archives]     [Linux Networking Development]     [Fedora Linux Users]     [Linux SCTP]     [DCCP]     [Gimp]     [Yosemite Campsites]

  Powered by Linux