memset

how to use memset for double dimentional array?

一世执手 提交于 2019-12-25 06:40:07
问题 I have a Double dim. array: alarm_1_active_buffer[MAX_NUM_ALARMS][MAX_ALARM_STRING_SIZE]; I want to clear the buffer before filling it. Like this : for(index=0; index<MAX_NUM_ALARMS ; index++) { memset(&alarm_1_active_buffer[index], 0, MAX_ALARM_STRING_SIZE); memset(&alarm_1_active_buffer[index],string, MAX_ALARM_STRING_SIZE); } It is not working. 回答1: Since the arrays are laid in continuos address spaces, you don't have to do anything special for 2d arrays. You can simply use memset(alarm_1

what would be reason for “undefined behaviors” upon using memset on library class(std::string)? [closed]

烂漫一生 提交于 2019-12-25 05:32:46
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 4 years ago . Basically string is type of basic_string template class with char instantiation typedef basic_string string Up to my knowledge, Here basic_string is class which contains some collections of some data members and member functions as similar like developer is creating one class. Memseting on user

数学3班 慕文琪 第五章总结

…衆ロ難τιáo~ 提交于 2019-12-24 11:48:43
第五单元 数组 第一课 一维数组的定义一维数组的格式如下类型标识符 数组名[常量表达式];类型标识符可以是任何基本数据,也可以是结构体等构造类型。常量表达式的值是数组元素的个数。例:int score[10000];//或者float score[10000];bool xb[10000];//或者char xb[10000];2.一维数组的元素引用数组定义好后,引用数组中的任意一个元素。格式:数组名[下标]下标,只能为整型常量或整型表达式。不能引用整个数组,只能逐个引用数组的单个元素。3.一维数组的存储结构第二课 一维数组的输入与输出两个函数给数组“整体”赋值(1)memset函数给数组按字节进行赋值,一般用在char型数组中。如果是int类型的数组,一般赋值0和-1。使用前需要头文件:#include例如,memset(h,0,sizeof(h))将h数组,所有元素赋值为0。(2)fill函数给数组按元素进行赋值,可以是整个数组,也可以是部分连续元素,可以赋任何值。需要头文件:#include例,fill(a,a+10,5),就是将a数组的前十个元素赋值为5。#include#includeusing namespace std;int main(){int a[10],b[10],c[10],d[10],i;memset(a,0,sizeof(a));for(i=0;i<9

Unix domain socket

青春壹個敷衍的年華 提交于 2019-12-24 00:11:22
转载:http://www.cnblogs.com/chekliang/p/3222950.html socket API原本是为网络通讯设计的,但后来在socket的框架上发展出一种IPC机制,就是UNIX Domain Socket。 虽然网络socket也可用于同一台主机的进程间通讯(通过loopback地址127.0.0.1),但是UNIX Domain Socket用于IPC更有效率:不需要经过网络协议栈,不需要打包拆包、计算校验和、维护序号和应答等,只是将应用层数据从一个进程拷贝到另一个进程。 这是因为,IPC机制本质上是可靠的通讯,而网络协议是为不可靠的通讯设计的。UNIX Domain Socket也提供面向流和面向数据包两种API接口,类似于TCP和UDP,但是面向消息的UNIX Domain Socket也是可靠的,消息既不会丢失也不会顺序错乱。   UNIX Domain Socket是全双工的,API接口语义丰富,相比其它IPC机制有明显的优越性,目前已成为使用最广泛的IPC机制,比如X Window服务器和GUI程序之间就是通过UNIX Domain Socket通讯的。   使用UNIX Domain Socket的过程和网络socket十分相似,也要先调用socket()创建一个socket文件描述符,address family指定为AF_UNIX

Educational Codeforces Round 44 (Rated for Div. 2)

*爱你&永不变心* 提交于 2019-12-23 13:36:25
题目链接: https://codeforces.com/contest/985 ’A. Chess Placing 题意 :给了一维的一个棋盘,共有n(n必为偶数)个格子。棋盘上是黑白相间的。现在棋盘上有n/2个棋子,让你全部移动到黑色格子或者白色格子,要求步数最少,并输出步数。 题解:由于黑色或者白色都是固定位置,我们对棋子位置排个序,然后全部移动到任意一个颜色,取两个最小步数的最小值就好了。 1 #include<bits/stdc++.h> 2 #define clr(x) memset(x,0,sizeof(x)) 3 #define clr_1(x) memset(x,-1,sizeof(x)) 4 #define mod 1000000007 5 #define INF 0x3f3f3f3f 6 #define LL long long 7 #define pb push_back 8 #define pbk pop_back 9 #define ls(i) (i<<1) 10 #define rs(i) (i<<1|1) 11 #define mp make_pair 12 using namespace std; 13 const int N=1e5+10; 14 int a[N],p[N]; 15 int n,d,ans1,ans2; 16 int main()

socket基本使用

我只是一个虾纸丫 提交于 2019-12-23 03:44:22
UDP发送和接收 MainRecv.cpp #include <iostream> #include <WinSock2.h> #include <sstream> #pragma comment(lib,"ws2_32.lib") #define RECV_IP "127.0.0.1" #define RECV_PORT 8899 #define LEN_RECV_BUF 2048 int main() { std::ostringstream ossTemp; WSAData wsaData; std::cout<<"Start..."<<std::endl; WSAStartup(MAKEWORD(2,2),&wsaData); SOCKET sktRecv=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP); sockaddr_in addrRecv; addrRecv.sin_family=AF_INET; addrRecv.sin_addr.s_addr=inet_addr(RECV_IP); //htonl(INADDR_ANY); addrRecv.sin_port=htons(RECV_PORT); int retVal; retVal=bind(sktRecv,(sockaddr*)&addrRecv,sizeof(addrRecv))

SIGSEV on strcmp of memset string

99封情书 提交于 2019-12-20 07:49:17
问题 In the following program, i am expecting the for loop to stop after 3 elements. But it keeps on going indefinitely and fails later on with a coredump. is malloc() needed for char*[]` would strcmp fail if i memset to 0? . #include<stdio.h> #include<string.h> #include<stdlib.h> int main() { char* str[10]; memset(str,0,10); str[0]=malloc(sizeof(char)*(strlen("Sample")+1)); str[1]=malloc(sizeof(char)*(strlen("Sample")+1)); str[2]=malloc(sizeof(char)*(strlen("Sample")+1)); strcpy(str[0],"Sample");

What's the 'right' way to implement a 32-bit memset for CUDA?

♀尐吖头ヾ 提交于 2019-12-20 04:04:29
问题 CUDA has the API call cudaError_t cudaMemset (void *devPtr, int value, size_t count) which fills a buffer with a single-byte value. I want to fill it with a multi-byte value. Suppose, for the sake of simplicity, that I want to fill devPtr with a 32-bit (4-byte) value, and suppose we can ignore endianness. Now, the CUDA driver has the following API call: CUresult cuMemsetD32(CUdeviceptr dstDevice, unsigned int ui, size_t N) So is it enough for me to just: obtain the CUdeviceptr from the device

C memset seems to not write to every member

浪尽此生 提交于 2019-12-19 18:57:14
问题 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? 回答1: No don't use memset -- it zeroes out the size of a pointer (4 bytes on my x86 Intel machine)

memset to INT_MAX in C++ [duplicate]

老子叫甜甜 提交于 2019-12-19 11:44:16
问题 This question already has answers here : Using memset for integer array in C (10 answers) Closed 3 years ago . 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