何时释放变量decodeSet1?
我为decodeSet1和decodeSet2分配了内存。。。我可以释放decodeSet2,但当释放decodeSet1并提交我的代码以尝试它时,会出现以下错误“双重释放或损坏(out):0x0000000002046020***…但当我在valgrind中运行代码时,它不会给我任何错误 这是我的密码:何时释放变量decodeSet1?,c,malloc,valgrind,free,C,Malloc,Valgrind,Free,我为decodeSet1和decodeSet2分配了内存。。。我可以释放decodeSet2,但当释放decodeSet1并提交我的代码以尝试它时,会出现以下错误“双重释放或损坏(out):0x0000000002046020***…但当我在valgrind中运行代码时,它不会给我任何错误 这是我的密码: #define __STDC_FORMAT_MACROS #include <inttypes.h> #include <stdlib.h> #include <
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
#include <stdlib.h>
#include <stdio.h> // size_t type
#include <string.h> // C strings
#include <stdint.h> // uint64_t type
#define NUL '\0'
const size_t SETSIZE = sizeof( uint64_t ) << 3 ;
const size_t BUFSIZE = 256;
const char encodingTable[] = {'z', 'y', 'x', 'w', 'v', 'u', 't', 's',
'r', 'q', 'p', 'o', 'n', 'm', 'l', 'k',
'j', 'i', 'h', 'g', 'f', 'e', 'd', 'c',
'b', 'a', ',', '9', '8', '7', '6', '5',
'4', '3', '2', '1', '0', '.', 'Z', 'Y',
'X', 'W', 'V', 'U', 'T', 'S', 'R', 'Q',
'P', 'O', 'N', 'M', 'L', 'K', 'J', 'I',
'H', 'G', 'F', 'E', 'D', 'C', 'B', 'A'};
// Encodes a string
//
// @param st a string to be encoded
uint64_t set_encode( char * st ) {
unsigned long long bitMask = 1;
uint64_t set = 0;
for(size_t x = 0; x < strlen(st); x ++){
int indx = strchr(encodingTable, st[x]) - encodingTable;
if(indx >= 0){
set |= bitMask << indx;
}
}
return set;
}
// The intersection of two sets
//
// @param set1 a set of characters from files or a string
// @param set2 a set of characters from files or a string
uint64_t set_intersect( uint64_t set1, uint64_t set2 ) {
uint64_t set = 0;
set = set1 & set2;
return set;
}
// The union of two sets
//
// @param set1 a set of characters from files or a string
// @param set2 a set of characters from files or a string
uint64_t set_union( uint64_t set1, uint64_t set2 ) {
uint64_t set = 0;
set = set1 | set2;
return set;
}
// The complement of a set
//
// @param set1 a set of characters from files or a string
uint64_t set_complement( uint64_t set1 ) {
uint64_t set = 0;
set = ~set1;
return set;
}
// Takes the difference of set1 and ~set2
//
// @param set1 a set of characters from files or a string
// @param set2 a set of characters from files or a string
uint64_t set_difference( uint64_t set1, uint64_t set2 ) {
uint64_t set = 0;
set = set1 & ~set2;
return set;
}
// Takes the difference of the union of set1 and set2 and
// the intersect of set1 and set2.
//
// @param set1 a set of characters from files or a string
// @param set2 a set of characters from files or a string
uint64_t set_symdifference( uint64_t set1, uint64_t set2 ) {
uint64_t set = 0;
set = set_union(set1, set2) - set_intersect(set1, set2);
return set;
}
// Takes the cardinality of a set (number of 1's in its binary
// number.
//
// @param set a set of characters from files or a string
size_t set_cardinality( uint64_t set ) {
size_t cnt = 0;
for (size_t x = 0; x < SETSIZE; x++){
set >>= 1UL;
if (set & 1){
cnt++;
}
}
return cnt;
}
// Decodes a set
//
// @param set a set of characters from files or a string
char * set_decode( uint64_t set ) {
size_t lengthOfSet = set_cardinality(set);
size_t indx = 0;
char *bits = malloc(lengthOfSet + 1);
bits[lengthOfSet] = NUL;
for (size_t cnt = 0; set; set >>= 1){
if (set & 1){
bits[lengthOfSet - indx - 1] = encodingTable[cnt];
indx += 1;
}
cnt += 1;
}
return bits;
}
// Reads a string from a file and encodes it
//
// @param fp file to be read
uint64_t file_set_encode( FILE * fp ) {
char buf[BUFSIZE];
uint64_t set = 0;
while (fgets(buf, BUFSIZ, fp) != NULL){
uint64_t tmpBuf = set_encode(buf);
set |= tmpBuf;
}
fclose(fp);
return set;
}
int main( int argc, char *argv[] ){
int args;
uint64_t set1 = 0;
uint64_t set2 = 0;
args = argc - 1;
char *decodeSet1 = NULL;
char *decodeSet2 = NULL;
if (args < 2 || args > 2){
fprintf(stderr, "Usage: file-bitsets string1 string2");
exit(EXIT_FAILURE);
} else {
FILE *pfile1 = fopen(argv[1], "r");
if (pfile1){
set1 = file_set_encode(pfile1);
printf("string1: %s\tEncoding the file: %s\n", argv[1], argv[1]);
} else {
set1 = set_encode(argv[1]);
printf("string1: %s\tEncoding the string: %s\n", argv[1], argv[1]);
}
FILE *pfile2 = fopen(argv[2], "r");
if (pfile2){
set2 = file_set_encode(pfile2);
printf("string2: %s\tEncoding the file: %s\n\n", argv[2], argv[2]);
} else {
set2 = set_encode(argv[2]);
printf("string2: %s\tEncoding the string: %s\n\n", argv[2], argv[2]);
}
printf("set1: %#018lx\n", set1);
printf("set2: %#018lx\n\n", set2);
printf("set_intersect: %#018lx\n", set_intersect(set1, set2));
printf("set_union: %#018lx\n\n", set_union(set1, set2));
printf("set1 set_complement: %#018lx\n", set_complement(set1));
printf("set2 set_complement: %#018lx\n\n", set_complement(set2));
printf("set_difference: %#018lx\n", set_difference(set1, set2));
printf("set_symdifference: %#018lx\n\n", set_symdifference(set1, set2));
printf("set1 set_cardinality: %zu\n", set_cardinality(set1));
printf("set2 set_cardinality: %zu\n\n", set_cardinality(set2));
decodeSet1 = set_decode(set1);
decodeSet2 = set_decode(set2);
printf("members of set1: '%s'\n", decodeSet1);
printf("members of set2: '%s'\n", decodeSet2);
}
-> free(decodeSet1);
free(decodeSet2);
}
\define\u STDC\u FORMAT\u宏
#包括
#包括
#包括//size\u t类型
#包含//C字符串
#包括//uint64\u t类型
#定义NUL'\0'
const size\u t SETSIZE=sizeof(uint64\u t)=0{
set |=bitMask有一个微妙的错误,您可以通过调用set_decode(1)
set_基数(1)
在检查set&1
是否为真之前,将返回0
然后,您char*bits=malloc(1);
后跟:
if (1 & 1){
bits[0 - 0 - 1] = encodingTable[cnt];
Oops,写入位[-1]
破坏堆,使您看到错误。获取设置_基数
以返回奇数的正确值的修复方法应该是显而易见的,因此我将让您自己来做。与其堆积如山的代码,不如跟随并提供一个?您有一个不吉利的打字错误。如果您谈论ar,那么@AndrewMedico是什么意思free(decodeSet1)前面的行;我故意放在那里。您应该创建一个函数来读取文件或处理参数,该函数应该关闭它打开的文件(而不是使用file\u set\u encode()
关闭文件)。这与双重释放无关。你应该在循环结束之前释放decodeSet1
和decodeSet2
,而不是在循环结束之后。仔细阅读你的代码。我不知道你的意思。set_基数会返回奇数和偶数的正确数字。你会如何修复它?我不知道。请重新阅读我的anSWER,遵循<代码> StIGCARDALILITY()/Case>流在纸上(<代码> 5)/代码>可能是一个很好的使用价值,并考虑如果你以不同的顺序做事情会发生什么。
if (1 & 1){
bits[0 - 0 - 1] = encodingTable[cnt];