这是我现在拥有的完整代码。我已经尝试了我能想到的一切,但是当我拥有
else
块的内容时,尽管代码跳过了它,但代码运行顺利,没有内存泄漏。
代码不会通过
else
块,因此对我所看到的内容来说毫无用处,但如果我清空或删除 else 块,__debugbreak
会在 free
内的 DeleteSedex
上触发。
什么原因导致这种情况发生?
sedex.h:
#ifndef KAAL_SEDEX_H
#define KAAL_SEDEX_H
#include "limits.h"
#ifdef ULLONG_MAX
#include "assert.h"
#include "crtdbg.h"
#include "stdio.h"
#include "stdlib.h"
#define MEMORY_UNINITIALIZED (size_t)(0xCCCCCCCCCCCCCCCC)
#define MEMORY_AVAILABLE(ptr) (assert(NULL != ptr && MEMORY_UNINITIALIZED != (size_t)(ptr)))
typedef char* string;
typedef long long llong;
typedef enum { true, false } bool;
typedef struct sedecimstruct {
size_t length;
llong* content;
} *sedex;
sedex CreateSedex(string source);
void DeleteSedex(sedex target);
#endif
#endif
sedex.c:
#include "sedex.h"
int main(void)
{
sedex n = CreateSedex("+");
DeleteSedex(n);
_CrtDumpMemoryLeaks();
return 0;
}
sedex CreateSedex(string source)
{
struct sedecimstruct output;
bool sgned = source[0] == '+' || source[0] == '-';
if (source == NULL || strlen(source) == 0 ||
strlen(source) == 1 && sgned)
{
output.length = 2;
output.content = calloc(output.length, sizeof(llong));
}
else
{
size_t digits = strlen(source) - sgned,
mod = digits & 0xf;
bool excess = mod > 0;
output.length = (digits >> 4) + excess + 1;
output.content = malloc(output.length * sizeof(llong));
output.content[0] = source[0] == '-';
char sedecim[17] = { 0 };
MEMORY_AVAILABLE(output.content);
for (size_t i = 1; i <= digits; i++)
{
sedecim[(i - 1) & 0xf] = source[i - 1 + sgned];
if ((i & 0xf) == 0)
{
output.content[i >> 4] = atoll(sedecim);
memset(sedecim, 0, sizeof(sedecim));
continue;
}
if (i == digits && excess)
{
output.content[output.length - 1] = atoll(sedecim);
memset(sedecim, 0, sizeof(sedecim));
}
}
}
return &output;
}
void DeleteSedex(sedex target)
{
free(target->content); // breakpoint triggers here when else is empty or removed
target->content = target->length = NULL;
}
这是拼写错误吗?
typedef enum { true, false } bool;
通常是:
typedef enum { false, true } bool;