GetBuffer和ReleaseBuffer是从其父类CSimpleStringT继承过来的。GetBuffer的作用是:“Returnsapointertotheinternalcharacterbuffer”,ReleaseBuffer的作用是:“ReleasescontrolofthebufferallocatedbyGetBuffer.”。这两个函数的常见用法如下:CStringstr;constintbufferSize=10;LPTSTRp=str.GetBuffer(bufferSize);_tcscpy_s(p,bufferSize,_T("abcd1234."));//usethebufferdirectlystr.ReleaseBuffer();//Surplus(多余的)memoryreleased,pisnowinvalid.给GetBuffer函数传递的参数bufferSize,意思是:“Theminimumsizeofthecharacterbufferincharacters.Thisvaluedoesnotincludespaceforanullterminator.”。对于调用ReleaseBuffer释放内凯肢存时,是否应该带参数,局好msdn是这样说的:“Ifyoukeeptrackofthestringlengthyourself,youshouldnotappendtheterminatingnullcharacter.Youmust,however,specifythefinalstringlengthwhenyoureleasethebufferwithReleaseBuffer.Ifyoudoappendaterminatingnullcharacter,youshouldpass–1(thedefault)forthelengthtoReleaseBuffer,andReleaseBufferwillperformastrlenonthebuffertodetermineitslength.”盯腊世。因为ReleaseBuffer函数的默认参数是-1,所以通常在调用ReleaseBuffer函数时省去-1参数的书写。
标签:releasebuffer,函数