传递三重指针以在另一个函数中分配内存,sscanf异常 [英] Passing a triple pointer to allocate memory in another function, sscanf exception

查看:317
本文介绍了传递三重指针以在另一个函数中分配内存,sscanf异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在另一个函数中将内存分配给双指针,因此我需要使用指向指针的指针。我得到一个异常抛出时,我使用sscanf,我不知道确切的原因。代码的代码片段。这是工作更早,当它都在同一个函数,我只需要使用双指针,但现在,重构代码和使用三重指针im有这个问题。

Im allocating memory to a double pointer in another function, therefore I need to use a pointer to the pointer to the pointer. Im getting an exception thrown when i use sscanf, im not sure exactly why. Heres a snippet of the code. This was working earlier when it was all in the same function and i only needed to use double pointers, but now that im refactoring the code and using triple pointers im having this issue.

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

Once the code hits the sscanf part, i get the exception:

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


推荐答案

正如有人指出,使用三个指针。

As someone pointed out, you really shouldn't be using triple pointers here.

此外,你可能想做你的文件扫描作为除了链接if块之外的东西(或至少将它拆分成函数)。

Additionally, you might want to do your file scanning as something other than chained if-blocks (or at least split it up into functions).

这应该解决你的问题,因为你只有2个dereferences你应该有3:

This should fix your issue, since you only have 2 dereferences where you should have 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++;
    } 

这篇关于传递三重指针以在另一个函数中分配内存,sscanf异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆