从函数返回链接列表时出现分段错误

我有以下代码,该代码将文件打印在带有结尾的文件夹中。 默认情况下,它还会查找带有以下结尾“ SCL_10m.tif”的文件。 这是到目前为止有效的版本:

#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "gdal/gdal.h"
#include "gdal/cpl_conv.h"
#include <stdio.h>
#include <time.h>


// Linked-List
typedef struct nlist{
    char *data;
    struct nlist *next;
}Node;

// Function to replace a string with another 
// string 
char* str_replace(char* string,const char* substr,const char* replacement) {
    char* tok = NULL;
    char* newstr = NULL;
    char* oldstr = NULL;
    int   oldstr_len = 0;
    int   substr_len = 0;
    int   replacement_len = 0;

    newstr = strdup(string);
    substr_len = strlen(substr);
    replacement_len = strlen(replacement);

    if (substr == NULL || replacement == NULL) {
        return newstr;
    }

    while ((tok = strstr(newstr,substr))) {
        oldstr = newstr;
        oldstr_len = strlen(oldstr);
        newstr = (char*)malloc(sizeof(char) * (oldstr_len - substr_len + replacement_len + 1));

        if (newstr == NULL) {
            free(oldstr);
            return NULL;
        }

        memcpy(newstr,oldstr,tok - oldstr);
        memcpy(newstr + (tok - oldstr),replacement,replacement_len);
        memcpy(newstr + (tok - oldstr) + replacement_len,tok + substr_len,oldstr_len - substr_len - (tok - oldstr));
        memset(newstr + oldstr_len - substr_len + replacement_len,1);

        free(oldstr);
    }

    free(string);

    return newstr;
}


Node* insert(Node*,char*);
void show(Node*);

//Function to insert an element in the linked-list
Node* insert(Node *Head,char *value)
{
    Node *new_string;
    new_string = (Node *)malloc(sizeof(Node));
    new_string->data = malloc(strlen(value)+1);
    strcpy(new_string->data,value);
    Node *check;
    check = (Node *)malloc(sizeof(Node));

    if(Head == NULL){
        Head = new_string;
        Head->next = NULL;
    }
    else{
        check = Head;
        while(check->next != NULL)
            check = check->next;

        check->next = new_string;
        new_string->next = NULL;
    }
    return Head;
}

//Function to show the elements in the linked-list
void show(Node *Head)
{
    Node *check;
    check = (Node *)malloc(sizeof(Node));
    check = Head;
    if (check == NULL){
        return;
    }

    while(check != NULL) {
        printf("%s",check->data);
        check=check->next;
    }
    printf("\n");
}

//Function to find the files in the folder recursively based on 
// a wildcard,and adding them to a linked-list
void listFilesRecursively(char *path,char *suffix);

//checks if a string ends with a substring
int string_ends_with(const char * str,const char * suffix)
{
    int str_len = strlen(str);
    int suffix_len = strlen(suffix);

    return 
        (str_len >= suffix_len) &&
        (0 == strcmp(str + (str_len-suffix_len),suffix));
}

void listFilesRecursively(char *basePath,char *suffix)
{
    char path[1000];
    struct dirent *dp;
    DIR *dir = opendir(basePath);
    Node *Head = NULL;

    if (!dir)
        return;

    while ((dp = readdir(dir)) != NULL)
    {
        if (strcmp(dp->d_name,".") != 0 && strcmp(dp->d_name,"..") != 0)
        {
            strcpy(path,basePath);
            strcat(path,"/");
            strcat(path,dp->d_name);

            if (string_ends_with(path,suffix))
                Head = insert(Head,path);
            listFilesRecursively(path,suffix);
        }
    }

    show(Head);
    closedir(dir);
}

int main()
{
    char path[100];
    char suffix[100];

    // Input path from user
    // Suffix Band Sentinel-2 of Type B02_10m.tif

    printf("Enter path to list files: ");
    scanf("%s",path);
    printf("Enter the wildcard: ");
    scanf("%s",suffix);
    char *SCL_suffix = "SCL_10m.tif";
    listFilesRecursively(path,suffix);
    listFilesRecursively(path,SCL_suffix);

    return 0;
}

我开始处理此代码,进行了一些更改,最重要的是,与其通过调用show(Head)递归地打印在函数listFilesRecursive中生成的链接列表,然后我想将列表返回到main函数来其他流程(同时在两个列表上重复,这是将来的工作)。

但是,这样做之后,在main()中两次递归调用了listFiles函数,我遇到了分段错误错误。

根据某些来源,如果从函数中传递或返回指向堆栈中某些内容的指针,则会发生分段错误,堆栈是存储临时变量的内存区域。我一直在检查listFIlesRecursively函数,将其中的指针弄乱了,但是我无法解决这个问题。在这个问题上会有所帮助。

这是我的代码的修改版本,导致细分错误。

include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "gdal/gdal.h"
#include "gdal/cpl_conv.h"
#include <stdio.h>
#include <time.h>


// Node Structure of Linked-List
typedef struct nlist{
    char *data;
    struct nlist *next;
}Node;

// Function to replace a string with another 
// string 
char* str_replace(char* string,char*);
void show(Node*);

// Function to insert an element to the Linked-List
Node* insert(Node *Head,value);
    Node *check;
    check = (Node *)malloc(sizeof(Node));

    if(Head == NULL){
        Head = new_string;
        Head->next = NULL;
    }
    else{
        check = Head;
        while(check->next != NULL)
            check = check->next;

        check->next = new_string;
        new_string->next = NULL;
    }
    return Head;
}

//Function to show the elements of the Linked-List
void show(Node *Head)
{
    Node *check;
    check = (Node *)malloc(sizeof(Node));
    check = Head;
    if (check == NULL){
        return;
    }

    while(check != NULL) {
        printf("%s",check->data);
        check=check->next;
    }
    printf("\n");
}

//Function to check if a string finishes with a suffix
int string_ends_with(const char * str,suffix));
}


Node *listFilesRecursively(char*,char*);

//Function to find the files in a directory based on a wildcard
Node *listFilesRecursively(char *basePath,char *suffix)
{
    char path[1000];
    struct dirent *dp;
    DIR *dir = opendir(basePath);
    Node *Head = NULL;
    Node *Head_scl = NULL;

    //if (!dir)
    //    return;

    while ((dp = readdir(dir)) != NULL)
    {
        if (strcmp(dp->d_name,suffix);
        }
    } 
    //show(Head);
    closedir(dir);
    return Head;
}


int main()
{
    char path[100];
    char suffix[100];

    // Input path from user
    // Suffix Band Sentinel-2 of Type B02_10m.tif

    printf("Enter path to list files: ");
    scanf("%s",suffix);

    Node *B02_head;
    Node *SCL_head;
    B02_head = listFilesRecursively(path,suffix);
    char *suffix_scl = "SCL_10m.tif";
    SCL_head = listFilesRecursively(path,suffix_scl);
    show(B02_head);
    show(SCL_head);
    return 0;
}
woxxx123 回答:从函数返回链接列表时出现分段错误

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3118996.html

大家都在问