因爲我已經完成了一些C操作,所以需要一段時間,我必須重新理解指針。這是一個修改指針內容的函數。問題是這個代碼是否正確。如果我只釋放指針或是否必須釋放指針的內容,那麼這就足夠了嗎?正確的釋放內存的方法
void foo(char **str) {
// str has been allocated enough bytes for the following text
*str = "allocate some text";
}
int main(int arc, char *argv[]) {
char *someString;
foo(&someString);
free(someString); // is this the correct place to free
}
謝謝。
我相信你函數調用應該是'foo(&someString);'... – 2011-05-12 13:03:59
這將是foo(&someString)。 – Mel 2011-05-12 13:04:14
你是對的。 Editted。 – ALOToverflow 2011-05-12 13:05:26