C 在函数内部重新分配

发布于 2024-11-06 18:55:43 字数 1398 浏览 4 评论 0原文

这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>

void mp3files(char** result, int* count, const char* path) {
    struct dirent *entry;
    DIR *dp;

    dp = opendir(path);
    if (dp == NULL) {
        printf("Error, directory or file \"%s\" not found.\n", path);
        return;
    }

    while ((entry = readdir(dp))) {
        if ((result = (char**) realloc(result, sizeof (char*) * ((*count) + 1))) == NULL) {
            printf("error");
                return;
        }

        result[*count] = entry->d_name;
        (*count)++;
    }

    closedir(dp);
}

int main() {

    int* integer = malloc(sizeof (int));
    *integer = 0;

    char** mp3FilesResult = malloc(sizeof (char*));
        mp3files(mp3FilesResult, integer, ".");

    for (int i = 0; i < *integer; i++) {
        printf("ok, count: %d \n", *integer);
        printf("%s\n", mp3FilesResult[i]);
    }

    return (EXIT_SUCCESS);
}

它给了我分段错误。但是,当我将这个循环:

for (int i = 0; i < *integer; i++) {
    printf("ok, count: %d \n", *integer);
    printf("%s\n", mp3FilesResult[i]);
}

放在 mp3files 函数的末尾时,它就可以工作了。当我将 mp3files 函数的第三个参数从“.”更改为“.”时对于包含少于 4 个文件或目录的目录,效果很好。换句话说,当变量 mp3FilesResult 指向少于 4 个字符串时,它不会因分段错误而失败。

为什么它一直这样做?

预先感谢并抱歉我的英语。

Here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>

void mp3files(char** result, int* count, const char* path) {
    struct dirent *entry;
    DIR *dp;

    dp = opendir(path);
    if (dp == NULL) {
        printf("Error, directory or file \"%s\" not found.\n", path);
        return;
    }

    while ((entry = readdir(dp))) {
        if ((result = (char**) realloc(result, sizeof (char*) * ((*count) + 1))) == NULL) {
            printf("error");
                return;
        }

        result[*count] = entry->d_name;
        (*count)++;
    }

    closedir(dp);
}

int main() {

    int* integer = malloc(sizeof (int));
    *integer = 0;

    char** mp3FilesResult = malloc(sizeof (char*));
        mp3files(mp3FilesResult, integer, ".");

    for (int i = 0; i < *integer; i++) {
        printf("ok, count: %d \n", *integer);
        printf("%s\n", mp3FilesResult[i]);
    }

    return (EXIT_SUCCESS);
}

It gives me segmentation fault. However, when I put this loop:

for (int i = 0; i < *integer; i++) {
    printf("ok, count: %d \n", *integer);
    printf("%s\n", mp3FilesResult[i]);
}

in the end of mp3files function, it works. And when I change the third parameter of mp3files function from "." to a directory which contains less then 4 files or directories, it works great. In other words, when variable mp3FilesResult points at less then 4 strings, it doesn't fail with segmentation fault.

Why does it keep doing it ?

Thanks in advance and sorry for my english.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

宣告ˉ结束 2024-11-13 18:55:43

您传入一个 char **,一个指向 char 的指针,它代表指向“string”的指针,而“string”代表“字符串数组”。如果您想重新分配该数组,则必须通过引用传递它(传递指向它的指针),因此您需要一个“指向字符串数组的指针”,或一个 char ***:

... myfunc(char ***result, ...)
{
    *result = realloc(*result, ...); // writing *result changes caller's pointer
}


...
char **data = ...;
myfunc(&data, ...);

You pass in a char **, a pointer to a pointer to char, which is representing pointer to "string" which is representing "array of string". If you want to reallocate that array you must pass it by reference (pass a pointer to it) so you need a "pointer to array of string", or a char ***:

... myfunc(char ***result, ...)
{
    *result = realloc(*result, ...); // writing *result changes caller's pointer
}


...
char **data = ...;
myfunc(&data, ...);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文