[英]struct pointer as return type
I want to print kk[i].data[j]
but it is not printing at all. 我想打印
kk[i].data[j]
但根本不打印。
intarr_save_binary
is returning 2. I expect to get 0. intarr_save_binary
返回2。我希望得到0。
int k = sizeof(kk) / sizeof(kk[0]);
gives 0. I'm expecting to get 5. 给出0。我期望得到5。
Did I properly allocate the memory? 我是否正确分配了内存?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
int len;
int *data;
}intarr_t;
#define MAX 5
intarr_t* intarr_create(unsigned int len) {
intarr_t* new_intarr = (intarr_t*)malloc(sizeof(intarr_t));
if (!new_intarr) return NULL;
new_intarr->data = (int*)malloc(len * sizeof(int));
new_intarr->len = len;
return new_intarr;
}
int intarr_save_binary(intarr_t* ia, const char* filename) {
if (!ia) return 1;
if (!ia->data) return 2;
FILE* f = fopen(filename, "w");
if (!f) return 3;
if (fwrite(&ia->len, sizeof(ia->len), 1, f) == 1) {
fprintf(f, "%d ", ia->len);
}
else {
return 4;
}
if (fwrite(ia->data, sizeof(ia->data), ia->len, f) == ia->len) {
fclose(f);
return 0;
}
else {
fclose(f);
return 5;
}
}
int main() {
intarr_t *ia = (intarr_t*)malloc(MAX*sizeof(intarr_t));
int i;
int j;
for (j = 0; j < MAX; j++) {
ia[j].len = j + 1;
}
for (j = 0; j < MAX; j++) {
ia[j].data = (int*)malloc(ia[j].len * sizeof(int));
}
for (j = 0; j < MAX; j++) {
for (i = 0; i < ia[j].len; i++) {
ia[j].data = (i + 1) * j;
}
}
char name[20] = "myfile.txt";
int d;
printf("%d \n", intarr_save_binary(ia, name));
intarr_t *kk;
kk = intarr_create(MAX);
int k = sizeof(kk) / sizeof(kk[0]);
printf("%d\n",k);
for (j = 0; j < k; j++) {
for (i = 0; i < kk[j].len; i++) {
printf("%d: %d\n", i, kk[j].data[i]);
}
printf("\n");
}
free(kk);
return 0;
}
intarr_save_binary is returning 2. I expect to get 0.
intarr_save_binary返回2。我希望得到0。
for (j = 0; j < MAX; j++) {
for (i = 0; i < ia[j].len; i++) {
ia[j].data = (i + 1) * j;
}
}
This zeroes ia[0].data
on the very first pass through the double loop. 第一次通过双循环时,会将
ia[0].data
归零。 ( ia[0].data = (0 + 1) * 0
gives 0
). (
ia[0].data = (0 + 1) * 0
给出0
)。
Thus ia->data
is 0
, and !ia->data
is true, making the function return 2
. 因此,
ia->data
为0
, !ia->data
为true,使函数return 2
。
int k = sizeof(kk) / sizeof(kk[0]);
int k = sizeof(kk)/ sizeof(kk [0]); gives 0. I'm expecting to get 5.
给出0。我期望得到5。
You obviously expect sizeof kk
to give the total amount of memory allocated for kk
. 您显然希望
sizeof kk
给出分配给kk
的内存总量。
And that is what you get, actually -- the total amount of memory allocated for intarr_t *
, which is the type of kk
at that point. 实际上,这就是您所获得的-为
intarr_t *
分配的内存intarr_t *
,这是此时的kk
类型。 That most likely results in 4 or 8, depending on your architecture. 根据您的体系结构,最有可能导致4或8。 What it is not is whatever
len * sizeof(int)
resulted in when you called intarr_create()
. 它不是您调用
intarr_create()
时len * sizeof(int)
导致的结果。 As @BoPersson commented, if you allocate the memory yourself, you have to remember yourself how much you allocated. 正如@BoPersson所说,如果您自己分配内存,则必须记住自己分配了多少内存。
The sizeof kk / sizeof kk[0]
"trick" only works if kk
actually is an array , ie if it has been declared as such within the scope of you using the sizeof
operator on it so the compiler can "see" its size. sizeof kk / sizeof kk[0]
“技巧”仅在kk
实际上是一个数组时才有效,即,如果在您的范围内使用sizeof
运算符对其进行了声明,以便编译器可以“查看”其大小。
So, as you have an int
and an int *
in your struct kk[0]
, which together are very likely to require more memory than an intarr_t *
, the integer division results in 0
. 因此,由于您在结构
kk[0]
有一个int
和一个int *
,它们与intarr_t *
一起很可能需要更多的内存,因此整数除法会得出0
。
You might also take note that free()
is not recursive. 您可能还会注意到,
free()
不是递归的。 With free(kk)
, you are leaking all the memory you allocated for all the data
members. 使用
free(kk)
,您将泄漏为所有data
成员分配的所有内存。 For every malloc()
, there needs to be a corresponding free()
. 对于每个
malloc()
,都需要有一个对应的 free()
。 (And it does matter even if the program ends right after that one free()
, as not all operating systems can / will protect you from this error.) (而且它即使在节目结束物权之后一个
free()
因为不是所有的操作系统都可以/将保护你从这个错误。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.