获取矩阵中当前元素的数量以决定何时重新分配它
我有一个在程序启动后被调用的矩阵。然后我需要弄清楚何时需要重新分配它(在数组已满之后)。我最初的想法是在结构体中保存一个计数器变量,并在每次插入后递增它,但我认为有更好的方法使用 sizeof() 吗?我正在使用calloc
,所以我不需要结合使用malloc
和memset
,这意味着获得calloced<的索引/code> 将被设置为 0?因此,我需要检查数组中有多少个“非零”元素,以了解何时必须
realloc
。我编写了一个简单的测试程序,但我对输出“1”的底部 printf
语句感到困惑。将元素放入该数组后,它们不会以任何方式修改,这就是为什么计数器方法对我来说似乎很好,但我想测试这是否有效。
#include <stdio.h>
#include <stdlib.h>
struct c {
char** arr;
};
int main() {
struct c a;
a.arr = calloc(40, sizeof(char*));
for (int x = 0; x < 40; ++x) {
a.arr[x] = calloc(20, sizeof(char));
a.arr[x] = "lol\0";
}
a.arr[30] = "dfsd\0";
printf("%s\n", a.arr[30]);
printf("reallocing...\n");
a.arr = realloc(a.arr, 200 * sizeof(char*));
if (a.arr == NULL) {
printf("failed\n");
}
for (int x = 40; x < 200; ++x) {
a.arr[x] = calloc(20, sizeof(char));
a.arr[x] = "lol\0";
}
for (int x = 0; x < 200; ++x) {
printf("%s\n", a.arr[x]);
}
printf("total size: %i\n", sizeof(a.arr) / sizeof(a.arr[0]));
}
这不应该输出 1,而是输出矩阵的大小,因为 sizeof(a.arr[0]) 将为您提供 char 类型的大小,然后将其除以数组的大小(以字节为单位)。
I have a matrix that gets calloced
after program start. I then need to figure out when I need to realloc it (after the array is full). My initial thought was to hold a counter variable inside the struct and increment it after each insert, but i'm thinking there is a better way with using the sizeof()
? I am using calloc
so i do not need to use malloc
and memset
in conjuction, this means that the indexes that get calloced
will be set to 0? So i would need to check how many "non zero" elements there are in the array to know when i have to realloc
. I have wrote a simple test program but i am confused by the bottom printf
statement that outputs '1'. After the elements are put into this array, they wont be modified in any way, which is why the counter method seems fine to me, but I wanted to test if this worked.
#include <stdio.h>
#include <stdlib.h>
struct c {
char** arr;
};
int main() {
struct c a;
a.arr = calloc(40, sizeof(char*));
for (int x = 0; x < 40; ++x) {
a.arr[x] = calloc(20, sizeof(char));
a.arr[x] = "lol\0";
}
a.arr[30] = "dfsd\0";
printf("%s\n", a.arr[30]);
printf("reallocing...\n");
a.arr = realloc(a.arr, 200 * sizeof(char*));
if (a.arr == NULL) {
printf("failed\n");
}
for (int x = 40; x < 200; ++x) {
a.arr[x] = calloc(20, sizeof(char));
a.arr[x] = "lol\0";
}
for (int x = 0; x < 200; ++x) {
printf("%s\n", a.arr[x]);
}
printf("total size: %i\n", sizeof(a.arr) / sizeof(a.arr[0]));
}
This should not output one but rather the size of the matrix, since the sizeof(a.arr[0]) will give you the size of the char type, then divide it into the size of the array in bytes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
sizeof
对于报告对象的大小很有用。*alloc()
返回指向已分配内存的指针。sizeof
不能用于辨别从返回的指针分配的数量。跟踪使用辅助数据分配的内存。也许
sizeof
is useful to report the size of an object.*alloc()
returns a pointer to allocated memory.sizeof
cannot be used to discern the amount allocated from the returned pointer. Keep track of memory allocated using auxiliary data.Perhaps
sizeof(array)/sizeof(array[0])
机制仅适用于原始声明范围内的固定大小数组。它不能用于确定动态分配数组的大小,也不能用于作为单个指针传递给函数、存储等的数组 - 即数组已衰减为指针的地方您必须自己将大小存储在某处
The
sizeof(array)/sizeof(array[0])
mechanism only works for fixed size arrays within the scope of their original declaration. It cannot be used for determining the size of dynamically allocated arrays, not for arrays passed as a single pointer to a function , stored etc - ie where the array has decayed to a pointerYou have to store the size somewhere yourself