This question already has an answer here:
这个问题在这里已有答案:
- String assignment in C 4 answers
C 4答案中的字符串赋值
Here's the snippet with issues.
这是有问题的片段。
int main()
{
char** RESERV = (char**)malloc(sizeof(char*)*4);
printf("%i, %i, %i, %i, %i", **RESERV, *RESERV, RESERV, &**RESERV, sizeof(char*));
int i;
for(i = 0; i < 4; i++)
{
RESERV[i] = (char*)calloc(sizeof(char),16);
RESERV[i][15] = '\0';
}
for(i = 0; i < 4; i++)
RESERV[i]="Iambananananananananananana";
for(i = 0; i < 4; i++)
printf("\r\n>%i<", RESERV[i]);
for(i = 0; i < 4; i++)
{
printf("\r\n<%i>", (RESERV[i]));
free(RESERV[i]);
}
free(RESERV);
}
This code free()
is working fine in 32 bit , but somehow crashes horribly in 64 bit mode.
这段代码free()在32位工作正常,但在64位模式下以某种方式可怕地崩溃。
In my main program I've omitted freeing the members of the char**
causing unexptected behavior every now and then, which I obviously do not want.
在我的主程序中,我省略了释放char **的成员,导致无时无刻的行为,我显然不想要。
I've tried playing around with addresses and pointers, even tried
我试过玩地址和指针,甚至尝试过
free(RESERV+(i*sizeof(char*))
Which failed too. Can someone clarify what I'm doing wrong?
哪个也失败了。有人能澄清我做错了什么吗?
3 个解决方案
#1
3
In your code
在你的代码中
RESERV[i]="Iambananananananananananana";
creates the problem. It overwrites the memory allocated by malloc()
. Thus,
造成问题。它会覆盖malloc()分配的内存。从而,
- You face memory leak, because the
malloc()
ed pointer is lost. - You cannot call
free()
with the changed pointer. It invokes undefined behaviour.
您面临内存泄漏,因为malloc()ed指针丢失。
您无法使用更改的指针调用free()。它调用未定义的行为。
Solution:
In C
, you don't assign strings, instead, you can use strcpy()
to get your work done.
在C中,您不分配字符串,而是可以使用strcpy()来完成工作。
Notes:
-
even in case of
strcpy()
you cannot use"Iambananananananananananana"
. In this case, it will create memory overrun as destination does not have enough memory to hold it completely.即使在strcpy()的情况下,你也不能使用“Iambanananananananananananana”。在这种情况下,它将创建内存溢出,因为目标没有足够的内存来完全保存它。
-
Use proper format specifiers. in your
printf()
statements, most of the arguments to%i
are not of typeint
. For pointer type arguments, you should be using%p
, atleast. Otherwise, it will be UB.使用正确的格式说明符。在你的printf()语句中,%i的大多数参数都不是int类型。对于指针类型参数,您应该至少使用%p。否则,它将是UB。
#2
2
RESERV[i]="Iambananananananananananana";
This is not the proper way to populate this string after allocating it. You are overwriting the value of that pointer.
这不是在分配后填充此字符串的正确方法。您正在覆盖该指针的值。
strncpy(RESERV[i], "Iambananananananananananana", 15);
RESERV[i][15] = 0;
This is what you're looking for.
这就是你要找的东西。
Notice, too, that you are apparently to assign something into that pointer that is larger than the memory that you have allocated.
另请注意,您显然是在指针中指定了比您已分配的内存大的内容。
#3
0
The line
RESERV[i]="Iambananananananananananana";
is incorrect - You need to do
是不正确的 - 你需要这样做
strncpy(RESERV[i], "Iambananananananananananana", 15);
RESERVp[i][15] = 0;
#1
3
In your code
在你的代码中
RESERV[i]="Iambananananananananananana";
creates the problem. It overwrites the memory allocated by malloc()
. Thus,
造成问题。它会覆盖malloc()分配的内存。从而,
- You face memory leak, because the
malloc()
ed pointer is lost. - You cannot call
free()
with the changed pointer. It invokes undefined behaviour.
您面临内存泄漏,因为malloc()ed指针丢失。
您无法使用更改的指针调用free()。它调用未定义的行为。
Solution:
In C
, you don't assign strings, instead, you can use strcpy()
to get your work done.
在C中,您不分配字符串,而是可以使用strcpy()来完成工作。
Notes:
-
even in case of
strcpy()
you cannot use"Iambananananananananananana"
. In this case, it will create memory overrun as destination does not have enough memory to hold it completely.即使在strcpy()的情况下,你也不能使用“Iambanananananananananananana”。在这种情况下,它将创建内存溢出,因为目标没有足够的内存来完全保存它。
-
Use proper format specifiers. in your
printf()
statements, most of the arguments to%i
are not of typeint
. For pointer type arguments, you should be using%p
, atleast. Otherwise, it will be UB.使用正确的格式说明符。在你的printf()语句中,%i的大多数参数都不是int类型。对于指针类型参数,您应该至少使用%p。否则,它将是UB。
#2
2
RESERV[i]="Iambananananananananananana";
This is not the proper way to populate this string after allocating it. You are overwriting the value of that pointer.
这不是在分配后填充此字符串的正确方法。您正在覆盖该指针的值。
strncpy(RESERV[i], "Iambananananananananananana", 15);
RESERV[i][15] = 0;
This is what you're looking for.
这就是你要找的东西。
Notice, too, that you are apparently to assign something into that pointer that is larger than the memory that you have allocated.
另请注意,您显然是在指针中指定了比您已分配的内存大的内容。
#3
0
The line
RESERV[i]="Iambananananananananananana";
is incorrect - You need to do
是不正确的 - 你需要这样做
strncpy(RESERV[i], "Iambananananananananananana", 15);
RESERVp[i][15] = 0;