简体   繁体   English

使用memset初始化float数组

[英]Initializing a float array with memset

This is the code that I want to try to write: 这是我想要编写的代码:

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>

int main(int argc, char *argv[])
{
    float arry[3] = {0};

    memset(arry, (int) 10.0, 3*sizeof(float));

    return 0;
}

My problem is that I want to see if it's possible to use memset to make every entry of an array to be a number other than 0. However, After stepping through that line, the array contents change to a very small number (0). 我的问题是我想看看是否可以使用memset使数组的每个条目都是0以外的数字。但是,在单步执行该行之后,数组内容会变为非常小的数字(0)。 I wonder what I'm doing wrong in this case with using the memset() function. 我想知道在这种情况下我使用memset()函数做错了什么。 I hope this isn't a duplicate post, as none of the suggested related questions as I'm typing this appears to be. 我希望这不是一个重复的帖子,因为我输入的内容似乎没有任何建议的相关问题。

Memset takes a int, but casts it to an unsigned char, and then fills each byte of float (sizeof(float) is probably 4) with that bit pattern. Memset采用int,但将其转换为unsigned char,然后使用该位模式填充float的每个字节(sizeof(float)可能为4)。 If this is c++, prefer fill instead: 如果这是c ++,请选择填充

#include <algorithm>
using namespace std;

//...

fill (arry,arry+3,10.0);

Casting a double to an int just creates the binary number 00001010 (10 in binary), and that is the value that is memset'ed. 将double转换为int只会创建二进制数00001010(二进制10),这是memset的值。 Since it's a char, each of your floats is actually receiving the bit pattern 00001010 00001010 00001010 00001010. 因为它是一个char,所以每个浮点数实际上都是接收位模式00001010 00001010 00001010 00001010。

No. memset takes a single byte and writes it to the array. 编号memset需要一个字节并将其写入数组。 A float is a multi-byte type. float是一种多字节类型。

EDIT: Yes, I know memset takes an int. 编辑:是的,我知道memset需要一个int。 But it only uses an unsigned char (a single byte) to fill with. 但它只使用无符号字符(单个字节)来填充。

为什么不只是一个简单的for循环?

Actually your try is a little bit misleading, memset works on bytes.. actually using a float for memset value doesn't make any sense! 实际上你的尝试有点误导,memset工作在字节上..实际上使用浮点数为memset值没有任何意义!

The float format just has 23+1 bits for mantissa and 8 bits for exponent.. when you write raw bytes values (using memset) inside a float you don't know what you are obtaining because you are setting values that will be interpreted in a different way! 浮点格式只有23 + 1位用于尾数,8位用于指数..当你在浮点数内写入原始字节值(使用memset)时,你不知道你得到了什么,因为你设置了将被解释的值另一种方式!

In your snippet you also cast it to (int) turning a 4-bytes float containing 10.0f in a 4-bytes integer containing the value 10.. but as said before if you memset a float with 10 (= 0x0a) you'll end up obtaining 0x0A0A0A0A that is not 10.0f in float format, it can be whatever value (also something very near to 0, like in your case). 在你的代码片段中,你还将它转换为(int)将包含10.0f的4字节浮点数转换为包含值10的4字节整数。但如前所述,如果你设置一个10(= 0x0a)的浮点数,你将会最终获得浮点格式不是10.0f的0x0A0A0A0A,它可以是任何值(也非常接近0,就像你的情况一样)。

Actually memset is useful when you want to initialize an array of chars (since you obtain effectively that value for every char because they are 1 byte long) or when you want to set everything to 0 (NULL) that works for every basic kind of data.. 实际上,当你想要初始化一个字符数组时, memset非常有用(因为你有效地获得每个字符的值,因为它们长1个字节),或者当你想将所有字符串设置为0(NULL)时,它适用于每种基本类型的数据..

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

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