數組的大小可以在程式運行時定義嗎?
不。在數組的定義中,數組的大小必須是編譯時間可知的,不能是在程式運行時才可知的。例如,假設i是一個變數,你就不能用i去定義一個數組的大小:
char array[i]; /*(notvalidc */
有些語言支援這種定義,但C語言不支援。如果C語言支援這種定義,棧就會變得更複雜,調用函數的開銷就會更大,而程式的運行速度就會明顯變慢。
如果數組的大小在編譯時間是可知的,即使它是一個非常複雜的運算式,只要它在編譯時間能被計算出來,你就可以定義它。
如果你要使用一個在程式運行時才知道其大小的數組,你可以說明一個指標,並且調用malloc()或calloc()函數從堆中為這個數組分配記憶體空間。以下是一個拷貝傳給main()函數的argv數組的例子:
例 7.15 在動行時確定大小的數組,使用了指標和malloc() /* A silly program that copies the argv array and all the pointed-to strings. Just for fun, it also deallocates all the copies. */ # include <stdlib. h> # include <string. h> int main (int argc, char* * argv) { char* * new_argv; int i; /* Since argv[0] through argv [argc] are all valid, the program needs to allocate room for argc + 1 pointers. */ new_argv = (char* * ) calloc(argc + l, sizeof (char * )); / * or malloc ((argc +1) * sizeof (char * ) ) * / printf ("allocated room for %d pointers starting at %P\n", argc + 1, new_argv); /* now copy all the strings themselves (argv[0] through argv[argc-l]) */ for (i = 0;i<argc; + +i) { / * make room for '\0' at end, too * / new_argv [i]= (char* ) malloc(strlen(argv[i]) + l); strcpy(new_argv[i], argv[i]); printf ("allocated %d bytes for new_argv[%d] at %P", "copied\"%s\"\n", strlen(argv[i]) + l, i, new_argv[i], new_argv[i]) ; } new_ argv [argc] = NULL: /* To deallocate everything, get rid of the strings (in any order), then the array of pointers. If you free the array of poiners first, you lose all reference to the copied strings. */ for (i = 0;i<argc; ++i) { free(new_argv[i]); printf ("freed new_argv[%d] at %P\n" , i, new_argv[i]) ; argv[i]=NULL; /* 習慣,見本例後面的注意 */ } free(new_argv); printf("freed new_argv itself at %P\n",new_argv); return 0; /*請參見16.4 */ }
注意:為什麼例7.5在釋放了new_argv數組中的每個元素之後,還要將這些元素賦值為NULL呢?這是一種在長期實踐的基礎上形成的習慣。在釋放了一個指標之後,你就無法再使用它原來所指向的資料了,或者說,該指標被“懸掛”起來了,它不再指向任何有用的資料。如果在釋放一個指標之後立即將它賦值為NULL,那麼,即使程式再次使用該指標,程式也不會出錯。當然,程式可能會間接引用這個null 指標,但這種錯誤在偵錯工具時就能及時被發現。此外,
程式中可能仍然有一些該指標原來的拷貝,它們仍然指向已被釋放的那部分記憶體空間,這種情況在C程式中是很自然的。總之,儘管上述這種習慣並不能解決所有問題,但確實有作用。
原文:http://lylgxy0704wht.blog.163.com/blog/static/57048039200993192852623/