我使用 malloc() 和字符串得到了一个简单的、意想不到的结果。代码:
int main(void) {
char* b64str;
char* binStr = "00000101";
printf("Expected size of allocated structure (b64str): 8\n");
b64str = (char*)malloc((strlen(binStr)+1)*sizeof(char));
printf("Actual size of allocated structure (b64str): %d\n\n", strlen(b64str));
Run Code Online (Sandbox Code Playgroud)
输出:
Expected size of allocated structure (b64str): 8
Actual size of allocated structure (b64str): 0
Run Code Online (Sandbox Code Playgroud)
为什么?