c - 我不断收到错误 "stack smashing detected"

标签 c stack-smash

我正在用 C 语言编程并使用 gcc 进行编译。 每次编译时,我都会收到堆栈粉碎检测到的错误。 这是什么意思以及如何解决它?

#include <stdio.h>

#define MAX_ARRAY 50


static int getScore(int assignmentNum[], int weight[], int daysLate[], float score[]){

  int position, tempWeight, tempLate;
  float tempScore;

  scanf("%d %f %d %d", &position, &tempScore, &tempWeight, &tempLate);
  score[position] = tempScore;
  weight[position] = tempWeight;
  daysLate[position] = tempLate;
  assignmentNum[position] = position;
  return weight[position];
}


int main(void){

  int penalty_points,  num_drop, num_assignment;
  static int assignmentNum[MAX_ARRAY], daysLate[MAX_ARRAY], weight[MAX_ARRAY];
  static float score[MAX_ARRAY];
  char statGen;
  int total = 0;

  scanf("%d %d %s", &penalty_points, &num_drop, &statGen);
  printf("%d\n", penalty_points);

  while (total <  100) {
    total = total + getScore(assignmentNum, weight, daysLate, score);
  }    
  return 0;
}

最佳答案

您将 %s&statGen 一起使用。 %s 说明符用于字符串;相应的参数必须指向一个具有足够空间容纳字符串的缓冲区,包括终止空字符。但是,statGen 是单个char

要仅读取单个字符,请使用"%c" 而不是"%s"。如果要跳过字符前的空格,请使用"%c"。空格要求 scanf 跳过空格。

关于c - 我不断收到错误 "stack smashing detected",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19052863/

相关文章:

c - 反转表的系数

c - *** 检测到堆栈粉碎 *** 中止(核心转储)

c - 缓冲区溢出 - 普通用户的 SegFaults

node.js - websocket.io 正在引发未定义错误的方法

c - 利用缓冲区漏洞时从单独的函数返回地址

c - 写入后文件中的垃圾值

c - C 戏剧节目问题

c++ - 打开/dev/ttyUSB0 返回 "Bad file descriptor"

c - 如何检测 C 中的运行时堆栈粉碎/缓冲区溢出并使 gdb 能够以正确的消息正常退出。

我可以使用 **i 而不是 *i[20] 来初始化二维数组吗?