memset

Is it guaranteed that memset will zero out the padding bits in a structure?

元气小坏坏 提交于 2019-12-04 03:22:57
In general ,as per C standard is it guaranteed that memset() with 0 will zero out the padding bits in a C structure? What about gcc? For example , something like: struct MyStruct { unsigned char member1; unsigned int member2; char member3; unsigned char member4; float member5; }; struct MyStruct ms; memset(&ms, 0, sizeof( struct MyStruct)); Perhaps worth noting that memset doesn't know anything about your struct (or array, or primitive, or any chunk of memory whatsoever that you happen to unleash it on), so even if it wanted to leave the padding bits intact, it wouldn't even know where they

洛谷P1979华容道

爱⌒轻易说出口 提交于 2019-12-03 22:39:54
题目 此题目中存在三种棋盘的放置方法(空白,不能活动,能活动)。 而每次变化的格子一定在当前空白格子的周围,因此只需要对空白格子的周围四个状态考虑即可,因此我们设 \(a[i][j][k]\) 为白格子在(i,j)的k方向的一个状态,然后我们考虑,如果活动和不能活动的格子已经确定了,那么如果按照暴力的解法每次询问都需要对一开始的白格子向外扩展而得到的,这样会重复计算,因此我们可以快速计算出所有可移动的格子向周围移动所需要的时间,用 \(dis[i][j][k][h]\) 表示空格子在 \((i,j)\) 的 \(k\) 方向,然后 \((i,j)\) 跳到 \((i+dx[h], j+dx[h])\) 所需要的时间。这样就比较使状态表现的完全了,因为每次从一个格子移动到令一个格子时,就不需要用搜索来更新了,直接用此时间更新。 再向外扩展一下,则将状态抽象到图上的点,则原题就转化成了从起点到终点状态的最短路了。 对于每个询问,终点状态都有最多4个: \(a[tx][ty][0/1/2/3]\) 里面的任何满足条件且用时最短的的一个。 起点状态有4个,分别是 \(a[sx][sy][0/1/2/3]\) ,但是此时白格子并没有到这四个方向上,因此我们用s表示一个不存在的节点,然后把这个节点跟起点的四个状态连边,边权分别是起始白格子位置到这四个方向的位置所用的时间。然后跑最短路就可以了

题解 SP2878 【KNIGHTS - Knights of the Round Table】

老子叫甜甜 提交于 2019-12-03 14:27:14
题目链接 Solution KNIGHTS 题目大意:有 \(n\) 个骑士,其中有 \(m\) 对骑士相互憎恨。现在要排出若干个大小至少为 \(3\) 且为奇数的环,相互憎恨的骑士不能够相邻,问有多少个骑士不可能排入任何一个环 二分图,点双联通分量 分析:我们把有憎恨关系的连上边,这样相邻骑士之间必须没有连边,这样不好考虑,我们可以建出原图的补图,这样相邻骑士之间就必须有连边了。 问题变成了有多少个点没有被任何一个奇数长度的环包含 显而易见,如果一个点双联通分量内存在奇数长度的环,那么该点双联通分量里面所有的点都至少被一个奇数长度的环包含,不然与点双联通分量这个前提相矛盾 因此我们只需要看每一个点双联通分量里面有没有奇数长度的环就可以了,没有奇数长度的环等价于图是二分图,因此我们跑一次二分图染色就可以了 #include <cstdio> #include <cstring> #include <vector> #include <stack> #include <queue> using namespace std; const int maxn = 1024; vector<int> G[maxn],dcc[maxn]; inline void addedge(int from,int to){G[from].push_back(to);} int tmp[maxn]

有关nginx中Strings模块中ngx_explicit_memzero()函数的问题

别说谁变了你拦得住时间么 提交于 2019-12-03 14:16:21
1.背景 在nginx的Development guide中介绍Strings模块时,提到ngx_explicit_memzero()函数可以消除编译器的dead store elimination optimization(死区消除优化策略),使得编译器不会消除这个函数的调用。 2.ngx_explicit_memzero()函数的实现 从源码中我们可以看到该函数使用了两部分: memset(buf,0,n); __asm__ volatile("" ::: "memory"); 想必memet我们已经很清除,__asm__我们可以看出是一个内嵌的汇编语句,那么这么一段语句是如何防止编译器的dead store elimination optimization呢? 3.dead store elimination optimization简介: 首先我们要了解死区消除优化策略是如何由编译器决策的? 引用 http://gcc.gnu.org/news/dse.html 中的一段内容: GCC now tracks loads and stores more accurately within each basic block. GCC also uses alias analysis to detect when the dead store candidates can not

Linux 13网络服务器与客户端ser,cli

自古美人都是妖i 提交于 2019-12-03 12:25:29
1.网络编程 1.1基本概念 目的: 实现进程间的通信 网络: 把多个主机连接起来,构成一个网络, 互联网: 把网络和网络 连接起来就构成了互联网。 ip: 在网络中唯一标示一台主机 端口: 在某个主机上唯一标示一个进程。 1.2网络模型 2.tcp编程流程 3. tcp实现 3.1tcp ser.c #include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<string.h> #include<assert.h> #include<sys/socket.h> #include<netinet/in.h> #include<arpa/inet.h> int main() { int sockfd=socket(AF_INET,SOCK_STREAM,0); assert(sockfd!=-1); struct sockaddr_in saddr,caddr; memset(&saddr,0,sizeof(saddr)); saddr.sin_family=AF_INET; saddr.sin_port=htons(6000); saddr.sin_addr.s_addr=inet_addr("127.0.0.1"); int res=bind(sockfd,(struct sockaddr*)&saddr

[BZOJ3277/BZOJ3473] 串 - 后缀数组,二分,双指针,ST表,均摊分析

泄露秘密 提交于 2019-12-03 10:52:51
[BZOJ3277] 串 Description 现在给定你n个字符串,询问每个字符串有多少子串(不包括空串)是所有n个字符串中至少k个字符串的子串(注意包括本身)。 Solution 首先将所有串连接起来,预处理出后缀数组和高度数组。 显然直接主席树可以很容易做到 \(O(n \log^2 n)\) 。对于每一个后缀的位置,二分一个 LCP 长度,找到这个 LCP 长度对应的区间,检查这个区间是否合法来调节二分边界。 注意在这个做法里,瓶颈不在于主席树,因为主席树的功能完全可以用双指针预处理一个数组来替代。瓶颈在于,实质上使用了一个二分套二分的做法。 但我们有更好的做法。 引理:按照原始顺序,如果第 \(i\) 个后缀有 \(x\) 个前缀能被 \(k\) 个串包含,那么第 \(i+1\) 个后缀至少有 \(x-1\) 个前缀能被 \(k\) 个串包含。 那么我们先用双指针预处理 \(jmp[i]\) 代表按照后缀排序,最大的 \(j\) 使得 \([j,i]\) 这个后缀区间合法。 到第 \(i\) 个后缀的时候我们就从后缀 \(i-1\) 的答案开始向上枚举,用二分+ST表找出它左右边第一个高度比当前枚举值小的位置,判断这个区间的合法性来决定是否继续枚举,均摊时间复杂度 \(O(nlogn)\) 。 \(O(\log n)\) 解法 #include <bits/stdc++

What's the use of memset() return value?

岁酱吖の 提交于 2019-12-03 10:21:03
问题 memset() is declared to return void* that is always the same value as the address passed into the function. What's the use of the return value? Why does it not return void ? 回答1: The signature is in line with all the other similar functions: memcpy() , strcpy() etc. I always thought this was done to enable one to chain calls to such functions, and to otherwise use such calls in expressions. That said, I've never come across a real-world situation where I would feel compelled to use the return

区间dp提升复习

跟風遠走 提交于 2019-12-03 09:55:32
区间 \(dp\) 提升复习 不得不说这波题真的不简单。。。 技巧总结: 1.有时候转移可以利用背包累和 2.如果遇到类似区间添加限制的题可以直接把限制扔在区间上,每次只考虑 \([l,r]\) 被 \([i,j]\) 完全包含的情况 [BZOJ4897] [Thu Summer Camp2016] 成绩单 典型的利用背包转移, \(dp[i][j]\) 表示处理完这段 \([i,j]\) 区间的答案 转移时一段区间可以被先处理掉或者直接从已经处理完的 \(dp[i][j]\) 中取过来,这个可以用一个背包维护 const int N=52,P=999983,INF=1e9; int n,a,b,w[N]; int dp[N][N]; int tmp[N][N][N]; int id[N*N]; inline void cmin(int &a,int b){ ((a>b)&&(a=b)); } inline void cmax(int &a,int b){ ((a<b)&&(a=b)); } int main(){ n=rd(),a=rd(),b=rd(); w[0]=0,w[n+1]=1001; rep(i,1,n) w[i]=rd(); rep(i,0,n+1) id[w[i]]=i; memset(dp,63,sizeof dp); rep(i,1,n) { int ma=0

using memset() in double type array

匿名 (未验证) 提交于 2019-12-03 09:02:45
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I want to set all the index value to -1 in a double array. Here is my code : double dp[505]; memset(dp,-1,sizeof(dp)); cout<<dp[0]<<"\n"; But it is showing nan when i try to print its value. What does nan mean? Is it possible to use memset() in double array? 回答1: In C++, you can write: double initValue = -1; std::fill_n(dp, 505, initValue); memsetting a double array with a non-double value won't work. 回答2: memset operates on bytes, not floats, and a double with all bytes set to -1 does not equal -1. I think you're looking for std::fill :

Why does “memset(arr, -1, sizeof(arr)/sizeof(int))” not clear an integer array to -1?

匿名 (未验证) 提交于 2019-12-03 08:52:47
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Is it not possible to use memset on an array of integers? I tried the following memset call and didn't get the correct integer values in the int array. int arr[5]; memset (arr, -1, sizeof(arr)/sizeof(int)); Vaules I got are: arr[0] = -1 arr[1] = 255 arr[2] = 0 arr[3] = 0 arr[4] = 0 回答1: Just change to memset (arr, -1, sizeof(arr)); Note that for other values than 0 and -1 this would not work since memset sets the byte values for the block of memory that starts at the variable indicated by *ptr for the following num bytes. void * memset (