英文:
What's the best way to initialize a dynamically allocated boolean 2d array?
问题
以下是翻译好的部分:
"只是试图(重新)学习C并在一些基本概念上遇到了问题。
我看了一下这个帖子:https://stackoverflow.com/questions/9215210/how-to-memset-an-array-of-bools。
根据那个帖子,似乎我可以像下面这样初始化一个布尔型的二维数组:
size_t size = sizeof(bool[4][3]);
bool (*ary)[4] = malloc(size);
memset(ary, false, size);
我看到这段代码的问题是它依赖于布尔值占用1字节,我认为这是与具体实现相关的。除此之外,根据我的手册页(Linux手册页项目),memset的第二个参数似乎是一个整数... 手册页上写着:“memset()函数用常量字节c填充s指向的内存区域的前n个字节。”这不是矛盾吗?如果c是一个整数,那么它不是一个字节长。
编辑1:请参见下面的Ouroborus评论关于最后一段。
所以经过思考,我想出了以下代码:
size_t size = sizeof(bool[4][3]);
bool (*ary)[4] = malloc(size);
const bool orig[4][3] = {
{true, false, false},
{false, true, true},
{true, true, true},
{false, false, false}
};
memcpy(ary, &orig, size);
这样我可以初始化我的内存不仅仅是0(false),还可以是任何我想要的值。我已经测试过了,但这根本不起作用。
所以我的问题是:我的代码有什么问题?更重要的是,应该如何做?
谢谢!"
英文:
Just trying to (re)learn C and struggling with some basic concepts.
I've took a look this thread: https://stackoverflow.com/questions/9215210/how-to-memset-an-array-of-bools.
According to that, it seems i may initialize a boolean 2d array like the following:
size_t size = sizeof(bool[4][3]);
bool (*ary)[4] = malloc(size);
memset(ary, false, size);
The problem i see with this code is that it depends on a boolean taking 1 byte, which i think is implementation dependant. Apart from that, the second parameter to memset according to my manual page (linux manpages project) seems to be an int... That manual page says "The memset() function fills the first n bytes of the memory area pointed to by s with the constant byte c". Isn't this contradictory? If c is an int then it's not a byte long.
EDIT1: See Ouroborus comment below about the last paragraph.
So after thinking a bit, i came up with the following code:
size_t size = sizeof(bool[4][3]);
bool (*ary)[4] = malloc(size);
const bool orig[4][3] = {
{true, false, false},
{false, true, true},
{true, true, true},
{false, false, false}
};
memcpy(ary, &orig, size);
This way i can initialize my memory not just with 0s (false), but with whatever i want. I've tested and this doesn't work at all.
So my question is: What's wrong with my code? And more important, what's the way to do this?
Thanks!
答案1
得分: 2
除了[4]
之外,你的代码没有问题。不过,我建议使用以下方式:
bool (*ary)[3] = calloc(1, sizeof(bool[4][3]));
...
free(ary);
完成。一些注意事项:
- 注意
[3]
,而不是 4。这是一个指向bool [3]
数组的数组指针。之所以使用这种方式而不是bool (*ary)[4][3]
,是因为如果我们去掉一个维度 - 这个维度总是最左边的一个 - 我们可以使用方便的arr[i][j]
语法,这比(*ary)[i][j]
更可读。 - 请注意,
calloc
会在分配过程中将整个数组清零。这几乎是calloc
存在的唯一原因。 - 如果您需要将数组初始化为除了
false
之外的其他值,那么确实可以使用malloc
+memcpy
。但这可能会比仅仅调用一次calloc
慢一点。然而,始终优先考虑代码的可读性而不是微优化。
英文:
Apart from the [4]
, there's nothing wrong with your code. I would however recommend to use this:
bool (*ary)[3] = calloc(1, sizeof(bool[4][3]));
...
free(ary);
Done. Some notes:
- Note the
[3]
, not a 4. This is an array pointer pointing atbool [3]
arrays. The reason why we use this overbool (*ary)[4][3]
is because if we discard one dimension - which always has to be the left-most one - we can use the handyarr[i][j]
syntax, which is much more readable than(*ary)[i][j]
. - Note that
calloc
will zero out the whole array as part of the allocation process. That's pretty much the only reason whycalloc
exists in the first place. - If you need to init the array to something else than
false
then indeed usemalloc
+memcpy
. But this will perhaps be a tiny bit slower than just onecalloc
call. Always prioritize code readability over micro-optimizations, however.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论