string所佔用的內存問題? |
答題得分者是:Skyer
|
takdick
一般會員 發表:50 回覆:63 積分:22 註冊:2002-08-05 發送簡訊給我 |
|
Skyer
高階會員 發表:43 回覆:111 積分:120 註冊:2002-04-04 發送簡訊給我 |
引言: var s:string; begin s:='helloworld'; showmessage(inttostr(sizeof(s))); //=4; end; 請問sizeof返回的值'4'是否等於's'佔用了4byte的內存嗎? 如果是,想請問一下:一個字符不是最少需要1byte的內存嗎?為何sizeof返回的總是'4'? 請指教,謝謝.因為 string 預設是使用 AnsiString 故宣告為 string 的變數是 pointer 型態,所以看他長度一定是 4 如果要看字串長度的話,可以使用 Length() 如果要宣告固定長度的字串的話,須使用 ShortString 宣告方式 var s: string[20]; //長度為 20 的字串-- Skyer
------
-- Regards, Skyer |
takdick
一般會員 發表:50 回覆:63 積分:22 註冊:2002-08-05 發送簡訊給我 |
|
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
s1:string[20]; // length array[1..20] of char
s2:string; // dynamic string SizeOf(s1) will return the size of the array, i.e. 20 1, s1[0] stored the length since it is a short string. In the old days (e.g. TP in DOS), you can access the length of a string by its element 0. This is considered a dirty (and buggy) trick and is not recommended. SizeOf(s2) will return the size of the pointer to the dynamic string, i.e. 4 bytes.
|
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |