重新分配到一个函数 [英] Realloc into a function
问题描述
以下代码正常工作(没有警告):
#include< stdio.h>
#include< stdlib.h>
int main()
{
int num = 10;
int * vet;
int i;
(i = 0; i {
/ *将vet的内存分配给包含(i + 1)int * /
vet =(int *)realloc(vet,(i + 1)* sizeof(int));
/ *在分配的内存中写入数字* /
vet [i] = 321 + i;
}
/ *打印测试,如果所有的作品我必须看到:
| 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | * /
printf(|); (i = 0; i printf(%d |,vet [i])为
;
printf(\\\
);
返回0;
}
但是具有函数的相同程序不起作用!编译器返回以下警告:
$ p $ 在函数'main'中:
14:10:warning:'vet '在此函数中未初始化使用[-Wuninitialized]
代码是:
#include< stdio.h>
#include< stdlib.h>
void memoria(int *,int);
int main()
{
int * vet,num = 10;
memoria(vet,num);
/ *打印测试,如果所有的作品我必须看到:
| 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | * /
int i;
printf(|); (i = 0; i printf(%d |,vet [i])为
;
printf(\\\
);
返回0;
}
void memoria(int * vet,int num)
{
int i;
(i = 0; i {
/ *将vet的内存分配给包含(i + 1)int * /
vet =(int *)realloc(vet,(i + 1)* sizeof(int));
/ *在分配的内存中写入数字* /
vet [i] = 321 + i;
}
}
有人会说我为什么?非常感谢!
哦,和主函数中的'random'malloc相同的代码(带函数)...
#include< stdio.h>
#include< stdlib.h>
void memoria(int *,int);
int main()
{
int * vet,num = 10;
/ * ADDED MALLOC * /
vet =(int *)malloc(1);
memoria(vet,num);
/ *打印测试,如果所有的作品我必须看到:
| 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | * /
int i;
printf(|); (i = 0; i printf(%d |,vet [i])为
;
printf(\\\
);
返回0;
}
void memoria(int * vet,int num)
{
int i;
(i = 0; i {
/ *将vet的内存分配给包含(i + 1)int * /
vet =(int *)realloc(vet,(i + 1)* sizeof(int));
/ *在分配的内存中写入数字* /
vet [i] = 321 + i;
}
}
你有这样写的方式:
int * vet,num = 10;
memoria(vet,num);
您在 memoria
没有被送回到 main
。为了理解这一点,为了好玩,改变 num
而不是 memoria
的值,然后在主
。 main中的值仍然是 10
。变量 vet
和 num
按值传递,这样它们的原始值保持在 main 。
最常见的两种方法是传递 vet
在 memoria
中可以修改它,或者为 vet
返回一个新值。
第一种形式如下所示:
memoria(& vet,num);
void memoria(int ** vet,int num)
{
* vet = realloc(* vet,...);
或者,您可以更改 memoria $ c的返回类型$ c $,
$ b
vet = memoria(vet,num);
int * memoria(int * vet,int num)
{
...
return vet;
}
他们都有自己的优点和缺点。第二种形式对于非指针头的人来说可能更容易遵循。
malloc / realloc fluke
如果您将 printf
s添加到 main
,您最后一个示例的工作原理很容易看出。 >和 memoria
来显示 vet
的值。如果可以的话, realloc()
将新内存放在与旧指针相同的位置。在你简单的测试用例中,分配器很容易做到,所以内存保持在同一个位置。如果代码更复杂,并且对 realloc()
的调用移动了指针,则会在 main
之后。
My question is about 'realloc'. The following code works correctly (with no warning):
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int num=10;
int *vet;
int i;
for (i=0; i<num; i++)
{
/* allocate memory of vet to contains (i+1) int */
vet = (int*) realloc ( vet, (i+1) * sizeof(int) );
/* write numbers in the allocated memory */
vet[i] = 321 + i;
}
/* print test, if all works I must see:
| 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | */
printf ("| ");
for (i=0; i<num; i++)
printf ("%d | ", vet[i]);
printf ("\n");
return 0;
}
But the same program with a function doesn't work! And the compiler return the following warning:
In function ‘main’:
14:10: warning: ‘vet’ is used uninitialized in this function [-Wuninitialized]
The code is:
#include <stdio.h>
#include <stdlib.h>
void memoria (int *, int);
int main ()
{
int *vet, num=10;
memoria (vet, num);
/* print test, if all works I must see:
| 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | */
int i;
printf ("| ");
for (i=0; i<num; i++)
printf ("%d | ", vet[i]);
printf ("\n");
return 0;
}
void memoria (int *vet, int num)
{
int i;
for (i=0; i<num; i++)
{
/* allocate memory of vet to contains (i+1) int */
vet = (int*) realloc ( vet, (i+1) * sizeof(int) );
/* write numbers in the allocated memory */
vet[i] = 321 + i;
}
}
Someone can say me why? Thank you very much!
Oh, and the same code with a 'random' malloc in main works (with the function)...
#include <stdio.h>
#include <stdlib.h>
void memoria (int *, int);
int main ()
{
int *vet, num=10;
/* ADDED MALLOC */
vet = (int*) malloc (1);
memoria (vet, num);
/* print test, if all works I must see:
| 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | */
int i;
printf ("| ");
for (i=0; i<num; i++)
printf ("%d | ", vet[i]);
printf ("\n");
return 0;
}
void memoria (int *vet, int num)
{
int i;
for (i=0; i<num; i++)
{
/* allocate memory of vet to contains (i+1) int */
vet = (int*) realloc ( vet, (i+1) * sizeof(int) );
/* write numbers in the allocated memory */
vet[i] = 321 + i;
}
}
The way you have this written:
int *vet, num=10;
memoria (vet, num);
The changes you make inside of memoria
do not get sent back to main
. To understand this, just for fun change the value of num
instead of memoria
and then check it back in main
. The value in main will still be 10
. The variables vet
and num
are passed by value so that they keep their original values in main
.
The two most common ways around this are to pass the address of vet
in so memoria
can modify it, or return a new value for vet
.
The first form looks like this:
memoria( & vet, num ) ;
void memoria (int **vet, int num)
{
* vet= realloc( * vet, ... ) ;
Or, instead you can change the return type of memoria
,
vet= memoria( vet, num ) ;
int * memoria( int * vet, int num)
{
...
return vet ;
}
They both have their pros and cons. The second form is probably easier for people who aren't pointerheads to follow.
malloc/realloc fluke
The reason your last example works is easy to see if you add in printf
s to main
and memoria
to show the value of vet
. If it can, realloc()
puts the new memory at the same location as the old pointer. In your simple test case, the allocator has an easy time doing so, and so the memory stays at the same location. If the code was more complex, and the call to realloc()
moved the pointer, you'd be seeing a crash in main
after.
这篇关于重新分配到一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!