1

I am trying to implement a linked list using malloc. My linked list is just called a Vector because I figured it made sense to mimic C++.

So on my first TODO in initVector is where I figured I screwed up. I didn't have malloc() called to add the Vector passed through. That did not work. I even tried right after declaring the struct Vector a to be:

struct Vector a = malloc(sizeof(struct Vector));

However that did not work either. What do I need to do to properly allocate memory for my LinkedList?

My second TODO states where the segfault occurs.

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

// https://stackoverflow.com/questions/3536153/c-dynamically-growing-Vector
// https://stackoverflow.com/questions/314401/how-to-read-a-line-from-the-console-in-c

struct Vector {
    char *index;
    struct Vector *next;
    size_t used;
    size_t size;
};

void initVector(struct Vector *a, size_t initialSize) {

    a = malloc(initialSize * sizeof(struct Vector)); //TODO: --1-- this i am just trying. i don't know how to do this because my free function is not working
    a->index = malloc(initialSize * sizeof(char));
    a->next = malloc(sizeof(struct Vector));
    a->used = 0;
    a->size = initialSize;
}

void insertVector(struct Vector *a, char *element) {
    if (a->used == a->size) {
        a->size *= 2;
        a = realloc(a, a->size * sizeof(struct Vector));
    }
    a->used++;
    a->index = element;
} // Adds an element to the index. If the allocated size is at the cap, it reallocates the current amount multiplied by two

void freeVector(struct Vector *a) {
    free(a); //TODO: --1-- code segfaults here
    a = NULL;
    a->used = a->size = 0;
}

int main(int argc, char* argv[]) {     
    struct Vector a;
    char *st = argv[1];
        
    initVector(&a, 5);  // initially 5 elements
    insertVector(&a, st);
        
    printf("%s\n", a.index);
        
    freeVector(&a);
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • 2
    `a` is a local variable, not allocated by `malloc`, so you mustn't `realloc()` nor `free()` it. And the next line in `freeVector` doesn't make sense either - you set `a` to NULL and then immediately dereference it. – Nate Eldredge Feb 27 '21 at 05:09
  • Remember C uses pass by value, and function parameters behave as local variables within the function. When you do `a = malloc` in `initVector()`, you discard whatever was passed to the function, and set the *local variable* `a` to the pointer returned by `malloc`. This pointer is never seen by anyone outside `initVector()` so you just leaked that memory. – Nate Eldredge Feb 27 '21 at 05:11
  • `Vector` is also a poor name because the C++ `std::vector` is not a linked list but really more like an array. – Nate Eldredge Feb 27 '21 at 05:12
  • Your code looks like it's trying to be a linked list in some parts, and a resizeable array in others. Make sure you know which it is, and what your code actually does. – Dmitri Feb 27 '21 at 05:14
  • `free(a); a->x = y;` NEVER makes sense to do. – klutt Feb 27 '21 at 05:58

1 Answers1

1

You get a seg fault because you are trying to free an object that was not allocated via malloc() or a similar function: freeVector(&a); clearly passes the address of a local Vector object with automatic storage.

Your data structure is quite confusing: are you trying to implement a dynamically allocated array object, akin to c++ vector objects or a linked list of nodes carrying a string payload?

chqrlie
  • 131,814
  • 10
  • 121
  • 189