Error while accessing element of array in struct

落爺英雄遲暮 提交于 2019-12-11 13:04:01

问题


I'm trying to write an "ArrayList" program (Similar to the Java ArrayList) that will expand automaticly using realloc, so that the programmer won't have to worry about storage space in arrays. This is my code:

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

#define ARR_DEFAULT_SIZE 20
#define INCR 10

#define ARRTYPE char // Files using this #undef this macro and provide their own type

typedef struct {
    ARRTYPE *arr;
    long size;
    long nextincr;
} arrlst;

arrlst *nlst(void);
void add(arrlst *, ARRTYPE);
ARRTYPE elmat(arrlst *, long);

int main(int argc, char **argv) {
    arrlst *lst = nlst();
    add(lst, 'h');
}

arrlst *nlst() {
    arrlst lst = { malloc(ARR_DEFAULT_SIZE), 0, ARR_DEFAULT_SIZE };
    arrlst *lstptr = &lst;
    return lstptr;
}

void add(arrlst *lst, ARRTYPE elm) {
    if (lst->size >= lst->nextincr) {
        ARRTYPE *tmp = lst->arr;
        lst->nextincr += INCR;
        lst->arr = realloc(lst->arr, lst->nextincr);

        for (int i = 0; i < sizeof tmp; i++)
            lst->arr[i] = tmp[i];
    }

    lst->arr[lst->size++] = elm;
}

ARRTYPE elmat(arrlst *lst, long at) {
    if (lst->size < at)
        strerror(14);

    return lst->arr[at];
}

My problem is that whenever I run this, calling add() produces a segfault, and since most of the code in add() is skipped on the first time calling it, the line of error must be:

lst->arr[lst->size++] = elm;

And I don't know why this would segfault. Please help!


回答1:


Because in nlst you return a pointer to a local variable, and local variables goes out of scope and "dies" when the function they are defined in returns. Using that pointer will lead to undefined behavior, which is a very common reason for crashes.

You have two solutions: Either nlst should dynamically allocate the arrlst structure and return that pointer. Or you pass in a pointer to an arrlst structure, thereby emulating pass by reference.



来源:https://stackoverflow.com/questions/35822806/error-while-accessing-element-of-array-in-struct

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