指向指针的realloc问题 [英] Pointer-to-pointer realloc problem

查看:46
本文介绍了指向指针的realloc问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!


我是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检测到双重免费或损坏(输出)

错误。

其中''我的错误?

非常感谢!

解决方案

marvinla写道,2007年7月7日13:29:


你好!


我是C的初学者,我遇到了麻烦一个指向指针的指针

重新分配。

这段代码效果很好,但是Valkyrie警告一些部分(指向下面的
),以及是
打破我的真实代码。


#include< stdio.h>

#include< stdlib.h>


int main(){


int i;

int ** p =(int **) calloc(2,sizeof(int *));



松散演员阵容,你不需要它,而且还有一件事是错误的。

错误。

int ** p = calloc(2,sizeof(int *));

另外,为什么还要担心类型何时可以让编译器

这样做,这是另一回事。

int ** p = calloc(2,sizeof * p);

这分配了足够的空间两个指针并将其设置为所有位0.

为什么在立即初始化时使用calloc?

int ** p = malloc(2 * sizeof * p);

看,简单得多。


那么你需要检查一下呼叫是否成功,即检查p是否它是否b / b
是null。


for(i = 0; i< 2; i ++)

*(p + i)=(int *) calloc(1,sizeof(int));



以上所有评论均适用。

*(p + i)= malloc(1 * sizeof ** p);

或者更简单

p [i] = malloc(1 * sizeof * p [i]);


*(*(p + 0)+0)= 0;

*(*(p + 0)+1)= 1; //无效写入大小4



当然。你明确地为ONE int分配空间,是什么让你

认为你可以写第二个?从这一点开始,所有投注都已关闭。


< snip>


在我的实际代码中,glibc检测到 ;双重免费或腐败(退出)

错误。



你很幸运。


哪里'是我的错?

非常感谢!



请参阅上面的前几个错误。我没有检查你剩下的代码。

-

Flash Gordon


Flash Gordon,谢谢你的回复!

我重写了我的示例代码和我的真实代码。抱歉愚蠢

错误。

但是Valkyrie仍警告我,我的真实代码仍然破碎。对不起

重新发布所有代码。


#include< stdio.h>

#include< stdlib.h> ;


int main(){


int i;

int ** p = malloc(2 * sizeof * p);


for(i = 0; i< 2; i ++)

p [i] = malloc(2 * sizeof ** p );


p [0] [0] = 0;

p [0] [1] = 1;

p [1 ] [0] = 2;

p [1] [1] = 3;


printf("%d \ n",p [0 ] [0]);

printf("%d \ n",p [0] [1]);

printf("%d \ n",p [1] [0]);

printf("%d \ n",p [1] [1]);


p = realloc(p,3);


if(p == NULL){

printf(" OUT OF MEMORY!\ n" );

退出(1);

}


p [2] = malloc(2 * sizeof ** p); //无效写入大小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


free(p [0]); //无效读取大小4

free(p [1]); //无效读取大小4

free(p [2]); //无效读取大小4


免费(p);

返回0;


}


我想我并不理解realloc函数的行为:(。

非常感谢!


7月4日下午6:22,Flash Gordon< s ... @ flash-gordon.me.ukwrote:


marvinla写道,在04/07/07 13:29:


你好!


我是C的初学者,我在指针指针上遇到麻烦

重新分配。

这段代码效果很好,但是Valkyrie警告一些部分(指向

以下),并且

打破我的真实代码。


#include< stdio.h>

#include< stdlib.h>


int main(){


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。


for(i = 0; i< 2; i ++)

*(p + i)=(int *) calloc(1,sizeof(int));



以上所有评论均适用。

*(p + i)= malloc(1 * sizeof ** p);

或者更简单

p [i] = malloc(1 * sizeof * p [i]);


*( *(p + 0)+0)= 0;

*(*(p + 0)+1)= 1; //无效写入大小4



当然。你明确地为ONE int分配空间,是什么让你

认为你可以写第二个?从这一点开始,所有投注都已关闭。


< snip>


在我的实际代码中,glibc检测到 ;双重免费或腐败(退出)

错误。



你很幸运。


哪里'是我的错?

非常感谢!



请参阅上面的前几个错误。我没有检查你的其余代码。

-



在其余的代码中...


p =(int **)realloc(p,3);



这里只重新分配三个字节。它似乎没有像你想要的那样为3个指针分配足够的内存。如果你想要为3个整数指针分配
,那么就这样做:


p = realloc(p,3 * sizeof * p);


*(p + 2)=(int *)calloc(1,sizeof(int)); //无效写入大小4



这是因为*(p + 2)可能不属于您的程序,您应该

分配正如上所述。


*(*(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



以前的评论也适用于上述声明。


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!

解决方案

marvinla wrote, On 04/07/07 13:29:

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 *));

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.

for (i = 0; i < 2; i++)
*(p+i) = (int *)calloc(1, sizeof(int));

All comments above apply.
*(p+i) = malloc(1 * sizeof **p);
Or, more simply
p[i] = malloc(1 * sizeof *p[i]);

*(*(p+0)+0) = 0;
*(*(p+0)+1) = 1; // invalid write of size 4

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>

In my real code, glibc detects the "double free or corruption (out)"
error.

You were lucky.

Where''s my mistake?
Thanks a lot!

See above for the first few errors. I''ve not checked the rest of your code.
--
Flash Gordon


Flash Gordon, thanks the reply!
I rewrote my sample code, and my real code. Sorry for the dumbs
mistakes.
But Valkyrie still warns me and my real code is still broken. Sorry
for reposting all the code.

#include <stdio.h>
#include <stdlib.h>

int main() {

int i;
int **p = malloc(2 * sizeof *p);

for (i = 0; i < 2; i++)
p[i] = malloc(2 * sizeof **p);

p[0][0] = 0;
p[0][1] = 1;
p[1][0] = 2;
p[1][1] = 3;

printf("%d\n", p[0][0]);
printf("%d\n", p[0][1]);
printf("%d\n", p[1][0]);
printf("%d\n", p[1][1]);

p = realloc(p, 3);

if (p == NULL) {
printf("OUT OF MEMORY!\n");
exit(1);
}

p[2] = malloc(2 * sizeof **p); //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;

}

I think I didn''t understand the behavior of the realloc function :(.
Thanks a lot!


On Jul 4, 6:22 pm, Flash Gordon <s...@flash-gordon.me.ukwrote:

marvinla wrote, On 04/07/07 13:29:

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 *));


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.

for (i = 0; i < 2; i++)
*(p+i) = (int *)calloc(1, sizeof(int));


All comments above apply.
*(p+i) = malloc(1 * sizeof **p);
Or, more simply
p[i] = malloc(1 * sizeof *p[i]);

*(*(p+0)+0) = 0;
*(*(p+0)+1) = 1; // invalid write of size 4


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>

In my real code, glibc detects the "double free or corruption (out)"
error.


You were lucky.

Where''s my mistake?
Thanks a lot!


See above for the first few errors. I''ve not checked the rest of your code.
--

In the rest of the code...

p = (int **)realloc(p, 3);

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);

*(p+2) = (int *)calloc(1, sizeof(int)); // invalid write of size 4

That is because *(p+2) may not belong to your program, you should
allocate properly as stated above.

*(*(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

Previous comment applies for the above statements too.


这篇关于指向指针的realloc问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆