2011-12-21 58 views
1

將內存分配給另一個函數中的雙指針,因此我需要使用指針指向指針。我得到一個異常,當我使用sscanf時,我不知道爲什麼。下面是代碼的一小部分。這是早期工作時,它是在同一個函數,我只需要使用雙指針,但現在即時通訊重構代碼和使用三重指針即時通訊有這個問題。在另一個函數中傳遞三指針來分配內存,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]; 

    p = memory; 

    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的一部分,我得到異常:

Unhandled exception at 0x5e9cf2dc (msvcr100d.dll) in derp.exe: 0xC0000005: Access violation writing location 0xcccccccc. 
+9

如果你使用的是C++,並且你正在使用三指針,那麼你幾乎肯定會出錯...... – 2011-12-21 21:24:59

+0

這段代碼是如何到達sscanf部分的?當(p!= e)循環之後,當然你需要重置p來指向內存,否則第二個while(p!= e)循環將不會進入一次,因爲直到p == e 。 – 2011-12-21 21:36:50

+0

另外,將p前進到'\ n'(0x0a)是非常危險的,然後回到假設你還沒有達到p == e。也許你直接通過e尋找'\ n' – 2011-12-21 21:38:48

回答

1

正如有人指出的那樣,你真的不應該在這裏使用三層指針。

此外,您可能希望將文件掃描作爲除鏈接if-blocks之外的其他功能(或至少將其分割爲函數)。

這應該可以解決你的問題,因爲你只有2個指針引用,你應該有3個:

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++; 
    } 
1

它們傳遞給sscanf()時,您沒有正確取消引用矢量指針。試試這個:

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; 
} 
2

你傳遞無效地址到sscanf。看看你的函數的參數之一:

vector **vertexArray 

vertexArray點從mainLoaderFunc傳遞一個指針的地址。 *vertexArray是指一個指向實際vector數組,你知道,當你爲它分配內存:

*vertexArray = new vector[numVerts]; 

因此,數組中的有效要素是:

(*vertexArray)[vertexIndex][0] 

注意*,你的代碼丟失; *vertexArray指的是實際的數組。現在sscanf想要一個地址給這個變量,所以現在得到它的地址(括號內額外增加了清晰起見):

&((*vertexArray)[vertexIndex][0]) 

更改所有的參數sscanf是這樣的。