【问题标题】:looping array of pointers doesn't give me the actual pointers and program crashes when trying to free them in c循环指针数组不给我实际的指针,当我试图在 c 中释放它们时程序崩溃
【发布时间】:2022-11-19 23:54:17
【问题描述】:

完整代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void printarray(int* array, int arraysize){
    for (int i = 0; i<arraysize; i++){
        printf("%d\n", *array);
        array++;
    }
}

void printStrArray(char** array, int arraysize){
    int j = 0;
    for (int i = 0; i<arraysize; i++){
        j = 0;
        while (array[i][j] != '\0'){
            printf("%c", array[i][j]);
            j++;
        }
        printf("\n");
    }
}
int isStringInArray(char* string, char** stringArray, int arrayLen){ // returns 1 if string is contained in the array.
    for (int i = 0; i < arrayLen; i++){
        if (strcmp(string, stringArray[i]) == 0){
            //printf("%s is equal to %s %d\n", string, stringArray[i], i);
            return 1;
        }
    }
    return 0;
}

int lenstring(char* string){ // checks string length (works only if string has null character at the end.)
    char currchar = string[0];
    int strlen = 0;
    while (currchar != '\0'){
        strlen++;
        currchar = string[strlen];
    }
    return strlen;
}

char** riassemble(char* stringa){
    char** riassembleds = calloc(1, sizeof(char*));
    char* charLen = malloc(sizeof(char));
    riassembleds[0] = charLen;
    int riassembledLen = 1;
    int stringalen = lenstring(stringa);
    char tempstring[stringalen];
    strcpy(tempstring, stringa);

    for (int i = 0; i < stringalen; i++){
        for (int j = 0; j < stringalen; j++){
            tempstring[i] = stringa[j];
            tempstring[j] = stringa[i];
            //printf("%s\n", tempstring);
            if (isStringInArray(tempstring, riassembleds, riassembledLen) == 0){
                riassembleds = realloc(riassembleds, (riassembledLen+1)*sizeof(char*));
                riassembledLen++;
                riassembleds[riassembledLen-1] = calloc(stringalen, sizeof(char));
                printf("%p\n", riassembleds[riassembledLen-1]);
                strcpy(riassembleds[riassembledLen-1], tempstring);
            }
            strcpy(tempstring, stringa);
        }
    }
    *charLen = (char)riassembledLen;

    riassembleds[0] = charLen; /*return the array with the length of the it casted into a char pointer as the first element*/
    return riassembleds;
}


int main(int argc, char *argv[]){
    char** array = riassemble("ciao");

    int arraylen = (int)(*(array[0]));
    printf("\n%d\n", arraylen);
    printStrArray(array, arraylen);

    for (int i=0; i<arraylen; i++) {
        free(array[i]);
    }
    free(array);    
    return 0;
}

我正在创建一个函数,该函数返回一个指向 char 的指针数组,其中第一个元素是一个指向转换为 char 的数组长度的指针。

当我尝试释放数组中的元素时

    char** array = riassemble("ciao"); /*Creates the array*/

    int arraylen = (int)(*(array[0])); /*Gets the length*/

    for (int i=0; i<arraylen; i++) {
        free(array[i]);
    }

程序在尝试释放此处定义的数组的第二个元素后崩溃:

riassembleds = realloc(riassembleds, (riassembledLen+1)*sizeof(char*));
riassembledLen++;
riassembleds[riassembledLen-1] = calloc(stringalen, sizeof(char));
printf("%p\n", riassembleds[riassembledLen-1]);
strcpy(riassembleds[riassembledLen-1], tempstring);

我真的不明白为什么会这样,我注意到的一件事是,如果我打印它试图释放的指针,它们与我在 riassemble 函数中分配它们后立即打印它们时不同,但除了我不知道我做错了什么。

编辑:我错了他们不一样的事实,他们实际上是,我很困惑。

【问题讨论】:

标签: arrays c pointers malloc


【解决方案1】:

您的函数lenstring 将返回不带终止空字符的字符串的长度。

因此,线

char tempstring[stringalen];

将创建一个大小足以存储字符串stringa的数组tempstring没有终止空字符。

但是,函数调用

strcpy(tempstring, stringa);

要求tempstring 足够大以存储stringa终止空字符。因此,您写信给 tempstring 越界,调用 undefined behavior

为了解决这个问题,我建议您更改行

char tempstring[stringalen];

至:

char tempstring[stringalen+1];

【讨论】:

    猜你喜欢
    • 2015-02-17
    • 1970-01-01
    • 2012-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-18
    • 1970-01-01
    相关资源
    最近更新 更多