2011-12-21 33 views
1

編輯:好的,因爲我是太模糊較早下面有一個SSCCE:使用指針的指針爲浮點[3]與sscanf的,得到一個例外

typedef float vector[3] 

int mainLoaderFunc() { 

    char* memory = NULL; 
    size_t size = loadFile(fileName, &memory); // load model file into memory, this works, tested and true 

    // create vector arrays 
    vector *vertexArray = NULL;   
    vector *normalArray = NULL;   
    vector *textureArray = NULL;   

    loadArrays(size, memory, &vertexArray, &normalArray, &textureArray); 

    // do other stuff with arrays 

} 

void loadArrays(size_t size, char *memory, vector **vertexArray, vector **normalArray, vector **textureArray) { 

    int numVerts = 0; 
    int numNormals = 0; 
    int numTextures = 0; 

    char* p = memory;   // pointer to start of memory 
    char* e = memory + size; // pointer to end of memory 

    // count verts, normals, textures for memory allocation 
    while (p != e) { 
     if (memcmp(p, "vn", 2) == 0) { 
      numNormals++; 
     } else if (memcmp(p, "vt", 2) == 0) { 
      numTextures++; 
     } else if (memcmp(p, "v", 1) == 0) { 
      numVerts++; 
     } 
     while (*p++ != (char) 0x0A); 
    } 

    // allocate memory for vector arrays 
    *vertexArray  = new vector[numVerts]; 
    *normalArray  = new vector[numNormals]; 
    *textureArray  = new vector[numTextures]; 


    int vertexIndex = 0; 
    int normalIndex = 0; 
    int textureIndex = 0; //*** IF BREAK POINT HERE: NO EXCEPTION 

    // load data from memory into arrays 
    while (p != e) { 

     if (memcmp(p, "vn", 2) == 0) { 
      sscanf(p, "vn %f %f %f", normalArray[normalIndex][0], normalArray[normalIndex][1], normalArray[normalIndex][2]); 
      normalIndex++; 
     } else if (memcmp(p, "vt", 2) == 0) { 
      sscanf(p, "vt %f %f", textureArray[textureIndex][0], textureArray[textureIndex][1]); 
      textureIndex++; 
     } else if (memcmp(p, "v", 1) == 0) { 
      sscanf(p, "v %f %f %f", vertexArray[vertexIndex][0], vertexArray[vertexIndex][1], vertexArray[vertexIndex][2]); 
      vertexIndex++; 
     } 
     while (*p++ != (char) 0x0A); 
    } 

} 

一旦代碼擊中sscanf的一部分,我得到例外,我試着把&和*數組的infront,但我得到一個例外的方式。

+1

你可以發佈你的代碼的實際代碼片段([SSCCE](http://sscce.org/))嗎? – AusCBloke 2011-12-21 09:53:21

+3

這是C++的許多指針。你有沒有考慮通過創建一個'vector'類並使用引用或者(例如)'boost :: smart_ptr'來清理你的代碼?此外,使用'new []'在'myFunction'中分配內存似乎很困惑,爲什麼不在那裏使用'new'? – Eric 2011-12-21 09:54:32

+3

爲什麼在地球上??! – 2011-12-21 09:57:02

回答

0

sscanf使用%f時需要一個float *vertexArrayfloat ***,vertexArray[i]float **,並且vertexArray[i][j]float*。然而,這是不是你想要的,因爲vertexArray是一個指向vertex數組,所以要做到這一點,而不是:

&(*vertexArray)[i][j] 

,將讓你正確float *傳遞給sscanf

+0

@eharvest,那不是'sscanf'的工作方式。這就是'printf'的工作原理。 'sscanf'必須帶一個指針,否則不能寫出'float'! – MSN 2011-12-23 00:31:01

+0

抱歉,這是一個錯誤...缺乏專注 – 2011-12-23 00:34:52

3

我想你必須地址傳遞給sscanf

sscanf(myMemChunk, "%f %f %f", &myVector[i][0], &myVector[i][1], &myVector[i][2]); 
+0

當我這樣做時,我仍然會得到異常......也許問題不在我認爲的問題中。如果有幫助,我添加了一個SSCCE。 – kbirk 2011-12-21 20:19:44

+0

這是不正確的;你會傳遞'float **'到'sscanf',而不是'float *'。 – MSN 2011-12-23 00:31:42