- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我知道段错误(核心转储)错误是因为我试图访问我无权访问的内存,但我不明白为什么它会对我的程序执行此操作。
我确信问题来自于 printList(node *head)
函数,但我不明白为什么。
在这段代码中,我必须接受用户输入并以链表的形式创建一个字典。完整代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
//To count the number of times an element was added to the dictionary
static int countWords=0;
typedef struct node{
char string[255]; //store the data
struct node *next; //pointer to the next structure -> this is a pointer
}node;
//Prints the list
void printList(node *head){
node *p = head;
do{
for(int i=0; i<255; i++)
printf("%c", p->string[i]);
p = p -> next;
}
while(p->next != NULL);
}
//Inserts the word in alphabetical order in the list
node *insert_dictionary_order(char *pWord, node *dictionary){
node *head = dictionary; //head of the first node
node *temp = NULL; //Node that will be placed in the linked list
node *p = NULL; //iterator
//creating individual isolated nodes -> not attached to a list
temp = (node*)malloc(sizeof(node));
//This stores the values that pointer gives us in an array
char word[255];
for(int i=0; i<5; i++)
word[i] = *(pWord + i);
//instantiating the node member
temp->next = NULL;
for(int i=0; i<5; i++)
temp -> string[i] = word[i];
//LinkedList is still empty
if(head == NULL){
printf("\nWe get here4!!\n");
head = temp;
printList(head); //THIS DOESNT WORK
printf("\nWe get here5!!\n");
}
else{
printf("\nWe get here6!!\n");
p = head;
//Comparing temp to words in the dictionary
//To know if we are at the first iteration (changing the head)
do{
printf("New word: %s\nDictionary Word: %s\n", temp -> string, p->string);
if(strcmp(temp->string, p->string) < 0 && countWords ==0){ //Special case for the head
temp -> next = p;
head = temp;
countWords++;
}
//Checking if we are at inserting position
else if(strcmp(temp->string, p->string) > 0 && strcmp(temp->string, (p->next)->string) < 0){
temp->next = p->next;
p->next = temp;
countWords++;
}
p = p->next;
}
while(p->next != NULL);
}
printList(head);
return head;
}
//To check if a certain character is a letter
int isALetter(char c){
if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
c == '\0')
return 1;
else
return 0;
}
int main(){
node *dictionary = NULL;
//Prompting the user to enter strings
printf("Please enter some strings.\n");
//To store the strings that the user enters
char string[10000];
fgets(string, 10000, stdin);
//Putting the string to lower case for easy manipulations
for(int i=0; i<10000; i++)
string[i] = tolower(string[i]);
//To put the word in an array
char word[255];
//looping through the strings
int i=0;
//looping through the word
int j = 0;
//Checking the strings to put the words in the dictionary
do{
//Checking if we need to escape the loop
if(i>0 && ((string[i] == '.' && string[i-1]== ' ' )|| string[i] == 0))
break;
//Checking if a letter
if(isALetter(string[i]) == 0){
i++;
continue;
}
//Checking if end of the word
if(isALetter(string[i+1]) == 0)
{
word[j] = string[i];
printf("\n");
char *pWord;
pWord = word;
printf("\nWe get here1!!\n");
//Adding word to the dictionary
dictionary = insert_dictionary_order(pWord, dictionary);
//***trying stuff
printf("\nWe get here2!!\n");
//***Printing the updated linked list
//printList(dictionary);
//***trying stuff
printf("\nWe get here3!!\n");
//reseting the word array
memset(word, 0, 255);
j = 0;
i++;
continue;
}
word[j] = string[i];
j++;
i++;
}
while(i != 10000);
//Printing the list
printf("\n\nPrinting the list..\n\n");
printList(dictionary);
return 0;
}
这是输出:
Please enter some strings.
zz aa
We get here1!!
We get here4!!
Segmentation fault (core dumped)
我们可以清楚地看到问题来自于具有此输出的 printList(node *head)
方法,因为“We get here!!”数字 2、3、5 和 6 没有出现(它们都是在调用此方法之后出现的)。
最佳答案
正如 kaylum 评论的那样,使用调试器。如果你这样做了,你会看到这个:
Please enter some strings.
aa zz
We get here1!!
We get here4!!
Program received signal SIGSEGV, Segmentation fault.
0x0000555555555334 in printList (head=0x612000000040) at t.c:26
26 while(p->next != NULL);
(gdb) p p
$1 = (node *) 0x0
这就是你的错误的根源(正如 kaylum 也已经评论过的那样)。
关于c - 以链表的形式构建字典并收到段错误(核心转储)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59095387/
Linux 有许多跨(假设是 2 个)CPU 内核执行的线程和进程。我希望我的单线程 C/C++ 应用程序成为 CPU0 上的唯一线程。我如何“移动”所有其他线程以使用 CPU1? 我知道我可以使用
我有一个类似于下图的数据库表 Table with 2 columns (UserId and value) 我将传递 UserId 和 2 个字符串。例如:userId: 1, key1: h1,
我想在我的新项目中使用 ASP.NET Core,因为我听说它更快。但是,该项目将使用广泛的数据库访问功能,Entity Framework Core 不支持其中一些功能。我想知道,是否可以使用 En
我已经使用 EntityFrameworkCore.SqlServer 2.0 开发了 asp .net core wep api 2.0 应用程序。它是使用数据库优先方法开发的。当尝试使用 dbco
我已经阅读了很多关于这个主题的文章,但我仍然无法处理这个问题。对不起,如果它是重复的,无论如何! 所以基本上,我正在从头开始构建一个 Angular 应用程序,并且我想按照最佳约定来组织我的代码。我有
我对MPI还是陌生的,所以如果这是一个琐碎的问题,请原谅我。我有一个四核CPU。我想运行一个在单个内核上使用两个进程的OpenMPI C++程序。有什么办法吗?如果是这样,那又如何?我提到了this
下面是一个传播异常处理机制的类问题,所需的输出是异常。任何人都可以解释为什么输出是异常,在此先感谢。 Class Question { public void m1() throws Excep
我想打印每个获得 CPU 时间片的进程的 name 和 pid。可能吗? 最佳答案 对于单个流程,您可以在以下位置获取此信息: /proc//stat 第14和第15个字段分别代表在用户态和内核态花费
我想知道是否可以识别具有特定 thread-id 的线程使用的物理处理器(核心)? 例如,我有一个多线程应用程序,它有两 (2) 个线程(例如,thread-id = 10 和 thread-id =
我有一个需要身份验证的 Solr 核心。假设我有一个用户,密码为password。当我现在尝试在控制台中创建一个 Solr 核心时 bin\solr create -c test 我收到 HTTP 错
我想为与使用它的项目不同的类库中的第二个和后续数据库创建迁移。有皱纹。我永远不会知道连接字符串,直到用户登录并且我可以从目录数据库 (saas) 中获取它。 对于目录数据库,我使用了来自 this 的
我想为一种可以产生 GHC Core 的简单语言创建一个前端。然后我想获取这个输出并通过正常的 GHC 管道运行它。根据this page , 不能直接通过 ghc 命令实现。我想知道是否有任何方法可
阅读文档,我构建了 2 个使用 BLE 连接 2 个 iDevices 的应用程序。 一个设备是中央设备,另一个是外围设备。 Central在寻找Peripheral,当找到它时,探索它的服务和特性,
在我的网络应用程序中,我对长时间运行的任务进行了操作,我想在后台调用此任务。因此,根据文档 .net core 3.1 Queued background tasks我为此使用这样的代码: publi
Solr 1.4 Enterprise Search Server 建议对核心副本进行大量更新,然后将其换成主核心。我正在按照以下步骤操作: 创建准备核心:http://localhost:8983/
它们是否存在,如果存在,文档和代码在哪里? 最佳答案 它们位于 Git 的 test 目录中。 https://github.com/jquery/jquery/tree/master/test 关于
我有一个 Lisp (SBCL 1.0.40.0.debian) 应用程序 (myfitnessdata),它使用以下代码来处理命令行参数: (:use :common-lisp) (:export
Core是GHC的中间语言。阅读Core可以帮助你更好地了解程序的性能。有人向我索要有关阅读 Core 的文档或教程,但我找不到太多。 有哪些文档可用于阅读 GHC Core? 这是我迄今为止发现的内
我有一个核心 WebJob 部署到 Azure Web 应用程序中。我正在使用WebJobs version 3.0.6 . 我注意到,WebJob 代码不会立即拾取对连接字符串和应用程序设置的更改(
我有一个在内部构造和使用 SqlConnection 类的第三方库。我可以从该类继承,但它有大量重载,到目前为止我一直无法找到合适的重载。我想要的是将参数附加到正在使用的连接字符串。 有没有办法在 .
我是一名优秀的程序员,十分优秀!