简体   繁体   中英

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. So I changed the declaration of L as

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.

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. You could try this:

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

Caveats :

  • Never have an unitialised pointer ,ie SeqList* L;
    Here L could be a garbage value and may point to any address causing serious memory exceptoins.
  • 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; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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