在 C 中使用 malloc 分配动态内存

Allocating dynamic memory using malloc in C

我是 C 的新手,一直在努力解决这个问题。这是 的延续。我取得了一些进步,但仍有很多东西需要学习和修复。

简而言之:

在这道题中,"vector" 是一个一维整数数组。因此,向量数组将是一个二维数组,其中包含一维数组。

我需要使用这些变量:

我需要编写以下函数:

这是我的代码:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int init(int*** vectors, int** sizes, int size)
{
    int i, k,j;
    *sizes = (int*)malloc(size * sizeof(int));
    if (*sizes == NULL)
        return 0;
    for (j = 0; j < size; j++)
    {
        (*sizes)[j] = 0;
    }

    *vectors = (int**)malloc(size * sizeof(int*));
    if (*vectors == NULL)
        return 0;
    for (i = 0; i < size; i++)
    {
        (vectors)[i] = NULL;
    }
    return 1;
}
int set(int **vectors, int *sizes, int index, int *tmp, int tmp_size)
{
    if ((vectors)[index] != NULL)
    {
        free((vectors)[index]);
    }
    (vectors)[index] = (int*)malloc(tmp_size * sizeof(int));
    if ((vectors)[index] == NULL)
        return 0;
    for (int b = 0; b < tmp_size; b++)
    {
        (vectors)[index][b] = tmp[b];
    }
    sizes[index] = tmp_size;
    return 1;
}
int main()
{
    int size, i, length, indexhere;
    int** vectors = NULL;
    int* sizes = NULL;
    int* tmp = NULL;
    int* p = &vectors;
    int tempindex;
    printf("\nPlease enter an amount of vectors:\n");
    scanf("%d", &size);
    init(p, &sizes, size);

        printf("Enter index\n");
        scanf("%d", &indexhere);
        printf("Enter Length\n");
        scanf("%d", &length);
        tmp = (int*)malloc(length * sizeof(int));
            printf("Enter elements:\n");
            for (int g = 0; g < length; g++)
                scanf("%d", &tmp[g]);
            set(&vectors, sizes, indexhere, tmp, length);

    system("pause");
    return 0;
}

谁能解释一下为什么程序总是崩溃?

你这样调用set

set(&vectors, sizes, indexhere, tmp, length);

但第一个参数声明为 int **。通过传递 &vector,您传递了一个指向 vector 的指针,即 int *** 类型的东西。这种不匹配将导致 未定义的行为 和可能的崩溃。

  1. init函数中(vectors)[i] = NULL;实际上应该是(*vectors)[i] = NULL;
  2. main 调用 set 函数时,您应该传递 vectors 而不是 &vectors

您的代码中似乎还有几个指针类型不匹配,因此您应该真正注意编译器的警告。这是因为不幸的是,C 允许在不兼容的指针之间进行隐式转换,这与 C++ 不同。

这是一个完整的工作示例。

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

void destroyVectors(int **vectors, int size)
{
for (int i = 0; i < size; i++)
{
    free(vectors[i]);
}
}

int init(int*** vectors, int** sizes, int size)
{
int i, j;
*sizes = (int*)malloc(size * sizeof(int));
if (*sizes == NULL)
    return 0;
for (j = 0; j < size; j++)
{
    (*sizes)[j] = 0;
}

*vectors = (int**)malloc(size * sizeof(int*));
if (*vectors == NULL)
    return 0;
for (i = 0; i < size; i++)
{
    (*vectors)[i] = NULL;
}
return 1;
}
int set(int **vectors, int *sizes, int index, int *tmp, int tmp_size)
{
if ((vectors)[index] != NULL)
{
    free((vectors)[index]);
}
(vectors)[index] = (int*)malloc(tmp_size * sizeof(int));
if ((vectors)[index] == NULL)
    return 0;
for (int b = 0; b < tmp_size; b++)
{
    (vectors)[index][b] = tmp[b];
}
sizes[index] = tmp_size;
return 1;
}

int main()
{
int size = 0, length = 0, indexhere = 0;
int** vectors = NULL;
int* sizes = NULL;
int* tmp = NULL;

printf("\nPlease enter an amount of vectors:\n");
scanf("%d", &size);
init(&vectors, &sizes, size);

printf("Enter index\n");
scanf("%d", &indexhere);

printf("Enter Length\n");
scanf("%d", &length);

tmp = (int*)malloc(length * sizeof(int));
printf("Enter elements:\n");
for (int g = 0; g < length; g++)
    scanf("%d", &tmp[g]);

set(vectors, sizes, indexhere, tmp, length);

for(int i = 0; i < length; ++i)
    printf("byte: %d\n", vectors[indexhere][i]);

printf("sizes index: %d\n", sizes[indexhere]);

free(tmp);
free(sizes);
destroyVectors(vectors, size);

return 0;
}