gpt4 book ai didi

c - 为什么我不能将指向结构的指针保存为指向指针数组的指针的索引?

转载 作者:太空宇宙 更新时间:2023-11-04 07:51:44 25 4
gpt4 key购买 nike

我过去做过类似的事情,所以我不确定为什么当我尝试将我的指针打印到一个指针数组时,我得到一堆 (null) 作为输出。这是我在文件底部附近所说的代码:

int z = 0;
while (z < 9) {
printf("%s ", allLines[z]->username);
z++;
}

我想做的是从用户那里获取一个目录(相对或绝对),然后切换到该目录并打开该目录中的所有文本文件,以便我可以从中提取每一行并存储每一行在记录结构中。请在下面查看我的代码:

#define _GNU_SOURCE
#define MAXLINE 256
#define MAXPATHLENGTH 1024
#define MAXRECORDS 10000
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>

typedef struct records {
char* username;
char* password;
char* bloodType;
char* domainName;
char* index;
} records;

int main(int argc, char** argv) {
if(argc != 2) {
printf("You must provide 2 arguments: <./filename> <directory>\n");
return -1;
}

char* path = malloc(MAXPATHLENGTH * sizeof(char));
char* cwd = malloc(MAXPATHLENGTH * sizeof(char));
FILE* sortedFile;
FILE* dirEntry;
DIR* dirp; //pointer to a directory stream
struct dirent* dirstructp; //pointer to a dirent structure
struct stat buffer;
records** allLines = malloc(MAXRECORDS * sizeof(records));
int linesInFile[MAXRECORDS];
records** files = calloc(MAXRECORDS, sizeof(records)); //pointer to an array of pointers (each of which point to a records struct)
int totalFiles;

if ((sortedFile = fopen("sorted.yay", "w+")) == NULL) {
fprintf(stderr, "%s\n", strerror(errno));
printf("fopen");
exit(errno);
}

//changes the current working directory of the calling process to the directory specified
if((chdir(argv[1])) == -1) {
fprintf(stderr, "%s\n", strerror(errno));
exit(errno);
}

/*copies the pathname of the current working directory to the array pointed to by cwd, which is of length MAXPATHLENGTH*/
if((path = getcwd(cwd, MAXPATHLENGTH)) == NULL) {
fprintf(stderr, "%s\n", strerror(errno));
exit(errno);
}

//returns a pointer to the directory stream if successful
if((dirp = opendir(path)) == NULL) {
fprintf(stderr, "%s\n", strerror(errno));
printf("opendir");
exit(errno);
}

int i = 0;
int j = 0;
int l = 0;
while((dirstructp = readdir(dirp)) != NULL) {

if ((strncmp(dirstructp->d_name, ".", strlen(".")) != 0) && (strncmp(dirstructp->d_name, "..", strlen("..")) != 0)) {

if(lstat(dirstructp->d_name, &buffer) == -1) {
fprintf(stderr, "%s\n", strerror(errno));
printf("Bob Saget");
exit(errno);
}

if(S_ISREG(buffer.st_mode)) {

if ((dirEntry = fopen(dirstructp->d_name, "r")) == NULL) {
fprintf(stderr, "%s\n", strerror(errno));
printf("fopen2");
exit(errno);
}

char* buf = malloc(MAXLINE * sizeof(char));
while(fgets(buf, MAXLINE, dirEntry)) {
records* line = malloc(sizeof(records));
int k = 0;
while(k < 5) {
char* token = malloc(MAXLINE * sizeof(char));
token = strsep(&buf,",");
switch(k) {
case 0:
line->username = malloc(MAXLINE * sizeof(char));
strncpy(line->username, token, strlen(token));
break;
case 1:
line->password = malloc(MAXLINE * sizeof(char));
strncpy(line->password, token, strlen(token));
break;
case 2:
line->bloodType = malloc(MAXLINE * sizeof(char));
strncpy(line->bloodType, token, strlen(token));
break;
case 3:
line->domainName = malloc(MAXLINE * sizeof(char));
strncpy(line->domainName, token, strlen(token));
break;
case 4:
line->index = malloc(MAXLINE * sizeof(char));
strncpy(line->index, token, strlen(token));
break;
}
k++;
}
allLines[l] = line;
l++;
free(line);
free(buf);
buf = malloc(MAXLINE * sizeof(char));
}
linesInFile[j] = i;
i = 0;
files[j] = allLines;
j++;
totalFiles = j;
free(buf);
}
}
}
int z = 0;
while (z < 9) {
printf("%s ", allLines[z]->username);
z++;
}
return 0;
}

最佳答案

records* line = malloc(sizeof(records));
...
line->username = malloc(MAXLINE * sizeof(char));
...
allLines[l] = line;
...
free(line);
...
printf("%s ", allLines[z]->username);

您释放了 allLines[z]指针,因此它无效并且访问它是未定义的行为。

  1. 我建议您清理代码。
  2. 另外,下次请发一个 MCVE这是重现问题所需的最小示例。
  3. 这是学习如何调试程序的好时机。
  4. char* token = malloc(MAXLINE * sizeof(char));<br/>
    token = strsep(&buf,",");
    只是泄漏内存....
  5. 如果行格式不正确并且 token = strsep(&buf,","); 怎么办?返回 NULL?您的代码将在 strncpy(line->index, token, strlen(token)); 中执行未定义的行为并且该程序很可能会在 Linux 上收到 sigsegv。
  6. char* buf = malloc(MAXLINE * sizeof(char));
    while(fgets(buf, MAXLINE, dirEntry)) {
    free(buf);<br/>
    buf = malloc(MAXLINE * sizeof(char));
    .具有相同缓冲区的 free + malloc 毫无意义。
  7. 请正确缩进您的代码。如果您的代码有超过 3 个级别的标识,那么是时候创建一个函数了。好好读一读:Linux kernel coding style .
  8. 所有 strncpy(line->password, token, strlen(token));只是奇怪。首先,它与 strcpy(line->password, token) 相同第二个如果 strlen(token)等于 MAXLINE 输出将不会以 null 终止,其次如果 strlen(token)大于 MAXLINE 这将调用未定义的行为。你应该line->username = malloc((strlen(token) + 1) * sizeof(char)); memcpy(line->username, token, strlen(token) + 1);或者只是 line->username = strdup(token); .对if (line->username == NULL) { handle errors; }也很好.或者使用简单的 if (strlen(token) >= MAXLINE) { fprintf(stderr, "token too long!"); abort(); } 来防止 UB .或者你可以 strlcpy(line->username, token, MAXLINE)这将防止溢出并始终以 null 终止输出。不要使用 strncpy ,这是一个糟糕的功能。
  9. allLines的分配是无效的。 records** allLines = malloc(MAXRECORDS * sizeof(records));它应该分配一个 sizeof(records*) 的数组指针不是记录。您可以分配一个数组 records* allLines = malloc(MAXRECORDS * sizeof(records));这可能可以用 allLines[l] = *line; 修复你的代码.但是访问需要使用.然后-> , 比如 printf需要用 printf("%s ", allLines[z].username); 更改
  10. 同样适用于 records** files = calloc(MAXRECORDS, sizeof(records));这是指向文件指针的指针。
  11. 可移植且位置正确定义的宏,其含义可能与 MAXPATHLENGTH 相同是PATH_MAXlimits.h 中定义
  12. char* path = malloc(MAXPATHLENGTH * sizeof(char));path = getcwd(cwd, MAXPATHLENGTH)泄漏内存(!)而且很奇怪。成功path == cmd ...我建议你只想要cmd = get_current_dir_name() .

关于c - 为什么我不能将指向结构的指针保存为指向指针数组的指针的索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53403384/

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