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

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

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的行,编译就没有问题

  1. pokemon_t * createPkmDatabase( int taille){
  2. printf("taille %d",taille);
  3. pokemon_t *tableau=malloc(sizeof(pokemon_t)*taille);
  4. ...
  5. ...
  6. }
  1. int main(){
  2. /*"taille" means size in French */
  3. int taille=5;
  4. printf("Saisir taille : ");
  5. scanf("%d",&taille); /* <-- BREAKS EVERYTHING */
  6. printf("valeur de taille : %d\n ",taille);
  7. pokemon_t *database=createPkmDatabase(taille);
  8. }

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

  1. pokemon_t * createPkmDatabase( int taille){
  2. printf(&quot;taille %d&quot;,taille);
  3. pokemon_t *tableau=malloc(sizeof(pokemon_t)*taille);
  4. ...
  5. ...
  6. }
  1. int main(){
  2. /*&quot;taille&quot; means size in french */
  3. int taille=5;
  4. printf(&quot;Saisir taille : &quot;);
  5. scanf(&quot;%d&quot;,&amp;taille); /* &lt;-- BREAKS EVERYTHING */
  6. printf(&quot;valeur de taille : %d\n &quot;,taille);
  7. pokemon_t *database=createPkmDatabase(taille);
  8. }

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. 在展示的代码中没有错误。
  2. 一些建议:
  3. 1. 对于大小,请使用正确的类型(`size_t`
  4. 2. 始终检查 `scanf` `malloc` 的结果
  5. typedef struct {
  6. int x, y, z;
  7. } pokemon_t;
  8. pokemon_t *createPkmDatabase(size_t taille){
  9. printf("taille %d", taille);
  10. pokemon_t *tableau = malloc(sizeof(*tableau) * taille);
  11. return tableau;
  12. }
  13. int main(){
  14. /* "taille" 在法语中意味着大小 */
  15. size_t taille;
  16. printf("Saisir taille : ");
  17. if(scanf("%zu", &taille) == 1)
  18. {
  19. printf("valeur de taille : %zu\n ", taille);
  20. pokemon_t *database = createPkmDatabase(taille);
  21. if(database)
  22. {
  23. printf("\nAllocation OK\n");
  24. }
  25. else
  26. {
  27. printf("\nAllocation FAILED\n");
  28. }
  29. free(database);
  30. }
  31. }
英文:

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
  1. typedef struct {
  2. int x,y,z;
  3. }pokemon_t;
  4. pokemon_t * createPkmDatabase(size_t taille){
  5. printf(&quot;taille %d&quot;,taille);
  6. pokemon_t *tableau=malloc(sizeof(*tableau)*taille);
  7. return tableau;
  8. }
  9. int main(){
  10. /*&quot;taille&quot; means size in french */
  11. size_t taille;
  12. printf(&quot;Saisir taille : &quot;);
  13. if(scanf(&quot;%zu&quot;,&amp;taille) == 1)
  14. {
  15. printf(&quot;valeur de taille : %zu\n &quot;,taille);
  16. pokemon_t *database=createPkmDatabase(taille);
  17. if(database)
  18. {
  19. printf(&quot;\nAllocation OK\n&quot;);
  20. }
  21. else
  22. {
  23. printf(&quot;\nAllocation FAILED\n&quot;);
  24. }
  25. free(database);
  26. }
  27. }

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:

确定