简体   繁体   English

c程序的分段错误

[英]segmentation fault with c program

The program is getting input from a text file and then parsing them by space and then storing them in an array.该程序从文本文件中获取输入,然后按空格解析它们,然后将它们存储在数组中。 I'm using the "<" in linux to read in the file.I needed to create a dynamically allocated array to store the words我在 linux 中使用“<”来读入文件。我需要创建一个动态分配的数组来存储单词

if the input strings are "I am the cat in the hat" then the array should look like: index:如果输入字符串是“我是戴帽子的猫”,则数组应如下所示:索引:

   "I" "am" "the" "cat" "in" "the" "hat" 
    0    1    2     3     4    5     6

I am getting a segmentation fault and I can't figure out why I am getting the error?我遇到了分段错误,但不知道为什么会收到错误消息? I need to Any help would be appreciated.我需要任何帮助将不胜感激。

code:代码:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int main()
{

int wordCount = 1, i;
char ch,* wordsArray;

while((ch = getc(stdin)) != EOF)
    {
      if(ch == ' ') {++wordCount;}
    }

    wordsArray =(char*)malloc(wordCount * sizeof(char));

    for(i = 0; i<wordCount; i++)
     {
        scanf("%s",&wordsArray[i]);
        printf("%s\n", wordsArray[i])
     }
  return 0;
}

Edit: I have posted the actual code.编辑:我已经发布了实际代码。 I am no longer getting segmentation falls, but the words are not going in the array.我不再得到分段下降,但单词不在数组中。

example output:示例输出:

 a.out<dict.txt
(null)
(null)
(null)
(null)
(null)
(null)
(null)

Briefly, you're asking scanf to scan strings into memory space that you've only allocated big enough for a few integers.简而言之,您要求scanf将字符串扫描到您只为几个整数分配足够大的内存空间中。 Observe that your malloc call is given a number of ints to allocate and then you're assigning that to a char * .观察到您的 malloc 调用被赋予了许多要分配的整数,然后您将其分配给char * Those two things don't mix, and what's happening is you're scanning a bunch of bytes into your buffer that is too small, running off the end, and stomping on something else, then crashing.这两件事不会混在一起,发生的事情是你将一堆字节扫描到太小的缓冲区中,跑到最后,踩到别的东西,然后崩溃。 The compiler is letting you get away with this because both malloc and scanf are designed to work with various types ( void* ), so the compiler doesn't know to enforce the type consistency in this case.编译器让您摆脱这种情况,因为mallocscanf都被设计为与各种类型( void* )一起使用,因此编译器不知道在这种情况下强制执行类型一致性。

If your goal is to save the word tokens from the input string into an array of words, you can do that but you need to allocate something quite different up front and manage it a bit differently.如果您的目标是将输入字符串中的单词标记保存到单词数组中,您可以这样做,但您需要预先分配一些完全不同的内容并对其进行稍微不同的管理。

But if your goal is simply to produce the output you want, there are actually simpler ways of doing it, like with using strtok or just looping through the input char by char and tracking where you hit spaces, etc.但是,如果您的目标只是产生您想要的输出,那么实际上有更简单的方法可以做到,例如使用strtok或只是逐个字符地循环输入字符并跟踪您击中空格的位置等。

What you're trying to accomplish is parsing standard input.您要完成的是解析标准输入。 Some functions are available to assist you, such as strtok() and strsep() .一些函数可以为您提供帮助,例如strtok()strsep()

You will need an input buffer, which you can mallocate or use an array (I would use an array).您将需要一个输入缓冲区,您可以对其进行分配或使用数组(我将使用数组)。 As the functions parse the input string, you can fling off the words' addresses into a char pointer array.当函数解析输入字符串时,您可以将单词的地址扔到一个字符指针数组中。 You could also build a linked list.你也可以建立一个链表。

Finally, you must buffer input, which adds some delicious complexity to the operation.最后,您必须缓冲输入,这给操作增加了一些可口的复杂性。 Still, it's nothing a good C programmer can't handle.尽管如此,没有什么是好的 C 程序员无法处理的。 Have fun.玩得开心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM