全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1386
推到 Plurk!
推到 Facebook!

string所佔用的內存問題?

答題得分者是:Skyer
takdick
一般會員


發表:50
回覆:63
積分:22
註冊:2002-08-05

發送簡訊給我
#1 引用回覆 回覆 發表時間:2002-11-04 10:38:30 IP:203.198.xxx.xxx 未訂閱
var s:string; begin s:='helloworld'; showmessage(inttostr(sizeof(s))); //=4; end; 請問sizeof返回的值'4'是否等於's'佔用了4byte的內存嗎? 如果是,想請問一下:一個字符不是最少需要1byte的內存嗎?為何sizeof返回的總是'4'? 請指教,謝謝.
Skyer
高階會員


發表:43
回覆:111
積分:120
註冊:2002-04-04

發送簡訊給我
#2 引用回覆 回覆 發表時間:2002-11-04 11:11:55 IP:61.217.xxx.xxx 未訂閱
引言: 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

發送簡訊給我
#3 引用回覆 回覆 發表時間:2002-11-04 17:18:42 IP:203.198.xxx.xxx 未訂閱
var s:string[20]; begin showmessage(inttostr(sizeof(s)));//=21 end; 既然sizeof返回的是pointer的長度,為何上述的s有21byte那麼多呢? sizeof得到的值到底是如何算的呢?請再指教.
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#4 引用回覆 回覆 發表時間:2002-11-04 17:29:45 IP:147.8.xxx.xxx 未訂閱
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.
系統時間:2024-04-25 21:46:42
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!