【测试】cstring头文件中memset的功能

前言:测试程序

#define USEFASTERREAD 1 

#define rg register
#define inl inline
#define DEBUG printf("[Passing [%s] in line %d.]\n", __func__, __LINE__)
#define putline putchar('\n')
#define putsp putchar(' ')
#define Rep(a, s, t) for(rg int a = s; a <= t; a++)
#define Repdown(a, t, s) for(rg int a = t; a >= s; a--)
typedef long long ll;
#include<cstdio>

#if USEFASTERREAD
char In[1 << 20], *ss = In, *tt = In;
#define getchar() (ss == tt && (tt = (ss = In) + fread(In, 1, 1 << 20, stdin), ss == tt) ? EOF : *ss++)
#endif
struct IO {
	void RS() {freopen("test.in", "r", stdin), freopen("test.out", "w", stdout);} 
	template<typename T> inline IO r(T& x)const	{
	    x = 0; T f = 1; char ch = getchar();
	    for(; ch < '0' || ch > '9'; ch = getchar()) if(ch == '-') f = -1;
	    for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + int(ch - '0');
	    x *= f; return *this;
	}
	template<typename T> inline IO w(T x)const {
	    if(x < 0) {putchar('-'); x = -x;}
	    if(x >= 10) w(x / 10);
	    putchar(x % 10 + '0'); return *this;
	}
	template<typename T> inline IO wl(const T& x)const {w(x), putline; return *this;}
	template<typename T> inline IO ws(const T& x)const {w(x), putsp; return *this;}
	inline IO l() {putline; return *this;}
	inline IO s() {putsp; return *this;}
}io;
template<typename T> inline T Max(const T& x, const T& y) {return y < x ? x : y;}
template<typename T> inline T Min(const T& x, const T& y) {return y < x ? y : x;}
template<typename T> inline void Swap(T& x, T& y) {T tmp = x; x = y; y = tmp;}
template<typename T> inline T Abs(const T& x) {return x > 0 ? x : -x;} 


#include<cstring>
int a[50];
ll b[50]; 
int main() {
    //io.RS();
    memset(a, 0x7f, sizeof a);
    memset(b, 0x7f, sizeof b);
    printf("int: \n%d\n%#x\n", a[0], a[0]);
    printf("ll : \n%lld\n%#llx\n", b[0], b[0]);
    return 0;
}

每次改变memset第二个参数的值,就可以完成一次测试.

一、赋极大值

一般有两种赋值法

危险赋值法:0x7f or 127

使用memset(a, 0x7f, sizeof a);
memset(a, 127, sizeof a);
得到的结果:

int:
2139062143
0x7f7f7f7f
ll :
9187201950435737471
0x7f7f7f7f7f7f7f7f

显然,这样赋值出来的数值太大,若有两个这样的数相加,就会爆掉当前的类型
一般可以用来求最小值。

安全赋值法:0x3f or 63

使用memset(a, 0x3f, sizeof a);
memset(a, 63, sizeof a);
结果:

int:
1061109567
0x3f3f3f3f
ll :
4557430888798830399
0x3f3f3f3f3f3f3f3f

这样的数,相加不会爆类型,比较安全(当然相乘还是不行)
一般来说,相乘我们要向上推一个类型:
short相乘用int
int相乘用long long
long long相乘用__int128

二、赋值为零: 0x00

很常用的用法
memset(a, 0x00, sizeof a);
或直接memset(a, 0, sizeof a);

得到的结果:

int:
0
0
ll :
0
0

三、赋为-1:0xff or -1

使用memset(a, -1, sizeof a);
memset(a, 0xff, sizeof a);
测试结果:

int:
-1
0xffffffff
ll :
-1
0xffffffffffffffff

四、赋极小值

危险赋值法:-0x7f or -127

使用memset(a, -127, sizeof a);
或者memset(a, -0x7f, sizeof a);
很好记不是吗2333
测试结果:

int:
-2122219135
0x81818181
ll :
-9114861777597660799
0x8181818181818181

同样,相加会爆类型

安全赋值法:-0x3f or -63

使用memset(a, -63, sizeof a);
或者memset(a, -0x3f, sizeof a);
测试结果:

int:
-1044266559
0xc1c1c1c1
ll :
-4485090715960753727
0xc1c1c1c1c1c1c1c1

相加ok!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

日居月诸Rijuyuezhu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值