gpt4 book ai didi

c - 函数中全局变量的malloc和realloc

转载 作者:太空宇宙 更新时间:2023-11-04 03:32:59 24 4
gpt4 key购买 nike

我试图以两种不同的方式通过动态分配从标准输入读取输入:

1) 我从 stdin、malloc、realloc 读取并写入 main 中的数组。然后当我打印数组的那些元素时,一切正常。

2) 我在 main 中声明全局变量,然后将其放入函数中,在该函数中我从 stdin、malloc、realloc 读取并写入该数组。当我在函数中打印这些元素时,它工作正常,但当我在函数之外执行此操作时,它会给我段错误。

你能检查这些代码并帮助我解决这个问题吗?

2)

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

/*--------------------------------------------------------------------------*/

void nacitaj( int *pocet_slov, char **slovnik ){

char buffer[60];
int size = 60;

while( fgets( buffer, size, stdin ) != NULL ){
*pocet_slov = *pocet_slov + 1;
slovnik = (char**) realloc( slovnik, (*pocet_slov+1) * sizeof(char*) );
slovnik[*pocet_slov-1] = (char*) malloc( strlen(buffer) * sizeof(char) );
buffer[strlen(buffer)-1] = '\0';
strcpy( slovnik[*pocet_slov-1], buffer );
}

for( int i = 0; i < *pocet_slov; i ++ ){
printf( "%d. %s\n", i+1, slovnik[i] );
}
}

/*---------------------------------------------------------------------------*/

int main(){
char **slovnik = NULL;

int pocet_slov = 0;

int i;

printf( "Zadavaj slova do slovniku:\n" );
nacitaj( &pocet_slov, slovnik );

for( i = 0; i < pocet_slov; i ++ ){
printf( "%d. %s\n", i+1, slovnik[i] );
}

return 0;
}

1)

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

int main(){
char **slovnik = NULL;

int pocet_slov = 0;

char buffer[60];
int size = 60;
int i;

printf( "Zadavaj slova do slovniku:\n" );

while( fgets( buffer, size, stdin ) != NULL ){
pocet_slov ++;
slovnik = (char**) realloc( slovnik, (pocet_slov+1) * sizeof(char*) );
slovnik[pocet_slov-1] = (char*) malloc( strlen(buffer) * sizeof(char) );
buffer[strlen(buffer)-1] = '\0';
strcpy( slovnik[pocet_slov-1], buffer );
}

for( i = 0; i < pocet_slov; i ++ ){
printf( "%d. %s\n", i+1, slovnik[i] );
}

return 0;
}

输入示例:

car
bird
pen

最佳答案

您应该将 slovnik 的地址传递给您的 nacitaj 函数。否则,nacitaj 中变量的更新在 main 中不可见。

另外,永远记得释放任何动态分配的内存。

void nacitaj( int *pocet_slov, char ***slovnik ){

char buffer[60];
int size = 60;

while( fgets( buffer, size, stdin ) != NULL ){
*pocet_slov = *pocet_slov + 1;
// dereference slovnik to update the variable in main
// don't cast the return value of malloc or realloc
*slovnik = realloc( *slovnik, (*pocet_slov+1) * sizeof(char*) );
(*slovnik)[*pocet_slov-1] = (char*) malloc( strlen(buffer) * sizeof(char) );
buffer[strlen(buffer)-1] = '\0';
strcpy( (*slovnik)[*pocet_slov-1], buffer );
}

for( int i = 0; i < *pocet_slov; i ++ ){
printf( "%d. %s\n", i+1, (*slovnik)[i] );
}
}

int main(){
char **slovnik = NULL;

int pocet_slov = 0;

int i;

printf( "Zadavaj slova do slovniku:\n" );
// pass the address of slovnik
nacitaj( &pocet_slov, &slovnik );

for( i = 0; i < pocet_slov; i ++ ){
printf( "%d. %s\n", i+1, slovnik[i] );
}

// free the allocated memory
for( i = 0; i < pocet_slov; i ++ ){
free(slovnik[i]);
}
free(slovnik);

return 0;
}

关于c - 函数中全局变量的malloc和realloc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34769986/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com