Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C++ Pointers and Memory Allocation Quandry

Name: Anonymous 2010-12-17 23:52

Hello /prog/, I have a minor problem with C++.

I'm trying to do this:

void* ReadData(float** array, int length){
*array = new float[length];
};

I call:

ReadData(&my_array, 1234);

..but when I try to read the values I get junk.

printf("%f\n", my_array[0]);

I've tried allocating the memory before passing the pointer to the function but it just doesn't care and kills whatever data I was working with.

I've tried pretty much every trick I know aside from a global variable or taking my code out of a function entirely.

Halp?

Name: Anonymous 2010-12-18 0:57

Try with

float *ColladaLoader::ReadFloatArray(char *string, int length)
{
    char* character = new char[256];
    int curValue = 0, curChar = 0, curVert = 0;
    float* array = new float[length];
   
    for (int i = 0, strlength = strlen(string) ; i < strlength; ++i)
    {
    if (string[i] == 0x20 || i == strlength-1)
    {
        character[curChar] = string[i];
        ++curChar;
    }

    character[curChar] = 0;

    array[curValue] = (float)atof(character);

    character = new char[256]; // are you sure about this? you don't delete[] the previous pointer
    curChar = 0;
    continue;
    }
    character[curChar] = string[i];
    ++curChar;
}

void *ColladaLoader::ReadPositionData(TiXmlElement *data)
{
    char *positionText = (char*)data->FirsrtChildElement("float_array")->GetText();
    numPositions = 0;
    data->FirsrtChildElement("float_array")->Attribute("count", &numPositions);

    // etc

    float *test = ReadFloatArray(positionText, Positions_Array, numPositions);

    printf("test: %p [0]: %f\n", test, test[0]);

    // etc
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List