简体   繁体   English

获取指针指向的数组的单个元素的 sizeof — char 指针

[英]getting sizeof individual element of array pointed to by pointer — char pointers

I have a char *c="hello" but if I do sizeof(*c) it will return 1 (1 byte).我有一个char *c="hello"但如果我执行sizeof(*c)它将返回 1(1 个字节)。 I like to know what is this size.我想知道这个尺寸是多少。 Is it size of individual character of first memory address for char *c="hello" like sizeof('h') and when I do sizeof(*(c+1)) etc still returns 1.它是char *c="hello"的第一个 memory 地址的单个字符的大小,例如sizeof('h')并且当我执行sizeof(*(c+1))等时仍然返回 1。

this is my system details这是我的系统详细信息

Architecture: x86_64架构:x86_64

CPU op-mode(s): 32-bit, 64-bit CPU 操作模式:32 位、64 位

Address sizes: 36 bits physical, 48 bits virtual地址大小:36 位物理,48 位虚拟

CPU(s): 4 CPU:4

also In bigger project I assigned char *c=payload_body_text the function is like following in the same project同样在更大的项目中,我分配了char *c=payload_body_text function 就像在同一个项目中一样

 somefunction(struct tpacket3_hdr *ppd)
  {
    struct ethhdr *eth = (struct ethhdr *) (ppd + ppd->tp_mac);
    struct iphdr *ip = (struct iphdr *) ( eth + ETH_HLEN);
    struct tcphdr *tcp=(struct tcphdr *)(ip+sizeof(struct iphdr));
    char *payload_body=(char *)(tcp + tcp->doff) ;
 }

So basically I like to know is payload_body is pointing to some area of object which is pointed to by ppd ?所以基本上我想知道payload_body指向的是ppd指向的 object 的某个区域? which I am sure my OS has allocated before packet entered into tcp-ip stack of the kernel.我确信我的操作系统在数据包进入 kernel 的 tcp-ip 堆栈之前已经分配了它。 tpacket3_hdr is from Packet MMAP. tpacket3_hdr 来自 Packet MMAP。 in a way you can call it like a packet.在某种程度上,您可以将其称为数据包。

Size() operator gives you the size of whole character. Size() 运算符为您提供整个字符的大小。 It gives you the size required to assign the variable in memory.它为您提供了在 memory 中分配变量所需的大小。 In case of 64 bit or 34 bit system, character variable reserve 1 byte address in memory.对于 64 位或 34 位系统,字符变量在 memory 中保留 1 个字节地址。

sizeof(*c+1) will return 4 (in case of 64 bit; in case of 32 bit it is 2) not 1. Check it again. sizeof(*c+1)将返回 4(在 64 位的情况下;在 32 位的情况下为 2)而不是 1。再次检查。

Because sizeof character and integer is 1 and 4 respectively.因为 sizeof 字符和 integer 分别为 1 和 4。 Result of character + integer expression is a integer.字符 + integer 表达式的结果是 integer。 So, *c+1 will generate an integer variable and hence it will print 4 because sizeof integer variable is 4因此, *c+1将生成一个 integer 变量,因此它将打印 4 因为 sizeof integer 变量为 4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM