c - 尝试添加到动态数组时程序崩溃

标签 c crash dynamic-arrays realloc

我正在尝试编写一个函数,将一组数据(名字、姓氏、分数)添加到 3 个不同的动态数组中(一个 2d 字符数组用于名字,一个 2d 字符数组用于姓氏) ,以及分数的 float 组)。这是到目前为止我的代码:

void add(char **firstname, char **lastname,char *newfirst,char *newlast,float newscore, float *score, int *num)
{
    realloc(firstname, sizeof(newfirst));
    realloc(lastname, sizeof(newlast));
    realloc(score, sizeof(float)*sizeof(newscore));
    *num = *num + 1;
    firstname[*num] = newfirst;
    lastname[*num] = newlast;
    score[*num] = newscore;


}

我知道我需要重新分配内存才能向数组添加任何内容。我试图将 1 添加到 num ,以便每当我运行其他函数(例如打印和排序等)时,它都会运行这些函数中的其他循环适当的次数。程序的所有其他方面都可以工作,当我通过这个函数运行它时它就会崩溃。我是否正确分配了内存?什么可能导致崩溃?

这是主要的情况,以防有人需要查看它:

case 2: printf("Enter record of student to add: \n");
            printf("Enter the first name: \n");
            scanf_s("%s", addfirst, 21);
            printf("Enter the last name: \n");
            scanf_s("%s", addlast, 21);
            printf("Enter the score:\n");
            scanf_s("%f", addscore);
            add(firstname, lastname, addfirst, addlast, addscore, score, num);
            break;

我 99% 确信我已经正确初始化了用于此函数的所有变量。

最佳答案

您错误地使用了realloc。您必须在与用作第一个参数的变量不同的变量中接受 realloc 的返回值,因为 realloc 可能会返回 NULL。如果不是NULL,则可以将其赋值给原始变量;但如果它是NULL,您仍然有一个指向先前内存的指针(避免内存泄漏)。

您的代码还有其他问题。

sizeof 不返回字符串的长度,而是返回变量引用的对象的大小,在本例中它是一个指针,因此它将返回 4 或 8。使用 改为 strlen

您使用 firstnamelastname 就好像它们是局部变量一样。当函数返回时,您分配给它们的值就会消失。您需要传入一个三重指针并在使用时取消引用它们。

您正在尝试为字符串重新分配空间,但尚未尝试为数组中的另一个指针重新分配任何空间以实际指向新的字符串内存。

你不能用简单的赋值语句来复制字符串;使用 strcpy 代替。

而且您也没有正确传递变量。如果他们(可能)接收新值,您需要传递地址,而不是值。

所以,看起来您正在尝试执行此操作:

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

void add(
    char *** firstname,  char *** lastname, float ** score,
    char   * newfirst,   char   * newlast,  float    newscore,
    int      index)
{
    char ** t;
    float * f;

    t = realloc(*firstname, (index + 1) * sizeof(*firstname));
    if (!t) ; // handle error
    *firstname = t;
    (*firstname)[index] = malloc(strlen(newfirst) + 1);
    strcpy((*firstname)[index], newfirst);

    t = realloc(*lastname, (index + 1) * sizeof(*lastname));
    if (!t) ; // handle error
    *lastname = t;
    (*lastname)[index] = malloc(strlen(newlast) + 1);
    strcpy((*lastname)[index], newlast);

    f = realloc(*score, (index + 1) * sizeof(*score));
    if (!f) ; // handle error
    *score = f;
    (*score)[index] = newscore;
}

int main() {
    char **firstname = NULL, **lastname = NULL;
    float *score = NULL;
    int num = 0;

    add(&firstname, &lastname, &score, "one",   "two",  1.1f, num++);
    add(&firstname, &lastname, &score, "three", "four", 2.2f, num++);
    add(&firstname, &lastname, &score, "five",  "six",  3.3f, num++);

    for (int i = 0; i < num; ++i)
        printf("%s %s %f\n", firstname[i], lastname[i], score[i]);

    return 0;
}

请注意,只有指针数组被重新分配。字符串内存被简单地分配。

每次重新分配单个元素的效率非常低。通常,您一次会重新分配一大块元素。这样的数组最好表示为结构体,这样它当前分配的空间和当前占用的空间可以与数据一起保存。

关于c - 尝试添加到动态数组时程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23317590/

相关文章:

.net - 将“开始”菜单中的项目拖到其上时,WPF 应用程序崩溃

c++ - boost 动态数组的序列化

c++ - 逐个删除动态数组的元素

c - 为什么使用函数指针而不是直接调用函数

c - 在 if else 中相互比较时,字符如何存储值?

c - 使 malloc() 返回 NULL 而不是使程序崩溃?

qt - 如何在 QTimer 中检查 QtDesigner 生成的小部件点的有效性?

php - 如何通过另一个数组的值访问数组值

c - 从 C 中的地址访问值

在 C 中将 Protocol Buffer 与 JSON 相互转换,而不生成 C 代码