这是错误的:
// So far so good...
filename = (char *)calloc(length, sizeof(char));
// No!!! You just leaked the previously allocated memory block!
// The `\0` at the end is also wrong.
filename = "Hello World\0";
// This is OK
key = filename + 6;
// You are writing into memory of a string literal, that's undefined behavior
*(filename + 5) = 0;
// You are freeing a string literal, that's undefined behavior too
free(filename);
至于没有段错误的部分,未定义的行为可能不会立即表现出来:例如,当您释放错误的区域时,释放本身可能会起作用,但随后的分配可能会失败。
如果您想缩短字符串,请复制并释放原始字符串:
char *filename = malloc(length); // no cast
strcpy(filename, "Hello, world"); // no \0
char *trimmed = strdup(filename+6); // Make a copy
free(filename); // Free the original
filename = trimmed; // You are done!
通常,您只能释放已分配的内容。这样做的主要原因是malloc
//将“簿记”信息calloc
存储在与返回给您的地址相关联的内存中,通常在分配地址之前的一个块中。您可以尝试伪造它,但即使它有效,该解决方案也将是脆弱且不可移植的。realloc