C program to transfer contents of one array to another results in segmentation fault

≯℡__Kan透↙ 提交于 2019-12-12 04:05:14

问题


C beginner here. My transfer function is supposed to take 5 parameters, two arrays (first array has values the other is empty), a length value for the arrays, a size of value, a remove function, and an insert function. It then takes the values of the first array and puts them in a linked list. Then it removes the values from the linked list and inserts them into the 2nd array. Unfortunately I'm getting the following error message when the transfer function runs in test.c file.

Segmentation fault: 11

Here are my files, dll.c

#include <dll.h>

List *List_create(void)
{
    return calloc(1, sizeof(List));
}

void add_to_back(List *list, void *value)
{  
    ListNode *node = calloc(1, sizeof(ListNode));
    node->value = value;

    if (list->first == NULL) {
        list->first = node;
        list->last = node;
    } else {
        list->last->next = node;
        node->prev = list->last;
        list->last = node;
    }

    list->count++;
}

void *remove_from_back(List *list)
{
    void *result = NULL;

    if (list->last == NULL) return result;

    ListNode *dead_node = list->last;
    result = list->last->value;

    if (list->last->value == list->first->value) {
        list->last = list->first = NULL;    
    } else {
        list->last = list->last->prev;
        list->last->next = NULL;        
    }   
    free(dead_node);
    return result;
}

void add_to_front(List *list, void *value)
{
    ListNode *node = calloc(1, sizeof(ListNode));
    node->value = value;

    if (list->first == NULL) {
        list->first = node;
        list->last = node;
    } else {
        list->first->prev = node;
        node->next = list->first;
        list->first = node;
    }

    list->count++;
}

void *remove_from_front(List *list)
{
    void *result = NULL;

    if (list->first == NULL) return result;

    ListNode *dead_node = list->first;
    result = list->first->value;

    if (list->last->value == list->first->value) {
        list->last = list->first = NULL;    
    } else {
        list->first = list->first->next;
        list->first->prev = NULL;       
    }   
    free(dead_node);
    return result;
    return 0;
}

void transfer(void **arr1, void **arr2, int length, int size, void (*insert)(List *, void *), void* (*remove)(List *)) {
    List *list = List_create();

    for (int i=0; i < length; i++) {
        (*insert)(list, &arr1[i]);
    }

    for (int i=0; i < length; i++) {
        void *indexPtr = arr2 + i*size;
        indexPtr = (*remove)(list);
    }
}

test.c

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

int test_add_to_back(void) {
    List *list = List_create();

    int new_value = 1;
    add_to_back(list, &new_value);
    ListNode *curr = list->first;
    if (curr->value != &new_value) return 0;
    if (list->first->value != list->last->value || list->last->value != &new_value) return 0;

    new_value = 2;
    add_to_back(list, &new_value);  
    new_value = 3;
    add_to_back(list, &new_value);  
    new_value = 4;
    add_to_back(list, &new_value);  
    curr = list->first;

    new_value = 4;
    if (list->last->value != &new_value) return 0;
    if (curr->value != &new_value) return 0;
    curr = curr->next;
    new_value = 2;
    if (curr->value != &new_value) return 0;
    curr = curr->next;
    new_value = 3;
    if (curr->value != &new_value) return 0;
    curr = curr->next;
    new_value = 4;
    if (curr->value != &new_value) return 0;

    return 1;
}

int test_remove_from_back(void) {
    List *list = List_create();
    char *test1 = "test1 data";
    char *test2 = "test2 data";
    char *test3 = "test3 data";

    add_to_back(list, test1);
    add_to_back(list, test2);
    add_to_back(list, test3);

    if (remove_from_back(list) != test3) return 0;
    if (remove_from_back(list) != test2) return 0;
    if (remove_from_back(list) != test1) return 0;
    return 1;
}

int test_add_to_front(void) {
    List *list = List_create();
    char *test1 = "test1 data";
    char *test2 = "test2 data";
    char *test3 = "test3 data";

    add_to_front(list, test1);
    add_to_front(list, test2);
    add_to_front(list, test3);

    ListNode *head = list->first;
    if (head->value != test3) return 0;
    head = head->next;
    if (head->value != test2) return 0;
    head = head->next;
    if (head->value != test1) return 0;
    return 1;
}

int test_remove_from_front(void) {
    List *list = List_create();
    char *test1 = "test1 data";
    char *test2 = "test2 data";
    char *test3 = "test3 data";

    add_to_back(list, test1);
    add_to_back(list, test2);
    add_to_back(list, test3);

    if (remove_from_front(list) != test1) return 0;
    if (remove_from_front(list) != test2) return 0;
    if (remove_from_front(list) != test3) return 0;
    return 1;
}

int test_transfer(void) {
    #define ARRAY_LENGTH 10
    void *arr1[ARRAY_LENGTH];
    for (int i = 0; i < ARRAY_LENGTH; i++) {
        arr1[i] = &i;
    }

    void *ptr;
    void *arr2[ARRAY_LENGTH];
    for (int i = 0; i < ARRAY_LENGTH; i++) {
        arr2[i] = ptr;
    }

    transfer(arr1, arr2, ARRAY_LENGTH, sizeof(int), (*add_to_front), (*remove_from_front));

    for (int i=0; i < ARRAY_LENGTH; i++) {
        printf("%d\n", *((int *)arr2[i]));
    }

    return 0;
}

int main() {
    printf("helloworld\n");
    if(test_add_to_back() != 1) printf("Add to back test failed\n");
    else printf("Add to back test passed\n");

    if(test_remove_from_back() != 1) printf("Remove from back test failed\n");
    else printf("Remove from back test passed\n");

    if(test_add_to_front() != 1) printf("Add to back test failed\n");
    else printf("Add to back test passed\n");

    if(test_remove_from_front() != 1) printf("Remove from front test failed\n");
    else printf("Remove from front test passed\n");

    test_transfer();

    return 0;
}

I also have a dll.h file but I didn't include it.

来源:https://stackoverflow.com/questions/36488554/c-program-to-transfer-contents-of-one-array-to-another-results-in-segmentation-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!