#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char *p ;
char *q ;
char *l = "aaaaaasdfsfdqwecvxcxfwdsagfd" ;
p = (char*)malloc(sizeof(l)) ;
q = (char*)malloc(strlen(l)) ;
strcpy(p, l) ;
strcpy(q, l) ;
printf("%s\n",p) ;
printf("%s\n",q) ;
printf("%d\n",strlen(l)) ;
return 0 ;
}
請看看這個,在23個字符內輸入一些字符的時候,沒問題,輸入的字符超過23個,就像圖片顯示的那樣,我的結果顯示亂碼,爲什麼會這樣? enter image description herec sizeof()會導致溢出?
不要將代碼作爲圖像發佈,這太可怕了。而你沒有分配足夠的內存,缺少null-termination字符。可能是這個問題,但現在我必須進行眼科手術,所以你會原諒我。 –
我很抱歉,這是我第一次問stackoverflow問題,我會解決它。謝謝。我不認爲插件空終止字符在這裏工作,它已經存在了 –