memset

Initializing entire array with memset

三世轮回 提交于 2019-12-02 00:19:42
问题 I have initialised the entire array with value 1 but the output is showing some garbage value. But this program works correctly if i use 0 or -1 in place of 1. So are there some restrictions on what type of values can be initialised using memset. int main(){ int a[100]; memset(a,1,sizeof(a)); cout<<a[5]<<endl; return 0; } 回答1: memset, as the other say, sets every byte of the array at the specified value. The reason this works with 0 and -1 is because both use the same repeating pattern on

memset函数的理解

老子叫甜甜 提交于 2019-12-02 00:07:43
void val_init(void) { unsigned int i,len; len = sizeof(GF_cs) / sizeof(GF_cs[0]);//计算GF_cs数组的长度,注GF_cs为全局变量,定义为:unsigned int GF_cs[33]; //--------memset函数清理内存 vs for循环清理内存 //memset:3496.25us - 3390.50us = 105.75us = 423个指令(4Mhz) // for:3390.50us - 3231.75us = 158.75us = 635个指令(4Mhz) NOP();//测算时间-----3231.75us for(i = 0;i < len;i++) GF_cs[i] = 0; NOP();//测算时间-----3390.50us memset(GF_cs,0,sizeof(GF_cs));//将GF_cs指向的地址为开始,到GF_cs数组长度内的内存以0填充 NOP();//测算时间-----3496.25us } 来源: https://www.cnblogs.com/inlod/p/11722418.html

What is/are the fastest memset() alternatives for OpenCL?

跟風遠走 提交于 2019-12-01 22:29:16
问题 I'm using OpenCL, and I need to memset() some array in global device memory. CUDA has a memset() -like API function, but OpenCL does not. I read this, where I found two possible alternatives: using memset() on the host with some scratch buffer, then clEnqueueWriteBuffer() to copy that to the buffer on the device. Enqueueing (sp?) the following kernel: __kernel void memset_uint4(__global uint4* mem,__private uint4 val) { mem[get_global_id(0)]=val; } Which is better? Or rather, under which

What is/are the fastest memset() alternatives for OpenCL?

情到浓时终转凉″ 提交于 2019-12-01 21:39:20
I'm using OpenCL, and I need to memset() some array in global device memory. CUDA has a memset() -like API function, but OpenCL does not. I read this , where I found two possible alternatives: using memset() on the host with some scratch buffer, then clEnqueueWriteBuffer() to copy that to the buffer on the device. Enqueueing (sp?) the following kernel: __kernel void memset_uint4(__global uint4* mem,__private uint4 val) { mem[get_global_id(0)]=val; } Which is better? Or rather, under which circumstances/for which platforms is one better than the other? Note: If the special case of zero'ing

$NOIp$做题记录

假如想象 提交于 2019-12-01 20:25:39
虽然去年做了挺多了也写了篇一句话题解了但一年过去也忘得差不多了$kk$ 所以重新来整理下$kk$ $2018$ [X] 积木大赛 大概讲下$O(n)$的数学方法. 我是从分治类比来的$QwQ$.考虑对每个点,如果它左侧比它高,显然可以在左侧被消的时候顺便把它消了. 否则只能消到左侧那个高度. 所以答案为$\sum max(0,a_i-a_{i-1})$ #include<bits/stdc++.h> using namespace std; #define il inline #define gc getchar() #define ri register int #define rb register bool #define rc register char #define rp(i,x,y) for(ri i=x;i<=y;++i) #define my(i,x,y) for(ri i=x;i>=y;--i) int n,nw,pre,as; il int read() { ri x=0;rb y=1;rc ch=gc; while(ch!='-' && (ch>'9' || ch<'0'))ch=gc; if(ch=='-')ch=gc,y=0; while(ch>='0' && ch<='9')x=(x<<1)+(x<<3)+(ch^'0'),ch=gc; return

C memset seems to not write to every member

爱⌒轻易说出口 提交于 2019-12-01 17:33:17
I wrote a small coordinate class to handle both int and float coordinates. template <class T> class vector2 { public: vector2() { memset(this, 0, sizeof(this)); } T x; T y; }; Then in main() I do: vector2<int> v; But according to my MSVC debugger, only the x value is set to 0, the y value is untouched. Ive never used sizeof() in a template class before, could that be whats causing the trouble? No don't use memset -- it zeroes out the size of a pointer (4 bytes on my x86 Intel machine) bytes starting at the location pointed by this . This is a bad habit: you will also zero out virtual pointers

memset to INT_MAX in C++ [duplicate]

99封情书 提交于 2019-12-01 15:02:45
This question already has an answer here: Using memset for integer array in c 10 answers I have the following code: int board[5][5]; memset(board, INT_MAX, sizeof(board)); //printing out INT_MAX cout << INT_MAX << endl; for(int r = 0; r < 5; r++) { for(int c = 0; c < 5; c++) { cout << setw(3) << board[r][c]; } cout << endl; } For some reason i am getting all -1 s in my array: 2147483647 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 How can I explain? Why aren't all the elements setting to INT_MAX? When I print INT_MAX it is printed out fine. The second parameter to

Warm up——缩点、树上直径

余生颓废 提交于 2019-12-01 13:30:12
题目链接 题意: 给出n个点和m条边的无向图,存在重边,问加一条边以后,剩下的桥的数量最少为多少。 题解: 把这个无向图缩点后会得到一个只由桥来连接的图(可以说这个图中的所有边都是桥,相当于一棵树), 然后我们只需要找出来这棵树的最大直径(即相距最远的两个点)。 因为如果我们把直径所在的两个端点连起来,这样减少的桥最多。 所以 答案就是 桥的数量 - 树的直径上桥的数量 原图求桥的数量,缩点后建立新的图求直径 代码: #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <map> #include <queue> using namespace std; const int maxn = 200005;//点数 const int maxm = 2001000;//边数,因为是无向图,所以这个值要*2 struct Edge { int to,next; bool cut;//是否是桥标记 } edge[maxm],edge1[maxm]; int head[maxn],tot,head1[maxn],tot1; int low[maxn],dfn[maxn],Stack[maxn],belong[maxn];/

zeroing derived struct using memset

余生长醉 提交于 2019-12-01 11:50:34
I want to zero out all members of a derived structure. There are hundreds of members and more are added every once in a while so I feel that initializing them explicitly is error-prone. The structures have no virtual functions and all the member fields are built-in. However, they are not POD by virtue of having non-trivial constructors. Apart from the standard frowning on the practice, do you see any issues with the following? struct Base { // Stuff }; struct Derived : public Base { // Hundreds of fields of different built-in types // including arrays Derived() { ::memset(reinterpret_cast<char

2019 ACM/TINA实验室10.12训练赛

跟風遠走 提交于 2019-12-01 06:08:53
A CodeForces 1238A Prime Subtraction 水题,除了差1都行。 #include<stdio.h> #include<set> #include<iostream> #define mem(ss) memset(ss,0,sizeof(ss)) #define rep(d, s, t) for(int d=s;d<=t;d++) #define rev(d, s, t) for(int d=s;d>=t;d--) typedef long long ll; typedef long double ld; typedef double db; const ll mod = 998244353; const int N = 1e4 + 10; #define io_opt ios::sync_with_stdio(false);cin.tie(0);cout.tie(0) using namespace std; ll T,x,y; int main(){ scanf("%lld",&T); while(T--){ scanf("%lld%lld",&x,&y); if(x-y==1){ printf("NO\n"); } else{ printf("YES\n"); } } return 0; } B CodeForces 1238B Kill 'Em