【发布时间】:2014-02-11 12:56:59
【问题描述】:
我正在使用 VS2012。我宁愿从堆中为 CString 分配内存,所以给出下面的代码:
- CString csToken 是从 stack 还是 heap 分配内存?
-
我需要释放 csToken 正在使用的内存,还是会在函数终止时自动释放?
TCHAR *sAllCodes = (TCHAR *) calloc(50000,sizeof(TCHAR)); // Dynamically use Heap memory to hold all pipe-delimited codes TCHAR *sCode = (TCHAR *) calloc(128,sizeof(TCHAR)); // Dynamically use Heap memory to hold a single code DWORD i = 0; LoadAllCodes(&sAllCodes); // Custom function CString csToken; // Declare the CString variable csToken.Append(sAllCodes); // Assign the string to the Cstring variable vector<CString> vAllCodes; // Declare the vector variable vAllCodes = Split(csToken,L"|"); // Split the CString into a vector array while ( i < (DWORD) vAllCodes.size()) { if (vAllCodes[i].StringLength() > 0) // If there is a string in the vector item { _stprintf_s(sCode,128,L"%s",vAllCodes[i].GetString()); // Get the vector item and copy it into a string // Do work on sCode here... } i++; } free(sAllCodes);sAllCodes=NULL; free(sCode);sCode=NULL;
【问题讨论】:
标签: c++ memory-management heap-memory c-strings stack-memory