将指针传递给指针并重新分配 space
Passing pointer to pointer and reallocate space
我post这个示例代码是为了说明我的问题。我试图在函数之间传递一个指向整数的指针。如果你编译 & 运行 这段代码,你会看到奇怪的数字出现,我不明白为什么。我认为 realloc()
的使用不当。如果有人可以提供提示或将我重定向到与此类似的一些问题,我将不胜感激。我搜索了这个问题,但找不到任何类似的问题。
#include <stdio.h>
#include <stdlib.h>
void myFunction(int **output);
int main(){
int *indices;
myFunction(&indices);
printf("{");
for(int i=0;i<10;i++){//suppose also for some mysterious reasons that I know min_size in the main
printf("%i,", indices[i]);
}
printf("}\n");
return 0;
}
void myFunction(int **output){
int size = 130;//allocating big amount of space
int* indices = malloc(sizeof(int)*size);
//...start doing mysterious stuffs....
int min_size = 10;
for(int i=0;i<min_size;i++){
indices[i] = i;//just for saving something
}
//...end doing mysterious stuffs...
//now for some reasons I know I need only min_size elements and all other are wasting space so I reallocate
indices = realloc(indices,min_size);//resizing in order to save space
*output = indices;
}
您没有正确使用 realloc。重新分配范围的大小必须是
min_size * sizeof( int )
给你
#include <stdio.h>
#include <stdlib.h>
void myFunction(int **output);
int main( void )
{
int *indices;
myFunction(&indices);
printf("{");
for(int i=0;i<10;i++){//suppose also for some mysterious reasons that I know min_size in the main
printf("%i,", indices[i]);
}
printf("}\n");
return 0;
}
void myFunction(int **output){
int size = 130;//allocating big amount of space
int* indices = malloc(sizeof(int)*size);
*output = indices;
// ^^^^^^^^^^^^^^^
//...start doing mysterious stuffs....
int min_size = 10;
for(int i=0;i<min_size;i++){
indices[i] = i;//just for saving something
}
//...end doing mysterious stuffs...
//now for some reasons I know I need only min_size elements and all other are wasting space so I reallocate
indices = realloc(indices, min_size * sizeof( int ) );//resizing in order to save space
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
if ( indices ) *output = indices;
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
}
程序输出为
{0,1,2,3,4,5,6,7,8,9,}
我post这个示例代码是为了说明我的问题。我试图在函数之间传递一个指向整数的指针。如果你编译 & 运行 这段代码,你会看到奇怪的数字出现,我不明白为什么。我认为 realloc()
的使用不当。如果有人可以提供提示或将我重定向到与此类似的一些问题,我将不胜感激。我搜索了这个问题,但找不到任何类似的问题。
#include <stdio.h>
#include <stdlib.h>
void myFunction(int **output);
int main(){
int *indices;
myFunction(&indices);
printf("{");
for(int i=0;i<10;i++){//suppose also for some mysterious reasons that I know min_size in the main
printf("%i,", indices[i]);
}
printf("}\n");
return 0;
}
void myFunction(int **output){
int size = 130;//allocating big amount of space
int* indices = malloc(sizeof(int)*size);
//...start doing mysterious stuffs....
int min_size = 10;
for(int i=0;i<min_size;i++){
indices[i] = i;//just for saving something
}
//...end doing mysterious stuffs...
//now for some reasons I know I need only min_size elements and all other are wasting space so I reallocate
indices = realloc(indices,min_size);//resizing in order to save space
*output = indices;
}
您没有正确使用 realloc。重新分配范围的大小必须是
min_size * sizeof( int )
给你
#include <stdio.h>
#include <stdlib.h>
void myFunction(int **output);
int main( void )
{
int *indices;
myFunction(&indices);
printf("{");
for(int i=0;i<10;i++){//suppose also for some mysterious reasons that I know min_size in the main
printf("%i,", indices[i]);
}
printf("}\n");
return 0;
}
void myFunction(int **output){
int size = 130;//allocating big amount of space
int* indices = malloc(sizeof(int)*size);
*output = indices;
// ^^^^^^^^^^^^^^^
//...start doing mysterious stuffs....
int min_size = 10;
for(int i=0;i<min_size;i++){
indices[i] = i;//just for saving something
}
//...end doing mysterious stuffs...
//now for some reasons I know I need only min_size elements and all other are wasting space so I reallocate
indices = realloc(indices, min_size * sizeof( int ) );//resizing in order to save space
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
if ( indices ) *output = indices;
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
}
程序输出为
{0,1,2,3,4,5,6,7,8,9,}