sizeof

将服务端select设置为非阻塞,处理更多业务

不打扰是莪最后的温柔 提交于 2019-12-22 03:39:27
服务端代码: #include<WinSock2.h> #include<Windows.h> #include<vector> #include<stdio.h> #include<iostream> #pragma comment(lib,"ws2_32.lib") enum CMD { CMD_Login, CMD_Login_Result, CMD_Logout, CMD_Logout_Result, CMD_ERROR }; //包头 struct DataHeader { short dataLength; short cmd; }; //包体 struct Login:public DataHeader { Login() { dataLength = sizeof(Login); cmd = CMD_Login; } char username[32]; char password[32]; }; struct LoginResult :public DataHeader { LoginResult() { dataLength = sizeof(LoginResult); cmd = CMD_Login_Result; result = 0; } int result; }; struct Logout :public DataHeader { Logout()

网络报文的数据格式定义和使用

◇◆丶佛笑我妖孽 提交于 2019-12-22 03:37:24
服务器端: #include<WinSock2.h> #include<Windows.h> #include<stdio.h> #include<iostream> #pragma comment(lib,"ws2_32.lib") enum CMD { CMD_Login,CMD_Logout,CMD_ERROR }; //包头 struct DataHeader { short dataLength; short cmd; }; //包体 struct Login { char username[32]; char password[32]; }; struct LoginResult { int result; }; struct Logout { char username[32]; }; struct LogoutResult { int result; }; int main() { WORD ver = MAKEWORD(2, 2); WSADATA dat; //WinSocket启动 WSAStartup(ver, &dat); //1、建立一个socket SOCKET _sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //AF_INET创建一个IPV4的套接字,SOCK_STREAM面向数据流的

Does sizeof(T) == sizeof(const T) and alignof(T) == alignof(const T)

◇◆丶佛笑我妖孽 提交于 2019-12-22 01:23:36
问题 It seems reasonable to assume that T and const T would be two types that would be the same size and have the same alignment, but after thinking about some real systems, it seems that they could be different. Let me explain: Suppose you have a system with two types of memory: RAM and Flash (which is read only). The RAM is 8 bit addressable, while the Flash is only 16 bit addressable. Suppose this is T : struct T { uint8_t x; uint16_t y; }; In the byte addressable RAM this struct would be 3

socketServer

房东的猫 提交于 2019-12-21 15:02:50
#include<stdio.h> #include<string.h> #include<sys/types.h> #include<unistd.h> #include<ctype.h> #include<arpa/inet.h> // ================================================= // ================================================= // 宏定义 // ================================================= // ================================================= #define RECE_BUF_LEN 128 // receive buffer length #define SEND_BUF_LEN 128 // send buffer length #define PORT 0xa5a6 // port #define SERVER_INIT_IP "192.168.50.130" #define CLIENT_INIT_IP "192.168.50.129" // ================================================= // ==

Write raw struct contents (bytes) to a file in C. Confused about actual size written

半城伤御伤魂 提交于 2019-12-21 12:42:34
问题 Basic question, but I expected this struct to occupy 13 bytes of space (1 for the char, 12 for the 3 unsigned ints). Instead, sizeof(ESPR_REL_HEADER) gives me 16 bytes. typedef struct { unsigned char version; unsigned int root_node_num; unsigned int node_size; unsigned int node_count; } ESPR_REL_HEADER; What I'm trying to do is initialize this struct with some values and write the data it contains (the raw bytes) to the start of a file, so that when I open this file I later I can reconstruct

C++内存对齐的理解

痞子三分冷 提交于 2019-12-21 11:02:51
程序编译器对结构的存储的特殊处理确实提高CPU存储变量的速度,但是有时候也带来了一些麻烦,我们也屏蔽掉变量默认的对齐方式,自己可以设定变量的对齐方式。 编译器中提供了 #pragma pack(n)来设定变量以n 字节对齐 方式。 n字节对齐就是说变量存放的起始地址的 偏移量 有两种情况: 第一、如果n大于等于该变量所占用的字节数,那么偏移量必须满足默认的对齐方式, 第二、如果n小于该变量的类型所占用的字节数,那么偏移量为n的倍数,不用满足默认的对齐方式。 结构的总大小也有个约束条件,分下面两种情况: 如果n大于所有 成员变量 类型所占用的字节数,那么 结构的总大小必须为占用空间最大的变量占用的空间数的倍数 ; 否则必须为n的倍数。 重要的规则 1,复杂类型中各个成员按照它们被声明的顺序在 内存中顺序存 储,第一个成员的地址和整个类型的地址相同; 2, 每个成员分别对齐,即每个成员按 自己的方式对齐 ,并最小化长 规则就 是每个成员 按其类型的 对齐参数(通常是这个类型的大小) 和 指定对齐参数 中 较小的一个对齐(对齐指的是数据类型在内存中的起始的N倍的跨度) ; (指 复杂类型中成员类型 中自己的对齐方式,并不是结构体等整体的对齐方式) 3, 结构 、 联合 或者 类的数据成员 ,第一个放在偏移为0的地方;以后每个数据成员的对齐,按照#pragma

sizeof与strlen的区别

跟風遠走 提交于 2019-12-20 19:53:04
sizeof是一个关键字不是函数,发生在编译时刻。 sizeof是C/C++中的一个运算符,其作用是返回一个对象或者类型在内存中所占用的字节数。 注意:sizeof后面如果是类型则必须加括号,如 sizeof(char);而如果是变量名则可以不加括号,如 sizeof a; 但是建议使用时 均加上括号。sizeof不能返回动态地被分配的数组的大小。 总结如下,当参数分别如下时,sizeof返回的值表示的含义如下: 数组——编译时分配的数组空间大小; 指针——存储该指针所用的空间大小(在32位系统是4,在64系统是8); 类型——该类型所占的空间大小; 对象——对象的实际占用空间大小; 函数——函数的返回类型所占的空间大小。函数的返回类型不能是void strlen是C语言中的库函数,发生在运行时刻,所在头文件为#include <string.h>其函数原型为unsigned int strlen(char *s); 其中s为指定的字符串。 注意:strlen只能用char *作为参数,它求的是字符串的实际长度,方法是从开始到遇到第一个'\0'结束。 sizeof只关心这块内存的大小,不关心这块内存存放了什么数据, strlen只关心这块内存存放的数据,不关心这块内存的大小,直到遇到第一个NULL为止。 二、几个例子 例1: char str[20] = "0123456789";

VLAs and side-effect in sizeof's operand

霸气de小男生 提交于 2019-12-20 17:29:23
问题 I know that sizeof never evaluates its operand, except in the specific case where said operand is a VLA. Or, I thought I knew. void g(int n) { printf("g(%d)\n", n); } int main(void) { int i = 12; char arr[i]; // VLA (void)sizeof *(g(1), &arr); // Prints "g(1)" (void)sizeof (g(2), arr); // Prints nothing return 0; } What is going on? Just in case, this is compiled with GCC 5.1 on Coliru. 回答1: It seems that I should think twice before posting, because it struck me right after I did. My

C语言面试问题

﹥>﹥吖頭↗ 提交于 2019-12-20 17:11:07
内容源自: C语言面试题大汇总 P.S.只摘取了自己觉得可能会被问到的以及不会的。 static有什么用途?(请至少说明两种) 1.限制变量的作用域2.设置变量的存储域 引用与指针有什么区别? 1) 引用必须被初始化,指针不必。2) 引用初始化以后不能被改变,指针可以改变所指的对象。3) 不存在指向空值的引用,但是存在指向空值的指针。 全局变量和局部变量在内存中是否有区别?如果有,是什么区别? 全局变量储存在静态数据库,局部变量在堆栈 不能做switch()的参数类型是:switch的参数不能为实型。 局部变量能否和全局变量重名? 局部变量可以与全局变量同名,在函数内引用这个变量时,会用到同名的局部变量,而不会用到全局变量。对于有些编译器而言,在同一个函数内可以定义多个同名的局部变量,比如在两个循环体内都定义一个同名的局部变量,而那个局部变量的作用域就在那个循环体内 如何引用一个已经定义过的全局变量? extern 可以用引用头文件的方式,也可以用extern关键字,如果用引用头文件方式来引用某个在头文件中声明的全局变理,假定你将那个变写错了,那么在编译期间会报错,如果你用extern方式引用时,假定你犯了同样的错误,那么在编译期间不会报错,而在连接期间报错 全局变量可不可以定义在可被多个.C文件包含的头文件中?为什么? 可以,在不同的C文件中以static形式来声明同名全局变量。

Is C++ allowed to increase the derived class size if there're no new member variables compared to the base class?

倖福魔咒の 提交于 2019-12-20 10:26:15
问题 Suppose I have a base class with some member variables and no virtual functions: class Base { int member; }; and a derived class that derives in a non-virtual way from Base and has no new member variables an again no virtual functions: class Derived : Base { }; Obviously sizeof(Derived) can't be smaller than sizeof(Base) . Is sizeof(Derived) required to be equal to sizeof(Base) ? 回答1: From 5.3.2 [expr.sizeof] When applied to a class, the result [of sizeof ] is the number of bytes in an object