memset

Compiler error: memset was not declared in this scope

匿名 (未验证) 提交于 2019-12-03 01:58:03
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am trying to compile my C program in Ubuntu 9.10 (gcc 4.4.1). I am getting this error: Rect.cpp:344: error: ‘memset’ was not declared in this scope But the problem is I have already included in my cpp file: #include #include And the same program compiles fine under Ubuntu 8.04 (gcc 4.2.4). Please tell me what am I missing. 回答1: You should include (or its C++ equivalent, ). 回答2: Whevever you get a problem like this just go to the man page for the function in question and it will tell you what header you are missing, e.g. $ man memset MEMSET

memset used with zero length parameter: ignore or watch out?

匿名 (未验证) 提交于 2019-12-03 01:20:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm having trouble finding any information regarding the following warning when linking a dynamic library: In function `MyClass::myfunc()': MyClass.cpp:(.text+0x14e4): warning: memset used with constant zero length parameter; this could be due to transposed parameters Here is an excerpt of myfunc : void MyClass::myfunc() { vector<Variable*>::const_iterator it; for (it = m_vars.begin(); it != m_vars.end(); ++it) { if ((*it)->recordme) { MyRecord* r = new MyRecord(*it); initMyRecord(*r); m_records.push_back(r); } } } So I'm pretty much stuck

memset for initialization in C++

匿名 (未验证) 提交于 2019-12-03 01:17:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: memset is sometimes used to initialize data in a constructor like the example below. Does it work in general ? Is it a good idea in general? class A { public : A (); private : int a ; float f ; char str [ 35 ]; long * lp ; }; A :: A () { memset ( this , 0 , sizeof (* this )); } 回答1: Don't use memset . It's a holdover from C and won't work on non-PODs. Specifically, using it on a derived class that contains any virtual functions -- or any class containing a non-builtin -- will result in disaster. C++ provides a specific syntax for

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

こ雲淡風輕ζ 提交于 2019-12-03 00:52:28
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 ? 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 value in such a manner. It may be used for call chaining like: char a[200]; strcpy(memset(a, 0, 200), "bla"

memset函数

匿名 (未验证) 提交于 2019-12-03 00:32:02
1.我也曾天真的以为 memset(a,0,sizeof(a)) 中的0可以用任意数替换 实际上这是错误的 memset的功能是将一快内存中的内容以单个字节逐个拷贝的方式放到指定的内存中去。 2.介绍几个常用的 memset(a,-1,sizeof(a)) 每个都会变为-1 memset(a,0,sizeof(a)) 每个都会变为0 memset(a,0xnfnfnfnf,sizeof(a)) 每个都会变为0xnfnfnfnf memset(a,0xnf,sizeof(a)) 每个都会变为0xnfnfnfnf (可以看到简化了上面那句) 我们一般用n=3的情况(防止溢出) memset(a,0x3f,sizeof(a)) 每个都会变为0x3f3f3f3f (可以看到简化了上面那句) memset(a,127,sizeof(a)) 每个都会变为1061109567 memset(a,63,sizeof(a)) 每个都会变为1061109567 也看到过有人写 memset(a,127/3,sizeof(a)) 每个都会变为707406378 无穷大的值可采用上面的这些方法 无穷小的值 memset(a,128,sizeof(a)) 每个都会变为-2139062144 解释: 128 (128的二进制是10000000)则放的是10000000 10000000 10000000

mem系列与str系列函数了解

匿名 (未验证) 提交于 2019-12-03 00:28:02
系列函数通常处理内存内容,而Str通常处理字符串,这俩个家族系列函数经常会使用,在网上搜集了一些资料进行了整理,方便自己以后查阅,下面介绍了memcpy,strcpy,strncpy,memmove,memcmp,strcmp,strcat,strstr,strtok等函数: 函数原型 :void *memcpy(void *dest, const void *src,size_t n); 函数功能 :内存拷贝;将src指向内存地址的连续N个指针位置的内容拷贝至dest指针指向的位置 函数返回 :无 参数说明 : dest ― 目的内存空间指针 src ― 源内存 n ― 拷贝指针位置个数 # include <memory> include <iostream> include < string > void main() { char * src = “Hello Frankie World !”; char * dest = new char [ 50 ]; memset (dest, 0 , 50 ); memcpy (dest,src, 7 ); std :: cout <<”dset值为:”<< std ::endl; Out(dest); } 函数原型 :void * memccpy(void *dest, void *src, unsigned char c,

ZOJ3380 Patchouli&#039;s Spell Cards C++版(概率DP+大数)

匿名 (未验证) 提交于 2019-12-03 00:25:02
题意题解不说了,这里主要讲用C++大数 为了节省时间使用了进制压缩,使用long long数组,进制1e9,使进制的平方在long long范围内 1290ms 4836KB #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; const long long mod=1000000000; struct biginteger { long long number[26]; int len; biginteger operator+(biginteger &b) { int i,root=max(len,b.len),Len=1; biginteger ans; memset(ans.number,0,sizeof(ans.number)); ans.len=1; for(i=0; i<root||ans.number[i]; i++) { ans.number[i]+=number[i]+b.number[i]; if(ans.number[i]>=mod) { ans.number[i+1]+=ans.number[i]/mod; ans.number[i]%=mod; } if(ans.number[i]) Len=i; } ans.len=Len+1; return ans

strcpy、memcpy和memset之间的区别

匿名 (未验证) 提交于 2019-12-03 00:22:01
今天刷题时遇到了这个问题,记录一下。 strcpy 比较简单,就是拷贝字符串,遇到'\0'时结束拷贝 。 memcpy 用来做内存拷贝,可以拷贝任何数据类型的对象并指定拷贝数据的长度:char a[100],b[50]; memcpy(b, a, sizeof(b)); 总结一下: strcpy和memcpy主要有以下3方面的区别。 复制的内容不同。strcpy只能复制字符串,而memcpy可以复制任意内容,例如字符数组、整型、结构体、类等。 复制的方法不同。strcpy不需要指定长度,它遇到字符串结束符"\0"便结束。memcpy则是根据其第3个参数决定复制的长度。 用途不同。通常在复制字符串时用strcpy,而需要复制其他类型数据时则一般用memcpy。 //注意:如果用的是sizeof(a),则会造成内存泄露。 比较复杂点的是 memset ,用来对一段内存空间全部设置为某个字符,一般用在对定义的字符串进行初始化为 ‘ ’或 ‘\0’,比如: char a[100];memset(a, '\0', sizeof(a)); 另外比较方便的是对结构体的操作, memset可以方便的清空一个结构类型的变量或数组: 比如有结构体struct sample_strcut stTest,一般清空结构体的话得用如下方式: struct sample_struct { char csName

memset函数为二维数组初始化

匿名 (未验证) 提交于 2019-12-03 00:22:01
1 int* a; a = new int[10]; sizeof(a) 只会返回出来指针的大小,所以我们只能自己计算这个数组的长度,这里应当是sizeof(int) * 10, 因为数组里面有10个int 所以应该,memset(a, 0, sizeof(int)*10);//将a数组初始化为0 2 int **p;//开一个n*m的数组 p = new int*[n]; for (int i = 0; i < n; i++) { p[i] = new int[m + 1]; memset(p[i], 0, sizeof(int)*m); //p赋初值为0,sizeof一个指针返回的是指针大小,所以还是sizeof(int) } for (int i = 0; i < n; i++)//释放p delete[] p[i]; delete[] p; memset(p, 0, sizeof(int)*m*n);//因为是p相当于一个指向一个一维数组的 指针 ,首先格式就不对 memset(p, 0, sizeof(p)*m*n);//也不对,因为我们要的是真正储存数据的空间,而不是指针 转载请标明出处: memset函数为二维数组初始化 文章来源: memset函数为二维数组初始化