英文:
Seg fault when using scanf to define the size
问题
I have a strange issue with my code:
我在我的代码中遇到了一个奇怪的问题:
I have a function generating a dynamic array of struct and only takes an int size as parameter.
我有一个函数生成一个结构体的动态数组,并只接受一个整数大小作为参数。
It's work well until i try to add just a scanf("d";&size); to choose the size of my array, can't understand why i get a seg fault.
一切都很正常,直到我尝试添加scanf("d";&size);来选择数组的大小,我不明白为什么会导致段错误。
Note : There is no issue with compilation if i remove the line containing scanf
注意:如果我删除包含scanf的行,编译就没有问题
pokemon_t * createPkmDatabase( int taille){
printf("taille %d",taille);
pokemon_t *tableau=malloc(sizeof(pokemon_t)*taille);
...
...
}
int main(){
/*"taille" means size in French */
int taille=5;
printf("Saisir taille : ");
scanf("%d",&taille); /* <-- BREAKS EVERYTHING */
printf("valeur de taille : %d\n ",taille);
pokemon_t *database=createPkmDatabase(taille);
}
I don't get why changing the value of "taille" with a simple scanf change anything the value.
我不明白为什么用一个简单的scanf更改"taille"的值会改变任何值。
It doesn't even seem to enter the function because it doesn't even print the value of the size.
它甚至似乎没有进入函数,因为它甚至不打印大小的值。
英文:
I have a strange issue with my code :
I have a function generating a dynamic array of struct and only takes an int size as parameter.
It's work well until i try to add just a scanf("d";&size); to choose the size of my array, can't understand why i get a seg fault.
Note : There is no issue with compilation if i remove the line containing scanf
pokemon_t * createPkmDatabase( int taille){
printf("taille %d",taille);
pokemon_t *tableau=malloc(sizeof(pokemon_t)*taille);
...
...
}
int main(){
/*"taille" means size in french */
int taille=5;
printf("Saisir taille : ");
scanf("%d",&taille); /* <-- BREAKS EVERYTHING */
printf("valeur de taille : %d\n ",taille);
pokemon_t *database=createPkmDatabase(taille);
}
I don't get why changing the value of "taille" with a simple scanf change anything the value.
It doesn't even seems to enter in the function bc it doesn't even print the value of the size
答案1
得分: 1
在展示的代码中没有错误。
一些建议:
1. 对于大小,请使用正确的类型(`size_t`)
2. 始终检查 `scanf` 和 `malloc` 的结果
typedef struct {
int x, y, z;
} pokemon_t;
pokemon_t *createPkmDatabase(size_t taille){
printf("taille %d", taille);
pokemon_t *tableau = malloc(sizeof(*tableau) * taille);
return tableau;
}
int main(){
/* "taille" 在法语中意味着大小 */
size_t taille;
printf("Saisir taille : ");
if(scanf("%zu", &taille) == 1)
{
printf("valeur de taille : %zu\n ", taille);
pokemon_t *database = createPkmDatabase(taille);
if(database)
{
printf("\nAllocation OK\n");
}
else
{
printf("\nAllocation FAILED\n");
}
free(database);
}
}
英文:
There are no errors in the code shown.
Some remarks:
- Use the correct type for sizes (
size_t
) - Always check the result of
scanf
andmalloc
typedef struct {
int x,y,z;
}pokemon_t;
pokemon_t * createPkmDatabase(size_t taille){
printf("taille %d",taille);
pokemon_t *tableau=malloc(sizeof(*tableau)*taille);
return tableau;
}
int main(){
/*"taille" means size in french */
size_t taille;
printf("Saisir taille : ");
if(scanf("%zu",&taille) == 1)
{
printf("valeur de taille : %zu\n ",taille);
pokemon_t *database=createPkmDatabase(taille);
if(database)
{
printf("\nAllocation OK\n");
}
else
{
printf("\nAllocation FAILED\n");
}
free(database);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论