sizeof

socket编程的select模型

荒凉一梦 提交于 2019-11-28 22:14:15
原文链接: http://www.cnblogs.com/RascallySnake/p/3185071.html 在掌握了socket相关的一些函数后,套接字编程还是比较简单的,日常工作中碰到很多的问题就是客户端/服务器模型中,如何让服务端在同一时间高效的处理多个客户端的连接,我们的处理办法可能会是在服务端不停的监听客户端的请求,有新的请求到达时,开辟一个新的线程去和该客户端进行后续处理,但是这样针对每一个客户端都需要去开辟一个新的线程,效率必定底下。 其实,socket编程提供了很多的模型来处理这种情形,我们只要按照模型去实现我们的代码就可以解决这个问题。主要有select模型和重叠I/o模型,以及完成端口模型。这次,我们主要介绍下select模型,该模型又分为普通select模型,wsaasyncselect模型,wsaeventselect模型。我们将通过样例代码的方式逐一介绍。 一、select模型 使用该模型时,在服务端我们可以开辟两个线程,一个线程用来监听客户端的连接 请求,另一个用来处理客户端的请求。主要用到的函数为select函数。如: 全局变量: fd_set g_fdClientSock; 线程1处理函数: SOCKET listenSock = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP ); sockaddr_in

How can this structure have sizeof == 0?

时光怂恿深爱的人放手 提交于 2019-11-28 21:49:46
问题 There is an old post asking for a construct for which sizeof would return 0 . There are some high score answers from high reputation users saying that by the standard no type or variable can have sizeof 0. And I agree 100% with that. However there is this new answer which presents this solution: struct ZeroMemory { int *a[0]; }; I was just about to down-vote and comment on it, but time spent here taught me to check even the things that I am 100% sure on. So... to my surprise both gcc and

size guarantee for integral/arithmetic types in C and C++

◇◆丶佛笑我妖孽 提交于 2019-11-28 21:42:31
I know that the C++ standard explicitly guarantees the size of only char , signed char and unsigned char . Also it gives guarantees that, say, short is at least as big as char , int as big as short etc. But no explicit guarantees about absolute value of, say, sizeof(int) . This was the info in my head and I lived happily with it. Some time ago, however, I came across a comment in SO (can't find it) that in C long is guaranteed to be at least 4 bytes, and that requirement is "inherited" by C++. Is that the case? If so, what other implicit guarantees do we have for the sizes of arithmetic types

Is sizeof(void()) a legal expression?

ぐ巨炮叔叔 提交于 2019-11-28 21:01:22
From [5.3.3/1] , I found that: The sizeof operator shall not be applied to an expression that has function or incomplete type From [3.9/5] I found that: Incompletely-defined object types and cv void are incomplete types Anyway, for sizeof does not evaluate it's operands, I would have said that sizeof(void()) was a legal expression (actually GCC compiles it and the result is 1). On the other side, from here , void is not mentioned while discussing sizeof , neither when the types having size 1 are mentioned, nor in the list of the ones having an implementation defined size. The question is thus:

剑指offer47:位运算+递归。求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。

自闭症网瘾萝莉.ら 提交于 2019-11-28 19:44:23
1 题目描述   求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。 2 思路和方法   (1)递归,不能使用if等条件判断语句,可以使用&&逻辑运算符的短路特性实现。 当n=0时,不进行后一个判断的计算,作为递归终止 。   (2)利用sizeof(a)计算bool数组的字节数,bool类型在C++中占一个字节。bool a = [n][n+1]; 因一共有n*(n+1)个1,下三角或者上三角,第一行:[1]和为1;第二行:[1][1] 和为2;第三行:[1][1][1]和为3,……。bool类型的数据a,sizeof(a)=n*(n+1),所以1+2+3+...+n=sizeof(a)=n*(n+1)/2,或者是sizeof(a)>>1。 3 C++核心代码 1 #include<iostream> 2 3 #include<vector> 4 5 using namespace std; 6 7 class Solution { 8 public: 9 int Sum_Solution(int n) { 10 int sum = n; 11 sum && (sum += Sum_Solution(n - 1));// 利用前一个判断短路;当n=0时,不进行后一个判断的计算,作为递归终止

剑指offer

时间秒杀一切 提交于 2019-11-28 19:42:49
// 面试题23:链表中环的入口结点 // 题目:一个链表中包含环,如何找出环的入口结点?例如,在图3.8的链表中, // 环的入口结点是结点3。 ListNode* FindFirstNode(ListNode* HeadNode) { if(!HeadNode) return nullptr; ListNode* FastNode = HeadNode; ListNode* SlowNode = HeadNode; while(FastNode&(FastNode->NextPoint)) { SlowNode = SlowNode->NextPoint; FastNode = SlowNode->NextPoint; if(FastNode == SlowNode) { SlowNode = HeadNode; while(FastNode != SlowNode) { FastNode = FastNode->NextPoint; SlowNode = SlowNode->NextPoint; } return SlowNode; } } return nullptr; } //找到链表中环形链表的入口点 ListNode* FindTheFirstNode(ListNode* Node) { if(Node ==nullptr) return nullptr;

What's the difference between sizeof and alignof?

断了今生、忘了曾经 提交于 2019-11-28 18:27:16
问题 What's the difference between sizeof and alignof? #include <iostream> #define SIZEOF_ALIGNOF(T) std::cout<< sizeof(T) << '/' << alignof(T) << std::endl int main(int, char**) { SIZEOF_ALIGNOF(unsigned char); SIZEOF_ALIGNOF(char); SIZEOF_ALIGNOF(unsigned short int); SIZEOF_ALIGNOF(short int); SIZEOF_ALIGNOF(unsigned int); SIZEOF_ALIGNOF(int); SIZEOF_ALIGNOF(float); SIZEOF_ALIGNOF(unsigned long int); SIZEOF_ALIGNOF(long int); SIZEOF_ALIGNOF(unsigned long long int); SIZEOF_ALIGNOF(long long int);

内存对齐

你。 提交于 2019-11-28 18:02:24
1,结构体中内存的对齐 #include "pch.h" #include <iostream> struct Test { int a; // 4 short b; // 2 char c; // 1 double d; // 8 }; int main() { Test t; t.a = 100; t.b = 200; t.c = 'c'; t.d = 10.11; printf("sizeof(Test) = %d\n",sizeof(t)); std::cout << "Hello World!\n"; } 将结构体中数据位置换一下: #include "pch.h" #include <iostream> struct Test { int a; // 4 double d; // 8 short b; // 2 char c; // 1 }; int main() { Test t; t.a = 100; t.b = 200; t.c = 'c'; t.d = 10.11; printf("sizeof(Test) = %d\n",sizeof(t)); std::cout << "Hello World!\n"; } 2,结构体中含有结构体的内存对齐 3,含有位域的内存对齐    每个特定平台上的编译器都有自己的默认“对齐系数”(也叫对齐模数)。

sizeof empty structure is 0 in C and 1 in C++ why? [duplicate]

我的梦境 提交于 2019-11-28 17:13:52
Possible Duplicates: Empty class in C++ What is the size of an empty struct in C? I read somewhere that size of an empty struct in C++ is 1. So I thought of verifying it. Unfortunately I saved it as a C file and used <stdio.h> header and I was surprised to see the output. It was 0. That means struct Empty { }; int main(void) { printf("%d",(int)sizeof(Empty)); } was printing 0 when compiled as a C file and 1 when compiled as a C++ file. I want to know the reason. I read that sizeof empty struct in c++ is not zero because if the size were 0 two objects of the class would have the same address

Size of int and sizeof int pointer on a 64 bit machine

会有一股神秘感。 提交于 2019-11-28 16:29:54
问题 I was just wondering how can I know if my laptop is 64 or 32 bit machine. (it is a 64). So, I thought about printing the following: int main() { printf("%d",sizeof(int)); } and the result was 4, which seemed weird (since it is a 64 bit machine) But, when I printed this: int main() { printf("%d",sizeof(int*)); } the result was 8, which made more sense. The question is: Since I'm using a 64 bit machine, shouldn't a primitive type such as int should use 8 bytes (64 bit) and by that sizeof int