结构数组的第二个位置有垃圾

时间:2018-06-15 15:29:21

标签: c arrays pointers

我一直试图用C语言包围结构数组,但似乎无法弄清楚这一点。我已经完成了研究并搜索了SO,但我找不到以下问题的答案:

我创建了一个包含项目列表的向量,它的大小和maxsize,以便我可以将它用作动态数组。

当我在此向量上插入第二个项目时,它会打印出垃圾。然而,第一,第三和第四项似乎应该出现。为什么会这样?

这是我的代码。 LIBRARY.C:

#include "library.h"

int main(int argc, char* argv[]) {
    vector *catalog = init(2);

    Book a = {
        .title = "TestA", .isbn13 = "12345678901", .authors = "AuthorA", .publisher = "publisherA", .year = 1999
    }, b = {
        .title = "TestB", .isbn13 = "12345678901", .authors = "AuthorB", .publisher = "publisherB", .year = 1999
    }, c = {
        .title = "TestC", .isbn13 = "12345678901", .authors = "AuthorC", .publisher = "publisherC", .year = 1999
    }, d = {
        .title = "TestD", .isbn13 = "12345678901", .authors = "AuthorD", .publisher = "publisherD", .year = 1999
    };

    add(catalog, a);
    add(catalog, b);
    add(catalog, c);
    add(catalog, d);

    printCatalog(catalog);

    destroy(catalog);

    return 0;
}

library.h:

#ifndef library_h
#define library_h

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

#define ISBN13_SIZE 14

typedef struct {
    char *title;
    char isbn13[ISBN13_SIZE];
    char *authors;
    char *publisher;
    int year;
} Book;

typedef Book type_t;

typedef struct {
    type_t *li;
    size_t length;
    size_t maxLength;
} vector;

/* libgest header */
vector *init(size_t initial_size);
int grow(vector *v);
type_t add(vector *v, type_t value);
void destroy(vector *v);
void printCatalog(vector *v);
void printBook(type_t b);

#endif

libgest.c:

#include "library.h"

vector *init(size_t initial_size) {
    vector *v = malloc(sizeof(vector));
    v->li = malloc(sizeof(type_t) * initial_size);
    v->length = 0;
    v->maxLength = initial_size;
    return v;
}

int grow(vector *v) {
    type_t *newli = malloc(2*v->maxLength); /* Allocate list with double size */
    if(newli == NULL)
        return 0; /* Failed to allocate memory */
    memcpy(newli, v->li, sizeof(vector) * v->length); /* Copy contents over to the new list */
    free(v->li); /* Free old list memory to avoid memory leaks */
    v->li = newli; /* Set our list to the new, expanded, list */
    v->maxLength *= 2; /* Update vector's max size */
    return 1; /* Success */
}

type_t add(vector *v, type_t value) {
    if(v->length > v->maxLength-1)
        grow(v);
    v->li[v->length++] = value;
    return value;
}

void destroy(vector *v) {
    free(v->li);
    free(v);
}

void printCatalog(vector *v) {
    size_t i;

    for (i = 0; i < v->length; i++) {
        printBook(v->li[i]);
    }
}

void printBook(type_t b) {
    printf("Title: %s\n", b.title);
    printf("ISBN: %s\n\n", b.isbn13);
}

当我运行它时,会打印出来:

Title: TitleA
ISBN: 12345678901

Title: x12
ISBN: b

Title: TitleC
ISBN: 12345678901

Title: TitleD
ISBN: 12345678901

那么为什么向量中的第二个元素是垃圾?

1 个答案:

答案 0 :(得分:1)

grow方法中,您希望将li数组的大小增加两倍,但malloc需要以字节为单位的参数,因此缺少因子 - sizeof(type_t),添加它并替换下面的行

type_t *newli = malloc(2*v->maxLength);

通过

type_t *newli = malloc(2 * v->maxLength * sizeof(type_t)); 

另一个问题是在memcpy函数中,您要复制所有type_t项而不是矢量项,替换

memcpy(newli, v->li, sizeof(vector) * v->length);

通过

memcpy(newli, v->li, sizeof(type_t) * v->length);