临时指针:正确的 malloc 和 free

Temp Pointers: Correct malloc and free

我的想法是在函数的开头声明一个 char *temp 指针,可以反复使用。

起初,我一开始使用malloc,最后使用free()。问题是我复制到 temp 的第一个字符串是所有字符串中最长的。因此,当我打印第一个字符串之后的字符串时,第一个字符串的末尾仍然存在: 例如。 第一个字符串是 "hello",下一个是 "no" -> 我将 temp 写入一个 FILE,结果是 nollo

为了解决这个问题,我决定在每次将字符串写入文件后都使用 free() 以便为下一个字符串释放存储空间。这导致了错误 "error for object 0x7fe274801000: pointer being freed was not allocated".

所以我的想法都没有奏效...处理这个问题的最佳方法是什么?谢谢!!

void hexdump (FILE *output, char *buffer, int length){
int offset = 0;
int rows = (length%16)==0 ? (length/16) : ((length/16)+1);
char *temp = (char *)malloc(MAX_OUTPUT);

for(int j = 0; j<rows; j++){
    offset = offset + 16;
    sprintf(temp, "%06x : ", offset);
    fwrite(temp, sizeof(char), sizeof(temp), output);
    printf("%06x : ", offset);
    free(temp);
    for(int i = 0; i<16; i++){
        if ((j == rows-1) && (!buffer[i+(j*16)])){
            strcpy(temp,"   ");
            fwrite(temp, sizeof(char), sizeof(temp), output);
            printf("   ");
            free(temp);
        } else {
        sprintf(temp, "%02x ", buffer[i+(j*16)]);
        fwrite(temp, sizeof(char), sizeof(temp), output);
        printf("%02x ", buffer[i+(j*16)]);
        free(temp);
        }
    }
    strcpy(temp, "  ");
    fwrite(temp, sizeof(char), sizeof(temp), output);
    printf("  ");
    free(temp);
    for(int n = 0; n<16; n++){
        if ((j == rows-1) && (!buffer[n+(j*16)])){
            strcpy(temp, " ");
            fwrite(temp, sizeof(char), sizeof(temp), output);
            printf(" ");
            free(temp);
        } else {
                if((buffer[n+(j*16)]>31) && (buffer[n+(j*16)]<127)){
                sprintf(temp, "%c", buffer[n+(j*16)]);
                fwrite(temp, sizeof(char), sizeof(temp), output);
                printf("%c", buffer[n+(j*16)]);
                free(temp);
            } else {
                strcpy(temp, ".");
                fwrite(temp, sizeof(char), sizeof(temp), output);
                printf(".");
                free(temp);
            }
        }
    }
    strcpy(temp, "\n");
    fwrite(temp, sizeof(char), sizeof(temp), output);
    printf("\n");
    free(temp);

}

}

printf 输出正确,而输出文件不正确..

当你free一个指针指向的内存时,它就不再有效使用了。您需要再次调用 malloc 以获得有效的内存块。但即使你这样做了,真正的问题是你没有写正确的数量。

你所有的文章都是这样的:

fwrite(temp, sizeof(char), sizeof(temp), output);

这里,sizeof(temp)不是temp中包含的字符串的长度,而是temp的大小,这是一个指针,很可能是 4 或 8。

改用strlen,它会给出字符串的长度:

fwrite(temp, sizeof(char), strlen(temp), output);

或者更好的是,只需 fprint 写入文件而不是 sprintf 后跟 fwrite。例如,更改为:

sprintf(temp, "%06x : ", offset);
fwrite(temp, sizeof(char), sizeof(temp), output);

为此:

fprintf(output, "%06x : ", offset);

然后你可以完全摆脱 temp 和相关的 mallocfree 调用。

您可能想在 temp 上使用 realloc()

char *temp = malloc(size);
//Do some stuff with temp
temp = realloc(temp, newSize);//instead of free(temp);
//Clear the string (fill it with [=10=]) if you want to have a proper new string.
//Do some stuff, and so on until :
free(temp);
//Eventually some more code, WITHOUT using temp
return();

realloc(ptr, val); 就像 free(ptr); ptr = malloc(val);

希望对您有所帮助!

编辑:

我知道OP已经解决了,这只是另一种可能的解决方案。