简体   繁体   English

运行程序时出现分段错误(核心已转储)

[英]Segmentation fault (core dumped) while running the program

#include<stdio.h>
#include<string.h>
#include<malloc.h>
//#include<conio.h>
struct list
{
char *value;
struct list *link;
};
struct list *arr[12];int val;
int hf(char *item)
{
int sum,i=0;
while(item[i]!='\0')
{
    sum+=item[i];
    i++;
}
return sum%12;
}
void insert(struct list ** arr,char *item,int val)
{
struct list *temp,*r;
r=*arr;
     temp=(struct list *)malloc(sizeof(struct list));
 strcpy((temp->value),item);
  if(strcmp((r->value),NULL))
  {
      strcpy((r->value),(temp->value));
      (r->link)=NULL;
  }
  else
  {
      while(r->link!=NULL)
        r=r->link;
      r->link=temp;
      r=r->link;
       strcpy((r->value),(temp->value));
      r->link=NULL;

  }
 *arr=r;

}
void main()
{
  struct list *li[12];int i=0;
  for(i=0;i<12;i++)
  {
      li[i]=NULL;
  }
  char *item;int ret;
  strcpy(item,"Steve");
  ret=hf(item);
  insert(&li[ret],item,ret);
  strcpy(item,"raj");
  ret=hf(item);
  insert(&li[ret],item,ret);
  strcpy(item,"Notes");
  ret=hf(item);
  insert(&li[ret],item,ret);
}

The above program is to implement array of linked list and im trying to insert string as the value. 上面的程序是实现链接列表的数组,并尝试插入字符串作为值。 When i am trying to run the program, there are no errors but it tells segmentation fault(core dumped) so please explain the reason 当我尝试运行该程序时,没有错误,但是它告诉分段错误(核心已转储),因此请解释原因

The code 编码

char *item;int ret;
strcpy(item,"Steve");

tries to copy the string literal "Steve" to an uninitialised pointer. 尝试将字符串文字"Steve"复制到未初始化的指针。 You need to allocate memory for item . 您需要为item分配内存。 The easiest way of doing this is to hard-code a suitably sized stack buffer 最简单的方法是对适当大小的堆栈缓冲区进行硬编码

char item[50];

You also have a similar problem inside insert . 您在insert内也有类似的问题。 You could solve this in the same way 您可以用相同的方式解决此问题

struct list
{
    char value[50];
    struct list *link;
};

or you could dynamically allocate the correct size of buffer inside insert 或者您可以在insert动态分配正确大小的缓冲区

temp->value = malloc(strlen(item) + 1);
if (temp->value == NULL) {
    /* handle oom error */
}
strcpy(temp->value, item);

In this latter approach, make sure to free(node->value) when you free that list node. 在后一种方法中,请确保在释放该列表节点时free(node->value) Note also that freeing of all dynamically allocated memory is currently missing from your program, meaning that you leak all memory allocated using malloc . 还要注意,当前程序中缺少所有动态分配的内存,这意味着您泄漏了使用malloc分配的所有内存。

There is one more bug in your code - insert assumes that arr is a pointer to a valid list* but it is always NULL . 您的代码中还有一个错误- insert假定arr是指向有效list*的指针list*但始终为NULL You need to update either main or the assumption in insert here. 您需要在此处更新maininsert的假设。

change the following 改变以下

In insert() function change the if loop 在insert()函数中更改if循环

if(r==NULL){
    r = temp;
}

Change the structure. 更改结构。 change the size of the structure for your need 根据您的需要更改结构的大小

struct list
{
char value[25];
struct list *link;
};

Change the variable item to 将变量项更改为

char item[25]; 字符项[25];

EDIT : There is no need to typecast the output of malloc 编辑:无需类型转换malloc的输出

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

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