gpt4 book ai didi

无法获取段错误的原因

转载 作者:行者123 更新时间:2023-11-30 19:02:23 26 4
gpt4 key购买 nike

编辑:请注意,这并不是说我无法访问 main() 中的 storeContents() 分配的内存(如果您这么认为的话)。执行 storeContents()

期间程序崩溃

程序在这里失败:

strcpy(contents[count], dir->d_name);
printf("Stored %s(out hiddenVisible)\n", dir->d_name); // for testing

这是strcpy()而不是printf(),我添加它只是为了引用。

调试器(gdb)说:

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7f3cd72 in __strcpy_ssse3 () from /usr/lib/libc.so.6

我正在制作一个涉及以下函数“storeContents”的程序(它将目录的内容名称存储在动态数组中)。此函数有两个问题:(1) 对于第一个文件,它显示“存储文件名”两次,(2)显示“<强>段错误”。我无法弄清楚他们中的任何一个。感谢您的努力!

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

static short hiddenVisible = 0;

/* Store directory's contents in **contents */
static char ** storeContents(struct dirent *dir, DIR *dirp, unsigned numOfContents);
/* Count files/directories in a directory */
static unsigned getNumOfContents(struct dirent *dir, DIR *dirp);

int main(int argc, char const *argv[])
{
char **contents;
DIR *dirp;
struct dirent *dir;
unsigned numOfContents;

dirp = opendir("/home/gaurav");
if((dir = readdir(dirp)) == NULL) {
perror("readdir");
exit(1);
}

/* Getting number of files/directories */
numOfContents = getNumOfContents(dir, dirp);
printf("There are %u files.\n", numOfContents);

/* To position again to the first entry */
rewinddir(dirp);

contents = storeContents(dir, dirp, numOfContents);

/* Print contents */
for(unsigned i = 0; i < numOfContents; ++i)
printf("%s\n", contents[i]);

closedir(dirp);
return 0;
}

char **
storeContents(struct dirent *dir, DIR *dirp, unsigned numOfContents) {

char **contents;
unsigned count = 0;

/* Allocating memory for entries */
contents = malloc(numOfContents * sizeof(*contents));

/* Allocating memory for each '*contents' */
for(unsigned i = 0; i < numOfContents; i++)
contents[i] = (char *)malloc(NAME_MAX); /* we know char is 1 byte, so no "sizeof" */

while(count < numOfContents) {
/* Ignore "." and ".." */
if(!(strcmp(dir->d_name, ".")) || !(strcmp(dir->d_name, ".."))) {
if((dir = readdir(dirp)) == NULL) {
perror("readdir");
exit(1);
}
continue;
}

if(hiddenVisible) {
strcpy(contents[count], dir->d_name);
if((dir = readdir(dirp)) == NULL) {
perror("readdir");
exit(1);
}
count++;
} else {
if(dir->d_name[0] == '.')
if((dir = readdir(dirp)) == NULL) {
perror("readdir");
exit(1);
}
else {
strcpy(contents[count], dir->d_name);
if((dir = readdir(dirp)) == NULL) {
perror("readdir");
exit(1);
}
count++;
}
}
}
return contents;
}

unsigned
getNumOfContents(struct dirent *dir, DIR *dirp) {

unsigned count = 0;

while(dir) {
if(hiddenVisible) {
/* Ignore "." and ".." */
if(!(strcmp(dir->d_name, ".")) || !(strcmp(dir->d_name, ".."))) {
if((dir = readdir(dirp)) == NULL) {
perror("readdir a");
exit(1);
}
continue;
}
count++;
if((dir = readdir(dirp)) == NULL) {
perror("readdir b");
exit(1);
}
} else {
if(dir->d_name[0] == '.') {
if((dir = readdir(dirp)) == NULL) {
perror("readdir c");
exit(1);
}
}
else {
count++;
if((dir = readdir(dirp)) == NULL) {
perror("readdir d");
exit(1);
}
}
}
}

return count;
}

最佳答案

函数 storeContents 中的

contentsmaincontents 的本地副本。

在函数中更改它不会更改 main 中的变量。

您应该返回数组。改变
static void storeContents(struct dirent *dir, DIR *dirp, char **contents, unsigned numOfContents);

static char **storeContents(struct dirent *dir, DIR *dirp, unsigned numOfContents);

,return content; 在函数中调用它,如 char **contents = storeContents(...);

关于无法获取段错误的原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56017314/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com