首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >总线错误(核转储)错误(ubuntu) C程序

总线错误(核转储)错误(ubuntu) C程序
EN

Stack Overflow用户
提问于 2018-03-13 19:32:40
回答 2查看 6.8K关注 0票数 0

我有一个程序,用户输入他们想要的句子,然后被提示输入句子。他们不能输入超过十个句子。当我测试时,如果我输入超过5,我会得到一个总线错误代码。这是我的代码,我在.h文件中调用两个函数,我认为这些函数不相关,但无论如何我都会提供它们。

代码语言:javascript
运行
复制
//this program will take in user input for a number of sentences  up to ten, each with 100 char or less, and convert them all to uppercase

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


int main () {

int i; //increment
int numberofsentences; //number of sentences
char **sentence_array; // sentence array

sentence_array = (char **)malloc(numberofsentences*sizeof(char));

printf ("I will take up to ten sentences of up to 100 chars each, and convert them to uppercase, as well as give you stats on them. \n");
printf ("How many sentences would you like? \n");

scanf ("%d", &numberofsentences);
fgetc(stdin);


//user inputs the sentences based on the number that was provided
for (i = 0;  i< numberofsentences; i++) {
    sentence_array[i] = (char *)malloc(100 * sizeof(char));
    printf ("Enter sentence :");
    fgets(sentence_array[i], 101, stdin);
    printf ("\n");
      }

//call function that converts all arrays to uppercase
convertAll(sentence_array, numberofsentences);

printf("Your converted sentences are: \n");


//print out every sentence thats converted to uppercase
for (i = 0; i < numberofsentences; i++){
    //convertSentence(sentence_array[i]);
    printf("%s", sentence_array[i]);
       }
}

和函数.c文件

代码语言:javascript
运行
复制
#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include "convert.h"

//function to convert char array to uppercase
void convertSentence(char *sentence){
        int i;
        for (i = 0; i <strlen(sentence); i++){
                sentence[i] = toupper(sentence[i]);
        }
}

//function to convert array of all sentences and converts all to upper
void convertAll(char **sentenceList, int numOfSentences){
        int i;
        for (i = 0; i < numOfSentences; i++){
                convertSentence(sentenceList[i]);
        }
}

我觉得这和内存分配有关。我知道扫描和飞碟是很糟糕的,但是我的教授告诉我们用它们.谢谢你的帮助

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-03-13 19:36:31

这是预期的,因为

代码语言:javascript
运行
复制
sentence_array = (char **)malloc(numberofsentences*sizeof(char));

没有为numberofsentences指针分配足够的内存。因此,在存储了几个句子之后,您会得到内存冲突或任何其他UB。

sizeof是错误的,它应该是:

代码语言:javascript
运行
复制
sentence_array = malloc(numberofsentences*sizeof(char *));

旁白:不需要演员:Do I cast the result of malloc? (答案是否定的,BTW)

编辑:我的答案是不完整的,H.S.指出了问题的另一部分(未初始化的值),可以很容易地通过以下方法避免:

  • 启用警告
  • 读它们

(加上另一个fgets边界错误)

这将教会我如何尝试手动执行操作代码。

票数 1
EN

Stack Overflow用户

发布于 2018-03-13 19:57:12

在本声明中:

代码语言:javascript
运行
复制
sentence_array = (char **)malloc(numberofsentences*sizeof(char));

在初始化局部变量numberofsentences之前,要使用它,这是一种未定义的行为。另外,sentence_array应该分配char指针数组的内存,即numberofsentences*sizeof(char *),并将该语句移到numberofsentences input语句下面。所以,应该是这样:

代码语言:javascript
运行
复制
scanf ("%d", &numberofsentences);
fgetc(stdin);
sentence_array = malloc(numberofsentences*sizeof(char *));

[您不需要强制转换malloc结果]

还有,这里

代码语言:javascript
运行
复制
fgets(sentence_array[i], 101, stdin);
                         ^^^

可能会发生缓冲区溢出。sentence_array[i]已被分配给内存以保存100字符,并且您将101作为要复制到sentence_array[i]的最大字符数。fgets()从流中读取字符,并将它们存储到缓冲区(这里是sentence_array[i])中,直到(num-1) (此处,101-1)字符被读取,或者到达换行符或文件末尾,无论哪种情况先发生,在将字符复制到缓冲区后,自动追加终止空字符。因此,它可能会导致缓冲区溢出。它应该是:

代码语言:javascript
运行
复制
fgets(sentence_array[i], 100, stdin);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49264460

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档