使用scanf定义大小时出现分段错误

huangapple go评论67阅读模式
英文:

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(&quot;taille %d&quot;,taille);
  pokemon_t *tableau=malloc(sizeof(pokemon_t)*taille);
  ...
  ...
}

int main(){
/*&quot;taille&quot; means size in french */
    int taille=5;
    printf(&quot;Saisir taille : &quot;); 
    scanf(&quot;%d&quot;,&amp;taille); /* &lt;-- BREAKS EVERYTHING */
    printf(&quot;valeur de taille : %d\n &quot;,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:

  1. Use the correct type for sizes (size_t)
  2. Always check the result of scanf and malloc
typedef struct {
    int x,y,z;
}pokemon_t; 

pokemon_t * createPkmDatabase(size_t taille){
  printf(&quot;taille %d&quot;,taille);
  pokemon_t *tableau=malloc(sizeof(*tableau)*taille);
  return tableau;
}

int main(){
/*&quot;taille&quot; means size in french */
    size_t taille;
    printf(&quot;Saisir taille : &quot;); 
    if(scanf(&quot;%zu&quot;,&amp;taille) == 1)
    { 
        printf(&quot;valeur de taille : %zu\n &quot;,taille);
        pokemon_t *database=createPkmDatabase(taille);
        if(database)
        {
            printf(&quot;\nAllocation OK\n&quot;);
        }
        else
        {
            printf(&quot;\nAllocation FAILED\n&quot;);
        }
        free(database);
    }
}

https://godbolt.org/z/ExjYe5G94

huangapple
  • 本文由 发表于 2023年2月19日 10:06:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497575.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定