简体   繁体   English

在数组中插入元素:分段错误(核心已转储)

[英]Insert an element in an array: Segmentation fault (core dumped)

Here is my code in C, I was declaring L as a non-pointer variable but then after running the program I realized that it didn't really change the values in the array after calling the Insert function. 这是我在C语言中的代码,我将L声明为非指针变量,但随后运行该程序后,我意识到在调用Insert函数后,它实际上并没有改变数组中的值。 So I changed the declaration of L as 所以我将L的声明更改为

SeqList* L

where I put an extra * sign and correspondingly changed those . 我在上面放了一个*号,并相应地改了。 to -> , but now I keep getting the 到->,但是现在我不断得到

Segmentation fault (core dumped)

message? 信息? Where did I miss something? 我在哪里错过了什么? Thanks! 谢谢!

#include "stdio.h"

#define MAXSIZE 100

typedef struct SeqList{
    int elem[MAXSIZE];
    int last;
} SeqList;

int GetData(SeqList* L,int i){
    return L->elem[i];
}

void Insert(SeqList* L, int i, int e){
    int temp;
    if (i < 1 || i > L->last + 2){
        printf("Invalid Inserting point.\n");
    }
    if (L->last > MAXSIZE){
        printf("List already full.\n");
    }
    for(temp = L->last; temp != i; temp--){
        L->elem[temp+1] = L->elem[temp];
    }
    L->elem[temp] = e;
    L->last++;
}

int main(){
    SeqList* L;
    int i;
    for(i=0;i<10;i++){
        L->elem[i] = i*i;
    }
    Insert(L,5,10);
    for(i=0;i<10;i++){
        printf("%d\n", L->elem[i]);
    }
    printf("%d\n", GetData(L,5));
}

The following block of code is not good because you have not allocated memory for L and are using it as if it points to valid memory. 以下代码块不好,因为您尚未为L分配内存,而是像指向有效内存一样使用它。

SeqList* L;
int i;
for(i=0;i<10;i++){
    L->elem[i] = i*i;
}

That explains the segmentation fault. 那解释了分割错误。

I'm not sure what you had tried before trying the above but the following program works for me. 我不确定在尝试上述操作之前您曾尝试过什么,但是以下程序对我有用。

#include "stdio.h"

#define MAXSIZE 100

typedef struct SeqList{
    int elem[MAXSIZE];
    int last;
} SeqList;

int GetData(SeqList* L,int i){
    return L->elem[i];
}

void Insert(SeqList* L, int i, int e){
    int temp;
    if (i < 1 || i > L->last + 2){
        printf("Invalid Inserting point.\n");
    }
    if (L->last > MAXSIZE){
        printf("List already full.\n");
    }
    for(temp = L->last; temp != i; temp--){
        L->elem[temp+1] = L->elem[temp];
    }
    L->elem[temp] = e;
    L->last++;
}

int main(){
    SeqList L;
    int i;
    for(i=0;i<10;i++){
        L.elem[i] = i*i;
    }
    L.last = 10;
    Insert(&L,5,10);
    for(i=0;i<10;i++){
        printf("%d\n", L.elem[i]);
    }
    printf("%d\n", GetData(&L,5));
}

You haven't pointed L to an allocated memory. 您没有将L指向已分配的内存。 You could try this: 您可以尝试以下方法:

SeqList* L = malloc(sizeof(SeqList));

Caveats : 注意事项

  • Never have an unitialised pointer ,ie SeqList* L; 永远不要有一个统一的指针,即SeqList* L;
    Here L could be a garbage value and may point to any address causing serious memory exceptoins. 这里L可能是垃圾值,并且可能指向导致严重内存异常的任何地址。
  • Never access pointer without allocation or without pointing it to an already allocated/initialized memory, ie 永远不要在没有分配或没有指向已分配/初始化的内存的情况下访问指针,即

      SeqList* L = malloc(sizeof(SeqList)); //OR SeqList initalizedList = {0}; SeqList* L = &initalizedList; 

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

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