0

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));
}
2
  • Try compiling it with debugging symbols (-g if you’re using GCC or Clang) and running it under a debugger (probably GDB). It should tell you where it crashes. Add that to your question. Commented Dec 24, 2014 at 3:52
  • 1
    you declare a SeqList* L, but not allocate space for it Commented Dec 24, 2014 at 3:54

2 Answers 2

3

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));
}
Sign up to request clarification or add additional context in comments.

Comments

0

You haven't pointed L to an allocated memory. You could try this:

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

Caveats:

  • Never have an unitialised pointer ,i.e 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, i.e

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.