我在这里浏览strlen代码,想知道是否确实需要代码中使用的优化?例如,为什么下面这样的东西不能同样好或更好? unsigned long strlen(char s[]) { unsigned long i; for (i = 0; s[i] != '\0'; i++) continue; return i; } 较简单的代码对编译器进行优化是否更好或更容易? strlen链接后面页面上的代码如下所示: /* Copyright (C) 1991, 1993, 1997, 2000, 2003 Free Software Foundation, Inc. This file is part of the GNU C Library. Written by Torbjorn Granlund (tege@sics.se), with …
我想知道如何malloc和free工作。 int main() { unsigned char *p = (unsigned char*)malloc(4*sizeof(unsigned char)); memset(p,0,4); strcpy((char*)p,"abcdabcd"); // **deliberately storing 8bytes** cout << p; free(p); // Obvious Crash, but I need how it works and why crash. cout << p; return 0; } 如果可能的话,我很感激如果答案在内存级别。