하나의 견고한 메모리 덩어리에 할당 된이 구조체의 배열이 필요합니다. "char * extension"및 "char * type"의 길이는 컴파일시에는 알 수 없습니다.구조체의 배열에 메모리 덩어리를 할당하십시오.
struct MIMETYPE
{
char *extension;
char *type;
};
"new"연산자를 사용하여 각 요소를 자체적으로 초기화하면 메모리가 흩어질 수 있습니다. 내가 액세스하려고 할 때, 데이터가 모든 순서를 벗어 나는이 같은에서 데이터를로드 할 때,
//numTypes = total elements of array
//maxExtension and maxType are the needed lengths for the (char*) in the struct
//std::string ext, type;
unsigned int size = (maxExtension+1 + maxType+1) * numTypes;
mimeTypes = (MIMETYPE*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
을하지만 흩어져 : 이것은 내가 그것을 위해 메모리를 하나의 연속 된 블록을 할당하려고 시도하는 방법이다 나중에.
for(unsigned int i = 0; i < numTypes; i++)
{
//get data from file
getline(fin, line);
stringstream parser.str(line);
parser >> ext >> type;
//point the pointers at a spot in the memory that I allocated
mimeTypes[i].extension = (char*)(&mimeTypes[i]);
mimeTypes[i].type = (char*)((&mimeTypes[i]) + maxExtension);
//copy the data into the elements
strcpy(mimeTypes[i].extension, ext.c_str());
strcpy(mimeTypes[i].type, type.c_str());
}
누구든지 나를 도울 수 있습니까?
편집 : 나는하지 않습니다 (
1) MIMETYPE의 배열 및
2 관리)
될 수있는 문자 배열을 관리 :
unsigned int size = (maxExtension+1 + maxType+1);
mimeTypes = (MIMETYPE*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size * numTypes);
for(unsigned int i = 0; i < numTypes; i++)
strcpy((char*)(mimeTypes + (i*size)), ext.c_str());
strcpy((char*)(mimeTypes + (i*size) + (maxExtension+1)), type.c_str());
'std :: vector'와'std :: string'을 사용할 수없는 이유가 있을까요? – GManNickG
나는 GMan과 함께하고있다. 왜이 트릭을하고 싶니? 이것이 필요한 최적화라고 측정 했습니까? – sbi
웹 서버용입니다. 연결된 각 사용자에 대해 각 요청에 대해 비교해야 할 수도있는 총 646 개의 유형이 있습니다. 진짜 느려질 수 있어요. – bitwise