使用 memset 初始化浮点数组

发布于 2024-07-25 04:39:55 字数 451 浏览 6 评论 0原文

这是我想尝试编写的代码:

#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;
}

我的问题是我想看看是否可以使用 memset 使数组的每个条目都为 0 以外的数字。但是,在单步执行该行之后,数组内容更改为非常小的数字 (0)。 我想知道在这种情况下使用 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). I wonder what I'm doing wrong in this case with using the memset() function. I hope this isn't a duplicate post, as none of the suggested related questions as I'm typing this appears to be.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

另类 2024-08-01 04:39:55

Memset 接受一个 int,但将其转换为无符号字符,然后填充float 的每个字节(sizeof(float) 可能是 4)都具有该位模式。 如果这是 C++,则更喜欢使用 fill 代替:

#include <algorithm>
using namespace std;

//...

fill (arry,arry+3,10.0);

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. If this is c++, prefer fill instead:

#include <algorithm>
using namespace std;

//...

fill (arry,arry+3,10.0);
萝莉病 2024-08-01 04:39:55

将 double 转换为 int 只会创建二进制数 00001010(二进制为 10),这就是 memset 的值。 由于它是一个字符,因此每个浮点实际上都接收位模式 00001010 00001010 00001010 00001010。

Casting a double to an int just creates the binary number 00001010 (10 in binary), and that is the value that is memset'ed. Since it's a char, each of your floats is actually receiving the bit pattern 00001010 00001010 00001010 00001010.

对你再特殊 2024-08-01 04:39:55

不。memset 接受一个字节并将其写入数组。 float 是一种多字节类型。

编辑:是的,我知道 memset 需要一个 int 。 但它只使用一个unsigned char(单个字节)来填充。

No. memset takes a single byte and writes it to the array. A float is a multi-byte type.

EDIT: Yes, I know memset takes an int. But it only uses an unsigned char (a single byte) to fill with.

眉目亦如画i 2024-08-01 04:39:55

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

Why not just a simple for loop?

ˇ宁静的妩媚 2024-08-01 04:39:55

实际上你的尝试有点误导,memset 适用于字节..实际上使用 float 作为 memset 值没有任何意义!

浮点格式只有 23+1 位用于尾数,8 位用于指数。当您在浮点中写入原始字节值(使用 memset)时,您不知道您将获得什么,因为您正在设置将被解释的值另一种方式!

在您的代码片段中,您还可以将其转换为 (int) 将包含 10.0f 的 4 字节浮点数转换为包含值 10 的 4 字节整数。但是如前所述,如果您用 10 (= 0x0a) memset 一个浮点数,您将最终获得的 0x0A0A0A0A 不是浮点格式的 10.0f,它可以是任何值(也可以是非常接近 0 的值,就像您的情况一样)。

实际上,当您想要初始化一个字符数组时(因为您可以有效地获得每个字符的值,因为它们的长度为 1 个字节),或者当您想要设置时, memset 很有用一切都为 0(NULL),适用于每种基本类型的数据。

Actually your try is a little bit misleading, memset works on bytes.. actually using a float for memset value doesn't make any sense!

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!

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).

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..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文