从数组创建链表时C中的分段错误

从数组创建链表时C中的分段错误,c,function,linked-list,segmentation-fault,C,Function,Linked List,Segmentation Fault,正如我在标题中所说,我正在尝试从数组生成链表P是一个结构,包含浮点x和指向列表中下一个元素的指针。调用gen_list函数后,第一行代码“first->x=V[0]”返回一个分段错误,这是我从调试器得到的结果: Program received signal SIGSEGV, Segmentation fault. 0x00000000004007f6 in gen_

正如我在标题中所说,我正在尝试从数组生成链表P是一个结构,包含浮点x和指向列表中下一个元素的指针。调用gen_list函数后,第一行代码“first->x=V[0]”返回一个分段错误,这是我从调试器得到的结果:

Program received signal SIGSEGV, Segmentation fault.                                                                 
0x00000000004007f6 in gen_list (V=0x602010, n=10000, first=0x601068 <first>)                                          
    at main.c:46                                                                                                     
46              (*first)->x = V[0];                                                                                   
(gdb)
我的代码:

P* gen_list(float V[], int n, P *first) {
first->x = V[0];
P *T = NULL;
P *new = NULL;
T = first;

t1 = clock();
for (int i = 1; i < n; i++) {
    new->x = V[i];
    T->next = new;
    T = new;
}
T->next = NULL;
t2 = clock();
printf("\nTime for creation of linked list is: %dms", t2 - t1);
return first;}
P*gen_列表(浮点V[],整数n,P*优先){
第一->x=V[0];
P*T=NULL;
P*new=NULL;
T=第一;
t1=时钟();
对于(int i=1;ix=V[i];
T->next=新建;
T=新的;
}
T->next=NULL;
t2=时钟();
printf(“\n创建链表的时间是:%dms”,t2-t1);
先返回;}

分段错误通常发生在指针操作错误时,例如在示例代码中:

typedef struct Popis {
  float x;
  struct Popis *next;
} P;
P *first;

int main(){
  float v[10];
  v[0] = 1;
  first->x = v[0];
}
看看变量*first,它是指向Popis结构的指针,现在在main函数中,您试图使用*first指针,但需要分配内存空间才能使用它。如果在使用以下代码之前分配内存,则不会发生分段错误

first = (P*)malloc(sizeof(P));

问题很可能是由于缺少分配的内存。在“最少需要的代码量”代码段中,没有为您的结构分配内存,这就是进程被终止的原因。内核返回SIGSEGV信号,因为您试图在程序尚未分配(请求)的内存上写入。为了解决这个问题,您可以使用
malloc()简单地分配内存我希望下面的代码可以帮助您解决此问题:

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

typedef struct Popis {
    float x;
    struct Popis *next;
}P;

int main(){

    P *first;
    /*Here we actually create the structure in the memory - we allocate memory for it*/
    first = (struct Popis*)malloc(sizeof(struct Popis));
    /*This is a simple check if the memory allocation was sucessfull*/
    if(!first) 
    {
        fprintf(stderr, "Error has occured during the memory allocation!\n");
        exit(1);
    }

    float v[10];
    v[0] = 1;
    first->x = v[0];
    printf("%f", first->x);

    return 0;
}
#包括
#包括
typedef结构Popis{
浮动x;
结构Popis*next;
}P;
int main(){
P*第一;
/*在这里,我们实际上是在内存中创建结构——我们为它分配内存*/
first=(struct-Popis*)malloc(sizeof(struct-Popis));
/*这是一个简单的检查,看看内存分配是否成功*/
如果(!第一个)
{
fprintf(stderr,“内存分配过程中发生错误!\n”);
出口(1);
}
浮动v[10];
v[0]=1;
第一->x=v[0];
printf(“%f”,第一个->x);
返回0;
}

错误可能在代码的其他地方。试着把它看一遍。如果你正在使用不应该使用的内存,它会告诉你。如果你在调试器中,我们需要一个调试器,然后使用它。首先打印
first
V
的值。我已经添加了最小值。您需要了解对象和指针。谢谢,这就是问题所在,我现在已经为指针分配了内存,一切正常!
#include <stdio.h>
#include <stdlib.h>

typedef struct Popis {
    float x;
    struct Popis *next;
}P;

int main(){

    P *first;
    /*Here we actually create the structure in the memory - we allocate memory for it*/
    first = (struct Popis*)malloc(sizeof(struct Popis));
    /*This is a simple check if the memory allocation was sucessfull*/
    if(!first) 
    {
        fprintf(stderr, "Error has occured during the memory allocation!\n");
        exit(1);
    }

    float v[10];
    v[0] = 1;
    first->x = v[0];
    printf("%f", first->x);

    return 0;
}