问题描述
您好!
我是C的初学者,我在指针指针上遇到麻烦
重新分配。
这段代码效果很好,但Valkyrie警告一些部分(下面指出
),并且打破我的真实代码是
。
#include< stdio.h>
#include< stdlib.h>
int main(){
int i;
int ** p =(int **)calloc(2,sizeof(int *));
for(i = 0; i< 2; i ++)
*(p + i)=(int *)calloc(1,sizeof(int));
*(*(p + 0)+0)= 0;
*(*(p + 0)+1)= 1; //无效写入大小4
*(*(p + 1)+0)= 2;
*(*(p + 1)+1)= 3 ; //无效写入大小4
printf("%d \ n",*(*(p + 0)+0));
printf("%d \ n",*(*(p + 0)+1)); //无效读取大小4
printf("%d \ n",*(*(p + 1)+0));
printf(" ;%d \ n,*(*(p + 1)+1)); //无效读取大小4
p =(int **)realloc(p,3);
*(p + 2)=(int * )calloc(1,sizeof(int)); //无效写入大小4
*(*(p + 2)+0)= 4; //无效读取大小4
*(*(p + 2)+1)= 5; //无效读取大小4
printf("%d \ n",*(*(p + 2)+0)); //无效读取大小4
printf("%d \ n",*(*(p + 2)+1)); //无效读取大小4
免费(*(p + 0)); //无效读取大小4
免费(*(p + 1)); //无效读取大小4
免费(*(p + 2)); //无效读取大小4
免费(p);
返回0;
}
在我的实际代码中,glibc检测到双重免费或损坏(输出)
错误。
其中''我的错误?
非常感谢!
松散演员阵容,你不需要它,而且还有一件事是错误的。
错误。
int ** p = calloc(2,sizeof(int *));
另外,为什么还要担心类型何时可以让编译器
这样做,这是另一回事。
int ** p = calloc(2,sizeof * p);
这分配了足够的空间两个指针并将其设置为所有位0.
为什么在立即初始化时使用calloc?
int ** p = malloc(2 * sizeof * p);
看,简单得多。
那么你需要检查一下呼叫是否成功,即检查p是否它是否b / b
是null。
以上所有评论均适用。
*(p + i)= malloc(1 * sizeof ** p);
或者更简单
p [i] = malloc(1 * sizeof * p [i]);
当然。你明确地为ONE int分配空间,是什么让你
认为你可以写第二个?从这一点开始,所有投注都已关闭。
< snip>
你很幸运。
请参阅上面的前几个错误。我没有检查你剩下的代码。
-
Flash Gordon
int i;
int ** p =(int **)calloc(2,sizeof(int *));
放弃演员阵容,你不需要它在C中,而且还有一件事是错误的。
错误。
int ** p = calloc(2,sizeof(int *));
另外,为什么还要担心类型何时可以让编译器
这样做,这是另一回事。
int ** p = calloc(2,sizeof * p);
这分配了足够的空间两个指针并将其设置为所有位0.
为什么在立即初始化时使用calloc?
int ** p = malloc(2 * sizeof * p);
看,简单得多。
那么你需要检查一下呼叫是否成功,即检查p是否它是否b / b
是null。
以上所有评论均适用。
*(p + i)= malloc(1 * sizeof ** p);
或者更简单
p [i] = malloc(1 * sizeof * p [i]);
当然。你明确地为ONE int分配空间,是什么让你
认为你可以写第二个?从这一点开始,所有投注都已关闭。
< snip>
你很幸运。
请参阅上面的前几个错误。我没有检查你的其余代码。
-
在其余的代码中...
这里只重新分配三个字节。它似乎没有像你想要的那样为3个指针分配足够的内存。如果你想要为3个整数指针分配
,那么就这样做:
p = realloc(p,3 * sizeof * p);
这是因为*(p + 2)可能不属于您的程序,您应该
分配正如上所述。
以前的评论也适用于上述声明。
Hello!
I''m a beginner in C, and I''m having trouble with a pointer-to-pointer
reallocation.
This piece of code works well, but Valkyrie warns some parts (pointed
below), and is
breaking my real code.
#include <stdio.h>
#include <stdlib.h>
int main() {
int i;
int **p = (int **)calloc(2, sizeof(int *));
for (i = 0; i < 2; i++)
*(p+i) = (int *)calloc(1, sizeof(int));
*(*(p+0)+0) = 0;
*(*(p+0)+1) = 1; // invalid write of size 4
*(*(p+1)+0) = 2;
*(*(p+1)+1) = 3;// invalid write of size 4
printf("%d\n", *(*(p+0)+0));
printf("%d\n", *(*(p+0)+1)); // invalid read of size 4
printf("%d\n", *(*(p+1)+0));
printf("%d\n", *(*(p+1)+1)); // invalid read of size 4
p = (int **)realloc(p, 3);
*(p+2) = (int *)calloc(1, sizeof(int)); // invalid write of size 4
*(*(p+2)+0) = 4; // invalid read of size 4
*(*(p+2)+1) = 5; // invalid read of size 4
printf("%d\n", *(*(p+2)+0)); // invalid read of size 4
printf("%d\n", *(*(p+2)+1)); // invalid read of size 4
free(*(p+0)); // invalid read of size 4
free(*(p+1));// invalid read of size 4
free(*(p+2));// invalid read of size 4
free(p);
return 0;
}
In my real code, glibc detects the "double free or corruption (out)"
error.
Where''s my mistake?
Thanks a lot!
Loose the cast, you don''t need it in C and it is one more thing to get
wrong.
int **p = calloc(2, sizeof(int *));
Also, why bother to worry about the type when you can let the compiler
do it, it''s another thing to get wrong.
int **p = calloc(2, sizeof *p);
This allocates enough space for two pointers and sets it to all bits 0.
Why use calloc when you immediately initialise it?
int **p = malloc(2 * sizeof *p);
See, much simpler.
Then you need to check if the call succeeded, i.e. check p to see if it
is null.
All comments above apply.
*(p+i) = malloc(1 * sizeof **p);
Or, more simply
p[i] = malloc(1 * sizeof *p[i]);
Of course. You explicitly allocate space for ONE int, what makes you
think you can write a second one? From this point on all bets are off.
<snip>
You were lucky.
See above for the first few errors. I''ve not checked the rest of your code.
--
Flash Gordon
Loose the cast, you don''t need it in C and it is one more thing to get
wrong.
int **p = calloc(2, sizeof(int *));
Also, why bother to worry about the type when you can let the compiler
do it, it''s another thing to get wrong.
int **p = calloc(2, sizeof *p);
This allocates enough space for two pointers and sets it to all bits 0.
Why use calloc when you immediately initialise it?
int **p = malloc(2 * sizeof *p);
See, much simpler.
Then you need to check if the call succeeded, i.e. check p to see if it
is null.
All comments above apply.
*(p+i) = malloc(1 * sizeof **p);
Or, more simply
p[i] = malloc(1 * sizeof *p[i]);
Of course. You explicitly allocate space for ONE int, what makes you
think you can write a second one? From this point on all bets are off.
<snip>
You were lucky.
See above for the first few errors. I''ve not checked the rest of your code.
--
In the rest of the code...
You''re reallocating with only three bytes here. Its not allocating
enough memory for 3 pointers as you seem to be trying. If you''re
trying to allocate for 3 integer pointers, make it:
p = realloc(p, 3 * sizeof *p);
That is because *(p+2) may not belong to your program, you should
allocate properly as stated above.
Previous comment applies for the above statements too.
这篇关于指向指针的realloc问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!