CString类的GetBuffer问题
叶云夕 2017-11-12 10:37:40 先看代码:
CString s= "hello";
CString m;
m=s;
LPSTR p1 = (LPSTR)(LPCSTR)s;
LPSTR p2 = (LPSTR)(LPCSTR)m;
LPSTR p3 = s.GetBuffer(0);
LPSTR p4 = (LPSTR)(LPCSTR)s;
int a=s.GetAllocLength();
LPSTR p5 = s.GetBuffer(10);
int b=s.GetAllocLength();
再看结果
+ p1 0x00557b58 "hello" char *
+ p2 0x00557b58 "hello" char *
+ p3 0x00557b88 "hello" char *
+ p4 0x00557b88 "hello" char *
p5 0x00557b88 "hello" char *
a 7 int
b 15 int
p1=p2好理解,因为刚开始时两者指向同一块内存区域。而p3处GetBuffer的参数为0,应该是小于其原本的长度,这时候不是不需要重新分配内存么,但是从p3和p4可以看出内存地址发生了改变,另外p5、a、b的值该怎么解释呢?