2013-05-18 1 views
-1

나는 가비지 수집을 시뮬레이트해야하는 프로젝트가 있습니다. 이는 마크 및 스윕 (sweep) 방법과 결합 된 생성 알고리즘을 사용합니다. 지금까지 코드에 표시된 구조를 설계했습니다. 문제는 코드에 메모리를 할당하는 것입니다. 저는 이제 벡터를 사용하고 있습니다. 또한 포인터를 사용하여 메모리의 시작과 메모리의 끝을 가리켜 야합니다. 나는 이것을 어떻게하는지 모른다. 제발 설계 도와주세요. 여기 내 코드가 있습니다.C++을 사용한 증분 가비지 수집 시뮬레이션

#include <iostream> 
#include <algorithm> 
#include <string> 
#include <iomanip> 
#include <limits> 
#include <stdio.h> 
#include <sstream> 
#include <vector> 


using namespace std; 
using std::stringstream; 



string pMem, comment, sGen, val,input,id,size,inits,incs; 

double pmemSize =0; 

char t[10], m[256],init[10],inc[10]; 


struct rootset { 
    double totSize; 
    double *rStrtPtr; 
    double *rEndPtr; 
    vector<double> physicalM; /* This is the size of physical memory i need to assign*/ 

    struct generations { 
    double totSize; 
    const char *genStrtPtr; 
    const char *genEndPtr; 
    int numOfGen; 
    string genName; 

    struct object { 
     double objSize; 
     const char *objStrtPtr; 
     const char *objEndPtr; 
     string id; 
     char markBit; 
     char objPtr; 
    }; 

    struct freeList { 
     double freeSpace; 
     int flNumb; 
    }; 
    }; 
}; 
int main() 
{ 
    int pmemSize; 
    cout<<" ENter the size "<<endl; 
    cin >> pmemSize; 
    vector<rootset> pRootSet; 
    pRootSet.push_back(rootset()); 
    pRootSet[0].totSize = pmemSize; 
    pRootSet[0].physicalM.reserve(pmemSize); 

    for (int s=0; s<pmemSize; ++s) 
     pRootSet[0].physicalM.push_back(s); 

    vector<double>::iterator it; 

    for(it = pRootSet[0].physicalM.begin(); it!= pRootSet[0].physicalM.end(); ++it) 
     cout <<"Printing it: " <<(*it)<<endl; 
} 

내 문제는 지금, 포인터 * rStrtPtr와 * rEndPtr을 가리 키도록하는 방법; physicalM (실제 메모리)의 첫 번째 위치 ..? 세부 정보 : 사용자가 시뮬레이션을 위해 예약 할 실제 메모리의 양을 입력합니다. 바이트 단위. 단순함을 위해 int를 사용했습니다. 할당이 최대 1GB까지 올라갈 수 있기 때문에 나중에 두 배로 변경하려고합니다. 나는 physicalM이라는 벡터를 만들었습니다. 이것은 실제 실제 메모리 블록입니다. 이것은 나중에 세대로 나누어 질 것입니다 (하위 구조 생성으로 표시됨). 사용자가 (abc = alloc(50B)); 명령을 지정할 때 하위 세대에서 abc라는 객체를 만들고 그 크기로 50MB를 할당해야합니다. (이 부분은 나중에 돌볼 것입니다). 어떤 도움을

편집 ... 감사합니다 : 나는 코드에서이 줄을 사용하여 시도하지만 난 오류가 :

pRootSet[0].rStrtPtr = &(pRootSet[0].physicalM); 

error: cannot convert ‘std::vector<double>*’ to ‘double*’ in assignment 

편집 :가 수정되었습니다. 내 rStrtPtr을 벡터로 초기화해야했다.

+0

내 디자인을 변경해야하는 경우 알려 주시기 바랍니다. – Tuffy

+1

다음은 스택 오버플로와 관련된 질문입니다. 당신이 유용하다고 생각하기를 바랍니다 : http://stackoverflow.com/questions/16474393/memory-allocation-for-incremental-garbage-collection-simulation-in-c –

+0

"어떻게 해야할지 모르겠다. 이 디자인을 도와주세요. ": 어떤 부분을 설계하는 데 도움이 필요합니까? –

답변

2

라인 pRootSet[0].rStrtPtr = &(pRootSet[0].physicalM);

읽어야합니다

pRootSet[0].rStrtPtr = &(pRootSet[0].physicalM[0]); 

또는

pRootSet[0].rStrtPtr = pRootSet[0].physicalM.data(); 

또는

pRootSet[0].rStrtPtr = &*(pRootSet[0].physicalM.begin()); 
pRootSet[0].rEndPtr = &*(pRootSet[0].physicalM.end());  // this will point to the first byte AFTER the end of the buffer. 

난 당신이 데이터 크기를 저장하는 이유를 모르겠어요 double 그래도. unsigned long은 2^32 - 1 (4294967295)의 4GB를 저장할 수 있습니다.

+0

예 .. 고맙습니다 ... – Tuffy