为什么 memset 不分配 1?
Why isn't memset assigning 1?
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
int color[1001][1001];
int main() {
int i, j;
memset(color, 1, sizeof(color[0][0]) * 2 * 2);
for(i = 0; i < 4; i++) {
for(j = 0; j < 4; j++) {
printf("%d ", color[i][j]);
}
printf("\n");
}
printf("\n");
return 0;
}
输出:
16843009 16843009 16843009 16843009
0 0 0 0
0 0 0 0
0 0 0 0
为什么不分配 1?为什么它不打印 1 而不是 16843009 ?我如何分配整数 1?
但是如果我写 memset(color, 0, sizeof(color[0][0]) * 2 * 2);
那么输出:
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
这是为什么?
任何答案将不胜感激。提前致谢。
因为memset
将每个字节设置为1
。
因此,如果 int
是四个字节(32 位,最常见的是什么),那么您将每个元素设置为 0x01010101
.
阅读更多in this memset
reference page。
对于更像 C++ 的解决方案,我建议使用 std::fill
:
std::fill(&color[0][0], &color[0][0] + sizeof(color) / sizeof(color[0][0]), 1);
这会将所有元素设置为 1
。
第三种选择是使用std::array
instead, and its fill
成员函数:
std::array<std::array<int, 1001>, 1001> color;
...
for (auto& inner : color)
{
inner.fill(1);
}
memset()
会将指定值写入范围内的 每个字节 ,因此如果 int
是 4 字节长,则值将是 0x01010101
,而不是 1
。
要分配整数1,分配整数1。
for (i = 0; i < (int)(sizeof(color)/sizeof(*color)); i++) {
for (j = 0; j < (int)(sizeof(color[i])/sizeof(*color[i])); j++) {
color[i][j] = 1;
}
}
Manpage :
#include <string.h>
void *memset(void *s, int c, size_t n)
The memset()
function fills the first n
bytes of the memory area pointed to by s
with the constant byte c
.
因此,memset
不能用1
初始化int
数组,因为如果int
用4个字节表示,那么它会初始化每个字节1
.
16843009
等同于 0x01010101
。 4 个字节中的每一个都用 01
.
初始化
使用 memset
,int
的数组只能用 0
或 -1
初始化,因为 0
和 -1
都具有所有位 0
和 1
分别以 二进制补码表示 而不管 int
数据类型的大小。
memset()
正在将您的内存设置为与您选择的值对齐的 8 位,即 1
。但是对于数组 color[][]
,您声明了 32 位数据类型 int
,它有四个字节。所以 memset()
所做的是将这四个字节中的每一个都设置为值 1
.
这也解释了您的结果:16843009d = 0x01010101
。
如果你看看你的记忆:
memset()
到 1
:
//.color[0][0].||..color[0][1]...||..color[0][2]...||..color[0][3]..
01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01
//.color[1][0].||..color[1][1]...||..color[1][2]...||..color[1][3]..
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
//.color[2][0].||..color[2][1]...||..color[2][2]...||..color[2][3]..
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
//.color[3][0].||..color[3][1]...||..color[3][2]...||..color[3][3]..
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
memset()
到 0
:
//.color[0][0].||..color[0][1]...||..color[0][2]...||..color[0][3]..
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
//.color[1][0].||..color[1][1]...||..color[1][2]...||..color[1][3]..
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
//.color[2][0].||..color[2][1]...||..color[2][2]...||..color[2][3]..
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
//.color[3][0].||..color[3][1]...||..color[3][2]...||..color[3][3]..
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
如果您使用值 0
调用 memset()
,那么您将获得 32 位 int
value = 0x00000000 = 0d
。
注:
如果要将整个数组设置为一个值,请使用以下行:
memset(color, 1, sizeof(color));
那么你的数组如下所示:
1010101 1010101 1010101 1010101
1010101 1010101 1010101 1010101
1010101 1010101 1010101 1010101
1010101 1010101 1010101 1010101
查看代码here[^].
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
int color[1001][1001];
int main() {
int i, j;
memset(color, 1, sizeof(color[0][0]) * 2 * 2);
for(i = 0; i < 4; i++) {
for(j = 0; j < 4; j++) {
printf("%d ", color[i][j]);
}
printf("\n");
}
printf("\n");
return 0;
}
输出:
16843009 16843009 16843009 16843009
0 0 0 0
0 0 0 0
0 0 0 0
为什么不分配 1?为什么它不打印 1 而不是 16843009 ?我如何分配整数 1?
但是如果我写 memset(color, 0, sizeof(color[0][0]) * 2 * 2);
那么输出:
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
这是为什么?
任何答案将不胜感激。提前致谢。
因为memset
将每个字节设置为1
。
因此,如果 int
是四个字节(32 位,最常见的是什么),那么您将每个元素设置为 0x01010101
.
阅读更多in this memset
reference page。
对于更像 C++ 的解决方案,我建议使用 std::fill
:
std::fill(&color[0][0], &color[0][0] + sizeof(color) / sizeof(color[0][0]), 1);
这会将所有元素设置为 1
。
第三种选择是使用std::array
instead, and its fill
成员函数:
std::array<std::array<int, 1001>, 1001> color;
...
for (auto& inner : color)
{
inner.fill(1);
}
memset()
会将指定值写入范围内的 每个字节 ,因此如果 int
是 4 字节长,则值将是 0x01010101
,而不是 1
。
要分配整数1,分配整数1。
for (i = 0; i < (int)(sizeof(color)/sizeof(*color)); i++) {
for (j = 0; j < (int)(sizeof(color[i])/sizeof(*color[i])); j++) {
color[i][j] = 1;
}
}
Manpage :
#include <string.h> void *memset(void *s, int c, size_t n)
The
memset()
function fills the firstn
bytes of the memory area pointed to bys
with the constant bytec
.
因此,memset
不能用1
初始化int
数组,因为如果int
用4个字节表示,那么它会初始化每个字节1
.
16843009
等同于 0x01010101
。 4 个字节中的每一个都用 01
.
初始化
使用 memset
,int
的数组只能用 0
或 -1
初始化,因为 0
和 -1
都具有所有位 0
和 1
分别以 二进制补码表示 而不管 int
数据类型的大小。
memset()
正在将您的内存设置为与您选择的值对齐的 8 位,即 1
。但是对于数组 color[][]
,您声明了 32 位数据类型 int
,它有四个字节。所以 memset()
所做的是将这四个字节中的每一个都设置为值 1
.
这也解释了您的结果:16843009d = 0x01010101
。
如果你看看你的记忆:
memset()
到 1
:
//.color[0][0].||..color[0][1]...||..color[0][2]...||..color[0][3]..
01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01
//.color[1][0].||..color[1][1]...||..color[1][2]...||..color[1][3]..
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
//.color[2][0].||..color[2][1]...||..color[2][2]...||..color[2][3]..
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
//.color[3][0].||..color[3][1]...||..color[3][2]...||..color[3][3]..
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
memset()
到 0
:
//.color[0][0].||..color[0][1]...||..color[0][2]...||..color[0][3]..
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
//.color[1][0].||..color[1][1]...||..color[1][2]...||..color[1][3]..
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
//.color[2][0].||..color[2][1]...||..color[2][2]...||..color[2][3]..
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
//.color[3][0].||..color[3][1]...||..color[3][2]...||..color[3][3]..
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
如果您使用值 0
调用 memset()
,那么您将获得 32 位 int
value = 0x00000000 = 0d
。
注:
如果要将整个数组设置为一个值,请使用以下行:memset(color, 1, sizeof(color));
那么你的数组如下所示:
1010101 1010101 1010101 1010101
1010101 1010101 1010101 1010101
1010101 1010101 1010101 1010101
1010101 1010101 1010101 1010101
查看代码here[^].