sizeof

零基础入门之结构体字节对齐

会有一股神秘感。 提交于 2019-12-04 08:32:59
一、字节对齐的规则: 1、一般设置的对齐方式为1,2,4字节对齐方式。结构的首地址必须是结构内最宽类型的整数倍地址;另外,结构体的每一个成员起始地址必须是自身类型大小的整数倍(需要特别注意的是windows下是这样的,但在linux的gcc编译器下最高为4字节对齐),否则在前一类型后补0;这里特别提到的是数组一定要注意,而且在一些编程的技巧中,我们可以使用数组强制字节达到对齐的目的。这在网络编程中是很常见的。 举例:比如CHAR型占用空间为1字节,则其起始位置必须可被1整除。INT为4字节,其起始位置必须被4带队,依次类推。(我们假定类或结构体的起始位置为0位置,其实编译器是在开辟空间时,会寻找起始位置可被结构内最宽类型整除的地址做为开始地址,因此我们可以假定其为0值,因为这0值可以被任意的类型整除。) 2、结构体的整体大小必须可被对齐值整除,默认4(结构中的类型大小都小于默认的4)。 3、结构体的整体大小必须可被本结构内的最宽类型整除。(其实和上一条是一样的,但这里独立出来,起注意作用。比如结构体里的有DOUBLE,那么结构的大小最后必须可被8整除) 注意:GCC不是这样,就是最高只能被4整除。此为32位系统,64为系统也会采用8整除的方式。否则(2、3条),编译器会在结构的最后添充一定的特定字符来补齐。 struct T { char ch; double d ; };

SCTP编程

旧城冷巷雨未停 提交于 2019-12-04 08:27:58
1. 简介 SCTP是为了在IP网上传输信令而 由IETF的信令传输工作组(SIGTRAN) 提出的传输层协议(RFC2960,RFC4960)。 和TCP,UDP相比, UDP是无连接的传输协议,它能满足低延迟的要求,但是它却无法保证可靠传输。TCP能保证数据可靠传输,但是它也不能完全符合信令传输的要求;TCP套接字不支持多宿性;TCP是面向比特流的,将数据传输当作是没有结构的字节序列。 2. SCTP的基本概念 多宿性(multi-homing) 多宿是指一个SCTP 端点可以通过多个IP地址到达,这样两个SCTP端点在建立了关联后,数据可以通过不 同的物理通路进行传送。也就是说,当一条通路坏掉后,可以通过另一条通路到达对端。 多流性(multi-streaming) 由于采用多个流进行传输而且各个流相互独立,这样当一个流中的数据包需要重传,其他流中的数据可以 继续传输, 解决了在TCP单流中容易出现的队头阻塞现象(head-of-line). 安全机制 SCTP采用“四次握手”的连接建立方式和COOKIE机制消除了SYN攻击的威胁, Cookie机制设立的主要用意 是将状态信息存储在客户端或者网络上,而非服务器内存中,它的使用将服务器资源预留的时间推迟到了 Cookie带回完整的鉴别信息后。这是一种简单有效的防御DoS攻击的方法。 3. SCTP编程 Linux内核从2

C++ sizeof C-style string / char array - optimization

谁都会走 提交于 2019-12-04 06:58:36
问题 I'm a student at university. I work mostly with Java, C++ is very new to me, so I probably make many silly mistakes and I have upcoming exams to cope with. Don't be too harsh with me. Note: I can NOT use C++ std::string because I need to work with C-strings due to university tasks! Referring to my studies and the question I asked about pointers and const arguments (which you find here) I tried messing around with memory management but it seems it has no effect, or I just misunderstood some

C# sizeof(enum) alternative? (to workaround resharper false error)?

孤街醉人 提交于 2019-12-04 06:41:08
In C# I've got some "safe" API code related to UAC elevation. It involves getting the size of an enum (as follows) int myEnumSize = sizeof (MyEnum); The code itself is valid, compiles, works correctly etc. But Resharper falsely flags it as a an error ("Cannot use unsafe construct in safe context") within the solution. ( Starting with version 2.0 of C#, applying sizeof to built-in types no longer requires that unsafe mode be used. ) I love Resharper, and I love the solution analysis, but with this code in the solution I have a big red dot in the corner that makes me always think something is

Amber

回眸只為那壹抹淺笑 提交于 2019-12-04 06:36:22
训练做的题里有板子单独拉出来。 欧拉筛 1 int vis[N+5],prim[N+5]; 2 int cnt; 3 void Eular() 4 { 5 vis[0]=vis[1]=1; 6 for(int i=0;i<N;i++) 7 if(!vis[i]) 8 { 9 prim[cnt++]=i; 10 for(ll j=(ll)i*i;j<N;j+=i) vis[j]=1; 11 } 12 } View Code 辗转相除法求逆元 1 ll ans,re; 2 void e_gcd(ll a,ll b) 3 { 4 if(b==0) 5 { 6 ans=1,re=0; 7 return; 8 } 9 e_gcd(b,a%b); 10 ll temp=ans; 11 ans=re; ///*** 12 re=temp-a/b*re; 13 } View Code SPFA找环【bfs 1 int vis[N]; 2 bool spfa_bfs() 3 { 4 ans[s]=v; 5 vis[s]=1; 6 queue<int> qu; 7 qu.push(s); 8 while(!qu.empty()) 9 { 10 int node=qu.front(); 11 qu.pop(); 12 vis[node]=0; 13 for(int i=1;i<=n;i++) 14 {

删除目录(非空)、复制目录(非空)

谁说我不能喝 提交于 2019-12-04 06:27:39
本来想自己写的,看到一份博客已经总结的很完整了。直接引用下: 转载请注明原文链接。 原文链接: http://www.cnblogs.com/xianyunhe/archive/2011/12/06/2278550.html 文件和文件夹的创建、复制、删除、重命名等操作是经常要用到的,作者根据自己的经验,并查询了MSDN,特意总结了常用文件和文件夹的相关操作,重点讨论了复制整个文件夹和删除整个文件夹 1、文件操作基本函数 WinBase.h中声明了 Windows 平台下的基本的 API 函数,包括文件和目录的基本操作。 下面列出部分常用的文件操作相关函数。 函数 说明 DeleteFile 删除单个文件,不能删除目录和只读文件 CopyFile 复制单个文件 MoveFile 移动移动文件或目录 CreateDirectory 创建目录 RemoveDirectory 删除空目录 更多的函数可查询 Winbase.h 文件或者 MSDN 中的 File Management Functions 和 Directory Management Functions 。 2 、复制目录和删除目录 WinBase.h 中的文件操作函数中并没有直接实现整个文件夹的复制和删除操作的函数,需要自己实现。 (1)判断指定路径是否有效目录 1 /*判断一个路径是否是已存在的目录*/ 2 BOOL

Invalid read/write of size 8 2

假装没事ソ 提交于 2019-12-04 05:42:14
问题 While working on my school project I keep receiving following error from Valgrind after compiling my project on Unix school server and being unable to run the program, as I receive "Segmentation fault: 11". ==95183== Memcheck, a memory error detector ==95183== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al. ==95183== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info ==95183== Command: ./Euler ==95183== ==95183== Invalid read of size 8 ==95183== at 0x400B65:

Struggling to get number of chars in char* [duplicate]

為{幸葍}努か 提交于 2019-12-04 05:22:22
问题 This question already has answers here : How to get the number of characters in a std::string? (12 answers) C sizeof char pointer (5 answers) Closed 6 years ago . char* string = "hello there"; cout << sizeof( string ); // prints 4, which is the size of pointer cout << sizeof( *string ); // prints 1, which is the size of char How do I get the number of characters contained in the string (11)? 回答1: It's strlen you want for that, not sizeof . The first counts the number of characters up to the

Can size of pointers to non-union classes differ?

折月煮酒 提交于 2019-12-04 05:01:21
I understand there are HW platforms where you need more information to point to a char than you need to point to an int (the platform having non-addressable bytes, so a pointer to char needs to store a pointer to a word and also an index of a byte in the word). So it is possible that sizeof(int*) < sizeof(char*) on such platforms. Can something similar happen with pointers to non-union classes? C++ allows covariant returns types on virtual functions. Let's say we have classes like this: struct Gadget { // some content }; struct Widget { virtual Gadget* getGadget(); }; Any code which calls

C++ 获取windows一些基本信息

一世执手 提交于 2019-12-04 04:57:18
1. 获得本机主机名 GetModuleFileName(NULL,info.InstallPath,128);//程序运行路径 GetUserName(user_name, &dwBufferLen);//用户名 char hostname[MAX_PATH] = { 0 }; gethostname(hostname, MAX_PATH);//主机名 GetComputerName(hostname, &dwBufferLen);//主机名 2.根据主机名获取IP struct hostent FAR* lpHostEnt = gethostbyname(hostname); // 取得IP地址列表中的第一个为返回的IP(因为一台主机可能会绑定多个IP) LPSTR lpAddr = lpHostEnt->h_addr_list[0]; // 将IP地址转化成字符串形式 struct in_addr inAddr; memmove(&inAddr, lpAddr, 4); char* m_strIP = inet_ntoa(inAddr) 3.根据IP获取MAC HRESULT hResult; IPAddr ipAddr; ULONG pulMAC[2]; ULONG ulLen; char strMacAddr[100] = { 0 }; ipAddr = inet