sizeof

二分匹配模板

 ̄綄美尐妖づ 提交于 2019-11-27 21:09:36
1 #include <cstdio> 2 #include <cstring> 3 #include <queue> 4 #include <vector> 5 #include <algorithm> 6 using namespace std; 7 /****************************************************** 8 * 二分图匹配(匈牙利算法得邻接矩阵+dfs实现) 9 * 初始化:g[][]两边顶点的划分情况 10 * 建立g[i][j]表示i->j的有向边就可以了,是左边向右边的匹配 11 * g 没有边相连则初始化为0 12 * //un是匹配左边的顶点数,vn是匹配右边的顶点数 13 * 调用:res = hungray():输出最大匹配 14 * 优点:适用于稠密图,dfs找增广路,实现简洁易于理解 15 * 时间复杂度:O(V,E)。 16 * 顶点编号从0开始 17 const int maxn = 500 + 5; 18 int un, vn;//二分图中u, v的数目,使用前必须赋值 19 int g[maxn][maxn]; 20 int linker[maxn];//每个结点的匹配结点 21 bool used[maxn]; 22 23 bool dfs(int u) { 24 for(int v = 0;

How do I find the size of a struct? [closed]

核能气质少年 提交于 2019-11-27 20:27:49
struct a { char *c; char b; }; What is sizeof(a)? #include <stdio.h> typedef struct { char* c; char b; } a; int main() { printf("sizeof(a) == %d", sizeof(a)); } I get "sizeof(a) == 8", on a 32-bit machine. The total size of the structure will depend on the packing: In my case, the default packing is 4, so 'c' takes 4 bytes, 'b' takes one byte, leaving 3 padding bytes to bring it to the next multiple of 4: 8. If you want to alter this packing, most compilers have a way to alter it, for example, on MSVC: #pragma pack(1) typedef struct { char* c; char b; } a; gives sizeof(a) == 5. If you do this,

字符数组和字符串数组

醉酒当歌 提交于 2019-11-27 20:12:21
demo int _tmain(int argc, _TCHAR* argv[]) { char a[5] = {1,2,3,4,5};  //字符数组 char b[5] = {1,2,3,4,'\0'};  //字符数组,也可以叫字符串数组;a[5]不可以叫字符串数组 char c[5] = "test";  //字符数组,也可以叫字符串数组,a[5]不可以叫字符串数组 int arry[5] = {10000,2147483647,2147483647,2147483647,2147483647};  //整型数组 getchar(); return 0; } - a 0x00cffc70 "烫烫烫烫烫蘆" char [5] [0] 1 '' char [1] 2 '' char [2] 3 '' char [3] 4 '' char [4] 5 '' char strlen(a) 18 int sizeof(a) 5 unsigned int - b 0x00cffc60 "" char [5] [0] 1 '' char [1] 2 '' char [2] 3 '' char [3] 4 '' char [4] 0 char strlen(b) 4 int sizeof(b) 5 unsigned int - c 0x00cffc50 "test" char [5] [0

语法规则

放肆的年华 提交于 2019-11-27 19:26:01
一、与if相关的 1.if(cond == true) {...}可以换成if(cond != false) {...},因为错误出现的情况更少 2.if(cond == value1) {...} if(cond == value2) {...},如果出现的情况很多,可以设置成宏,例: #if cond == value1 ..... #endif 3.如果有多个if(condx){}并列,换成if~else形式 4.如果if~else if过多,则换成switch~case形式 二、与变量定义相关 1.对于boolean flag,不要用byte\short\int等,用一个位bit就可以 2.对const变量使用const关键字,变量会被保存在ROM中,节省RAM 3.用sizeof关键字去获取数组的大小,不要直接定义数组大小 const RxMsg_t RxMsg[] = { R_POWER, C_GET_VERSION, R_GET_VERSION, R_REJECT } #define NB_ELEMENT (sizeof(RxMsg) / sizeof(RxMsg[0])); 4.各个模块不能直接使用全局变量,要通过Get或Set接口去访问 三、与运算方式相关 1.result = value / 4 改成 result = value >> 2 2.result =

How to get the string size in bytes?

心不动则不痛 提交于 2019-11-27 19:23:46
As the title implies, my question is how to get the size of a string in C . Is it good to use sizeof if I've declared it (the string) in a function without malloc in it? Or, if I've declared it as a pointer? What if I initialized it with malloc ? I would like to have an exhaustive response. nmikhailov You can use strlen . Size is determined by the terminating null-character, so passed string should be valid. If you want to get size of memory buffer, that contains your string, and you have pointer to it: If it is dynamic array(created with malloc), it is impossible to get it size, since

How can I find the number of elements in an array?

独自空忆成欢 提交于 2019-11-27 19:15:44
I have an int array and I need to find the number of elements in it. I know it has something to do with sizeof but I'm not sure how to use it exactly. If you have your array in scope you can use sizeof to determine its size in bytes and use the division to calculate the number of elements: #define NUM_OF_ELEMS 10 int arr[NUM_OF_ELEMS]; size_t NumberOfElements = sizeof(arr)/sizeof(arr[0]); If you receive an array as a function argument or allocate an array in heap you can not determine its size using the sizeof . You'll have to store/pass the size information somehow to be able to use it: void

同一端口监听tcp和udp请求

放肆的年华 提交于 2019-11-27 19:06:05
问题: 众所周知,同一台机器的同一个端口只可以被一个进程使用,一般用于tcp,或者udp。那一个进程使用同一个端口同时监听tcp、udp请求,是否可以呢?答案:可以。 代码: server 为了同时监听,server使用select进行多路访问控制。 server端代码如下: /* TCP INET use select */ #include<stdio.h> #include<stdlib.h> #include<string.h> #include<errno.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #define LENGTH_OF_LISTEN_QUEUE 20 #define SERVER_PORT 8888 #define MAXLINE 4096 #define MAX_FD_NUM 10 static int init_new_client( int client_fd); static int remove_client( int client_fd); static int get_max_fd( int fd); static int client_fdset[MAX_FD_NUM]; int main( int argc, char ** argv) {

How to specify 64 bit integers in c

若如初见. 提交于 2019-11-27 18:42:05
I'm trying to use 64 bit integers in C, but am getting mixed signals as to whether it should be possible. When I execute the printf: printf("Size of long int:%d\nSize of long long int:%d\n\n",(int)sizeof(long int), (int)sizeof(long long int)); The response I get is: Size of long int:4 Size of long long int:8 This makes me feel that a long long int has 8 bytes = 64 bits. However, when I try to declare the following variables: long long int a2 = 0x00004444; long long int b2 = 0x000044440; long long int c2 = 0x0000444400; long long int d2 = 0x00004444000; long long int e2 = 0x000044440000; long

TTCP程序源码剖析

回眸只為那壹抹淺笑 提交于 2019-11-27 18:37:22
1、ttcp作用:检测TCP吞吐量 测试的数据是每秒传输的字节数 带宽 Mb/s 测试程序的性能指标: 传输带宽,QPS/TPS, 以及 CPU利用率,延迟等等。 2、ttcp应用层协议: 3. 尝试自己用C语言写出简单的TTCP程序 先发送一个SessionMessage包中number表示消息的条数,length表示每条消息的长度 PayloadMessage包中存放数据的长度和数据 服务端接收函数 void receive(const Options& opt) { int sockfd = acceptOrDie(opt.port); struct SessionMessage sessionMessage = { 0, 0 }; if (read_n(sockfd, &sessionMessage, sizeof(sessionMessage)) != sizeof(sessionMessage)) { perror("read SessionMessage"); exit(1); } sessionMessage.number = ntohl(sessionMessage.number); sessionMessage.length = ntohl(sessionMessage.length); printf("receive number = %d\nreceive

implementation of sizeof operator

时光总嘲笑我的痴心妄想 提交于 2019-11-27 18:23:18
I have tried implementing the sizeof operator.. I have done in this way.. #define my_sizeof(x) ((&x + 1) - &x) But it always ended up in giving the result as '1' for either of the data type.. I have then googled it for this.. and i found the code typecasted #define my_size(x) ((char *)(&x + 1) - (char *)&x) And the code is working if it is typecasted.. I dont understand why.. This code is also PADDING a STRUCTURE perfectly.. It is also working for #define my_sizeof(x) (unsigned int)(&x + 1) - (unsigned int)(&x) Can anyone please explain how is it working if typecasted and if not typecasted?