memset

malloc,calloc与free函数

做~自己de王妃 提交于 2019-12-04 20:37:26
内存区域可以分为栈、堆、静态存储区和常量存储区,局部变量,函数形参,临时变量都是在栈上获得内存的,它们获取的方式都是由编译器自动执行的。 利用指针,我们可以像汇编语言一样处理内存地址,C 标准函数库提供了许多函数来实现对堆上内存管理,其中包括:malloc函数,calloc函数,free函数。使用这些函数需要包含头文件stdlib.h。 四个函数之间的有区别,也有联系,我们应该学会把握这种关系,从而编出精炼而高效的程序。 在说明它们具体含义之前,先简单从字面上加以认识,前2个函数有个共同的特点,就是都带有字符”alloc”,就是”allocate”,”分配”的意思,也就是给对象分配足够的内存,” calloc()”是”分配内存给多个对象”,” malloc()”是”分配内存给一个对象”,”free()”就比较简单了,”释放”的意思,就是把之前所分配的内存空间给释放出来。 void *calloc(size_t nobj, size_t size); 分配足够的内存给nobj个大小为size的对象组成的数组, 并返回指向所分配区域的第一个字节的指针; 若内存不够,则返回NULL. 该空间的初始化大小为0字节. char *p = (char *) calloc(100,sizeof(char)); void *malloc(size_t size);

概率专题·期望

浪尽此生 提交于 2019-12-04 13:57:19
uva 11021 Tribbles UVA 11722 Joining with Friend UVA 11427 Expect the Expected UVA 11762 Race to 1 UVA 10491 So you want to be a 2n-aire UVA 11346 Probability UVA 11637 Garbage Remembering Exam UVA 11605 Lights inside a 3d Grid UVA 10491 Cows and Cars UVA 1413 Game UVA 1498 Activation uva 11021 Tribbles k只麻球,每活一天就会死亡,但第二天可能会生一些麻球,具体说来,生i个麻球的概率为pi ,求m天后所有麻球都死亡的概率 f i = p 0 + p 1 f i − 1 + p 2 f i − 2 2 + . . . + p n − 1 f i − 1 n − 1 //--> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<functional> #include<iostream> #include<cmath> #include<cctype> #include

CSP复习与模板

自古美人都是妖i 提交于 2019-12-04 13:45:25
P3366 【模板】最小生成树 Kruskal 算法因为只与边相关,则适合求稀疏图的最小生成树。而 Prim 算法因为只与顶点有关,所以适合求稠密图的最小生成树。 Prim 是以更新过的节点的连边找最小值,Kruskal 是直接将边排序。两者其实都是运用贪心的思路。 Kruskal Kruskal 的时间复杂度为 \(O(e\log e)\) ,只和边有关系。 #include<cstdio> #include<algorithm> #define reg register using namespace std; const int N=5005,M=200005; struct Edge{ int u,v,w; bool operator<(const Edge x){ return w<x.w; } }e[M]; int fa[N],ans,n,m,tot; int father(int x){ return fa[x]==x?x:fa[x]=father(fa[x]); } int main(){ scanf("%d%d",&n,&m); for(reg int i=1;i<=n;++i)fa[i]=i; for(reg int i=1;i<=m;++i) scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w); sort(e+1,e+m+1);

memset的常见用法

流过昼夜 提交于 2019-12-04 13:28:30
头文件 <cstring> 描述 因为memset函数按照字节填充,所以一般memset只能用来填充char型数组 ———————————————————————————————————— 但是,我们一般都用memset来初始化int型的数组,所有就要有一些特殊情况 ——————————————————————————————————————— 常用用法 初始化为0 memset(a,0,sizeof(a)); 初始化为-1 memset(a,-1,sizeof(a)); 3。 初始化为MAX define MAX 0x3f3f3f3f //当心,一共有4个3f memset(a,0x3f,sizeof(a)); 这样a数组里面的全部元素,就定义成了0x3f3f3f3f(i.e=1061109567) //补充一下,0x3f3f3f3f是一个很好用的数字,大概为1e9,算一个非常大的数字,并且两个数加起来也没有超过int的范围,所以就不会出现超出范围,就更加鲁棒。 这里解释原因 因为memset是按字节操作的,而0x3f3f3f3f的每一个字节都是0x3f,所以就成立啦! 来源: https://www.cnblogs.com/battlin/p/11867481.html

割边模板

南楼画角 提交于 2019-12-04 09:24:45
判断方面,没有了割点的root的特判,并且==号去掉 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 const int maxn=1e3+10; 7 8 int dfn[maxn],low[maxn],head[maxn],vis[maxn]; 9 bool judge[maxn]; 10 int k,n,m,num,root; 11 struct Edge{ 12 int to,next; 13 }G[400010]; 14 struct node 15 { 16 int u,v; 17 }sto[maxn]; int cot; 18 //这个大小应该跟边数同步,但这里跟点的个数同步也能过; 19 //大概是因为割边的数量不多吧 20 void build(int u,int v){ 21 G[num].to=v;G[num].next=head[u];head[u]=num++; 22 } 23 24 void Tarjan(int u,int fa){ 25 int son=0; 26 vis[u]=1; //判断是否访问过,当访问完后定为2,不再访问; 27 k++; 28 dfn[u]=k; /

Fastest way to zero out a 2d array in C?

霸气de小男生 提交于 2019-12-04 07:23:09
问题 I want to repeatedly zero a large 2d array in C. This is what I do at the moment: // Array of size n * m, where n may not equal m for(j = 0; j < n; j++) { for(i = 0; i < m; i++) { array[i][j] = 0; } } I've tried using memset: memset(array, 0, sizeof(array)) But this only works for 1D arrays. When I printf the contents of the 2D array, the first row is zeroes, but then I got a load of random large numbers and it crashes. 回答1: memset(array, 0, sizeof(array[0][0]) * m * n); Where m and n are the

POJ1274-The Perfect Stall

微笑、不失礼 提交于 2019-12-04 06:44:43
啊就是一道匈牙利裸题,然而谁能想到我因为scanf在这题上wa了n次呢(我真是。。) #include<cstdio> #include<cstring> #define maxn 205 #define maxm 40005 using namespace std; int num,last[maxn],to[maxm],nxt[maxm],n,m,used[maxn],match[maxn],cnt; int dfs(int x) { for (int i=last[x];i;i=nxt[i]) { if (!used[to[i]]) { used[to[i]]=1; if (!match[to[i]] || dfs(match[to[i]])) { match[to[i]]=x; return 1; } } } return 0; } void add(int x,int y) { to[++num]=y;nxt[num]=last[x];last[x]=num; } int main() { int i,a,x; while (scanf("%d%d",&n,&m)!=EOF) { memset(last,0,sizeof(last)); memset(nxt,0,sizeof(nxt)); memset(to,0,sizeof(to)); memset(match,0

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 {

Memset not working

元气小坏坏 提交于 2019-12-04 06:12:28
I am trying to use memset on a pure 2D Array, using the following piece of code : #include <iostream> #include <cstring> using namespace std; int main() { int l[3][3]; memset (l, 1, sizeof(l)); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { cout << l[i][j] << " "; } cout << endl; } return 0; } I want the whole array to be initialized by 1 using the line : memset (l, 1, sizeof(l)); But I don't get the expected value, it gives me the following output: 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 16843009 Thought it might be a compiler problem, so I tried

memset in parallel with threads bound to each physical core

别等时光非礼了梦想. 提交于 2019-12-04 04:48:56
I have been testing the code at In an OpenMP parallel code, would there be any benefit for memset to be run in parallel? and I'm observing something unexpected. My system is a single socket Xeon E5-1620 which is an Ivy Bridge processor with 4 physical cores and eight hyper-threads. I'm using Ubuntu 14.04 LTS, Linux Kernel 3.13, GCC 4.9.0, and EGLIBC 2.19. I compile with gcc -fopenmp -O3 mem.c When I run the code in the link it defaults to eight threads and gives Touch: 11830.448 MB/s Rewrite: 18133.428 MB/s However, when I bind the threads and set the number of threads to the number of