C 在函数内部重新分配
这是我的代码:
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您传入一个
char **
,一个指向 char 的指针,它代表指向“string”的指针,而“string”代表“字符串数组”。如果您想重新分配该数组,则必须通过引用传递它(传递指向它的指针),因此您需要一个“指向字符串数组的指针”,或一个char ***:
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 achar ***
: