简体   繁体   English

为什么我们需要memset始终为零?

[英]Why do we need to memset always with zero?

Whenever we use memset we set it with zero. 每当我们使用memset我们将其设置为零。

Why? 为什么? Why not with 1 or 2 or something else. 为什么不用1或2或其他东西。

Also, setting a struct to 0 seems to work but setting to 1 doesn't: 此外,将结构设置为0似乎工作但设置为1不起作用:

typedef struct abc{
    int a;
} abc;

int main()
{
    abc* ab;
    memset(ab, 0, sizeof(abc));// it sets abc->a = 0; correct
}

But instead of 0 if I use 1 like: 但如果我使用1 ,则代替0

memset(ab, 1, sizeof(abc));

then the value of abc->a = garbage or not equals to 1 然后abc->a = garbagenot equals to 1

Why? 为什么?

You don't always need to memset to 0, this is just the most common (and useful) thing to do. 你并不总是需要将memset为0,这只是最常见(也是最有用)的事情。

memset sets each byte to some given value. memset将每个字节设置为某个给定值。 An int consists of 4 bytes, so, when memset ing to 1 , you'd set each of those 4 to 1 , then you'd have 00000001 | 00000001 | 00000001 | 00000001 一个int由4个字节组成,因此,当memset1 ,你将这4个中的每个设置为1 ,那么你就有了00000001 | 00000001 | 00000001 | 00000001 00000001 | 00000001 | 00000001 | 00000001 00000001 | 00000001 | 00000001 | 00000001 2 = 16843009 10 (the first numbers are in binary, the last in decimal). 00000001 | 00000001 | 00000001 | 00000001 2 = 16843009 10 (第一个数字是二进制数,最后一个数字是十进制数)。

Also - note that you're never allocating memory for ab . 另外 - 请注意,你永远不会为ab分配内存。 Even though your code may work now, it's not safe. 即使您的代码现在可以正常工作,也不安全。 This would be: 这将是:

abc ab;
memset(&ab, 0, sizeof(abc));

As others have mentioned, 0 is largely arbitrary. 正如其他人所提到的, 0基本上是任意的。 However, if we had to pick reasons, it's for convenience (at the cost of safety - you should prefer to explicitly initialise variables if you ever depend on them): 但是,如果我们必须选择原因,那是为了方便(以安全为代价 - 如果您依赖它们,您应该更喜欢显式初始化变量):

  • 0 is mostly understood as an initial value for integers, signed or unsigned, as it represents 0 no matter the width or endianness 0大部分被理解为有符号或无符号整数的初始值,因为它表示0,无论宽度或字节顺序如何
  • 0 is an end-of-string for char* s 0是char* s的字符串结尾
  • 0 is often 0.0 for floating point numbers 对于浮点数,0通常为0.0
  • 0 is often NULL in C, guaranteed to be NULL in C++ 0经常是NULL在C,保证是NULL在C ++中

The value you use with memset() to set memory to depends on your needs. 使用memset()设置内存的值取决于您的需要。 That's all. 就这样。 No one keeps you from using any other value to initialise memory. 没有人阻止你使用任何其他值来初始化内存。

As shown in your program When variable is defined, by default it will point to any location and any junk data can be previously available there. 如您的程序中所示定义变量时,默认情况下它将指向任何位置,并且之前可以使用任何垃圾数据。

we generally memset to (0)zero to make sure that variable is initialized to zero, so that later on in the program we can trust that the value assigned to the variable is genuinely assigned value and not any garbage. 我们通常将memset设置为零(0)以确保变量初始化为零,以便稍后在程序中我们可以信任分配给变量的值是真正赋值而不是任何垃圾。

hope it helps..... 希望能帮助到你.....

Firstly your program got an undefined behavior the reaosn is memory for abc* ab; 首先你的程序有一个未定义的行为,reaosn是abc * ab的内存; is never allocated! 永远不会分配!

Secondly it is required that string should be terminated with "null", which mean it is the end, so inspired by this we use null in memset. 其次要求字符串应该以“null”结尾,这意味着它是结束,因此受到启发,我们在memset中使用null。 But you can use any value you want... 但是你可以使用你想要的任何价值......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM