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

Pages: 1-

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:01

OP here, forgot to mention it's just a one dimensional array.

Name: Anonymous 2010-12-18 0:06

I have a minor problem with C++.
C++.

Looks like you have a major problem.

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


First of all, you declare the function to return a void pointer but you don't return.
Second, my_array[0] is a float*, because my_array should be a float**.
Third, ou don't need the ; after the closing brace.
Fourth, use [code] tags
Fifth, try with:

void readdata(float *a, int l) {
    a = new float[l];
}

float *my_array;
readdata(my_array, 256);
my_array[0] = 1.2f;
printf("%f\n", my_array[0]);

Name: >>3 2010-12-18 0:07

Forgot to say:
Don't use C++, learn C.

Name: Anonymous 2010-12-18 0:24

Thanks for replying, man. Luckily, the code I posted was pseudo code. I'm new to /prog/... so I didn't know there about tags.

What I'm trying to do is:

A) Pass a pointer from the outside of the function
B) Allocate memory to the pointer inside the function
C) Alter the memory inside the function
D) Use the data outside of the function


void* ReadData(float** array, int length){
*array = new float[length];
for(int i = 0; i<maxValue; i++){
//...
*array[curValue++] = value;
//...
}
};


Outside of the function I would access the data like so:

ReadData(&my_array, 1234);

Unfortunately, the function seems to be releasing my data once it returns which was why I was passing the pointer to the function like that.

Do you know what I mean?

Name: Anonymous 2010-12-18 0:28

...dammit.

I would access like so:


ReadData(&my_array, 1234);

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


Seems like I'm just going to have to do it with either a global (ugh) or copy and pasting the same tired ass code 50 times throughout my program... or write it as a #define macro maybe.

Name: Anonymous 2010-12-18 0:36

>>6
I still don't understand why you're using a pointer to pointer to floats, when you're clearly using it as simple pointer to float.
Basically, you're printing the address that my_array[0] points as float.

Name: Anonymous 2010-12-18 0:46

>>7

I'm trying to do it with an array so it's actually a pointer to an array of floats rather than a pointer to a pointer to a float.

I'm doing this so that I can talk to the data outside of the function without losing the data once the function returns.


float* ColladaLoader::ReadFloatArray(char* string, float* my_array, int length){
    char* character = new char[256];
    int curValue = 0;
    int curChar = 0;
    int curVert = 0;
 
    my_array = new float[length];

    printf("array: %p\n", my_array);
      
      for(int i = 0; i<strlen(string); i++){

        if(string[i]==0x20||i==strlen(string)-1){
           
             if(i==strlen(string)-1){
             character[curChar] = string[i];
             curChar++;
             }
            
             character[curChar] = 0x00;

             my_array[curValue] = (float)atof(character);
//            printf("%f\n", *my_array[curValue]);

             character = new char[256];
             curChar = 0;
             continue;
             }
            
             character[curChar] = string[i];
             curChar++;
      }
    };


That's the code I'm using right now... it's been molested a few times so it's pretty awful looking. If you can see it, I'm actually trying it the way you're suggesting but it still doesn't work.

[CODE]

void* ColladaLoader::ReadPositionData(TiXmlElement* data){
    //read
    char* positionText = (char*)data->FirstChildElement("float_array")->GetText();
    numPositions = 0;
    data->FirstChildElement("float_array")->Attribute("count", &numPositions);
   
    Positions_Array = new float[numPositions];
    ZeroMemory(Positions_Array, numPositions*sizeof(float));
   
    printf("BEFORE: %p\n", Positions_Array);
   
    float * test = ReadFloatArray(positionText, Positions_Array, numPositions);
   
    printf("test: %p\n", test);
   
    for(int i = 0; i<numPositions; i++){
    printf("%f\n", Positions_Array[i]);
    }
    };

[CODE]

Try not to mind the junk. It's a simple collada mesh loader using tinyxml.

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
}

Name: Anonymous 2010-12-18 1:00

>>9
Oops, I meant

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

Name: Anonymous 2010-12-18 1:03

My advice has already been said, in >>4.

Name: Anonymous 2010-12-18 1:08

>>9

{assuming you meant for me to return the array to float * test from ReadFloatArray}

Nope, the function releases the data on the return as expected.

test[0] actually comes up with accurate data... but the rest is junk just like before.

But yeah, I was gonna put a "free" in there somewhere. Eventually. :)

Name: Anonymous 2010-12-18 1:11

>>12
test[0] actually comes up with accurate data... but the rest is junk just like before.
That's strange.
Are you sure that the problem is ReadFloatArray and not something else?

Name: Anonymous 2010-12-18 1:11

>>12

Whoops, sry, I meant a delete somewhere in there.

>>11

...dually noted.

Name: Anonymous 2010-12-18 1:12

>>13

That's a possiblity.

Name: Anonymous 2010-12-18 1:16

>>15

Wait, no, when I'm in readfloatarray I can print out the entire array. It's the outside of the function that becomes a problem. I've always had this problem and I've never known EXACTLY how to fix it.

...because it's killing my data at the return because it's local, which is why I need dynamic allocation to a pointer outside and pass it inside and let the data stay there.

http://www.eskimo.com/~scs/cclass/int/sx5.html

I read this thing up and down left to right slant wise included and I still have nothing working.

I could do a static var... but idk.

Name: Anonymous 2010-12-18 1:22

>>16
I'm not a C++ expert, that's the best I could do.
My last suggestion is >>4 or D.

Name: Anonymous 2010-12-18 1:27

>>17

That's alright, I appreciate your assistance. Static var doesn't work either apparently.

You wouldn't happen to have Bjarne Stroustrup's phone number would ya? He's got some explainin' to do.

Name: Anonymous 2010-12-18 1:41

...looks like I'm gonna have to make a macro till I figure this stuff out.

FUCK, even the macro does it. WTF man... wtf.

Name: Anonymous 2010-12-18 1:54

>>19
I feel sorry for you.

(Do you really need to use C++? Seriously, it's a fucking inconsistent madness, switch to something else.)

Name: sage 2010-12-18 1:54

HOLY FUCK HOLY FUCK HOLY FUCK!!1

Guess what... do you wanna know what was wrong? THE WHOLE FUCKING TIME. You wanna know? Really?!

FUCK! FUCKFUCKFUCKFUCK FUCK FUCK FFFFFFUUUUUU!!!

...i forgot to increment curValue. LMFAO!!1

Oh boy... I surely do love programming. Still doesn't solve the mystery of what I was trying to accomplish in the beginning but at least now I'll know if it's working or not. FUCK!

Name: Anonymous 2010-12-18 1:58

>>20

I feel sorry for me too. Sometimes man... I swear to god, I could kick my own ass.

Name: Anonymous 2010-12-18 2:01

mfw this thread

Name: Anonymous 2010-12-18 2:02

>>23

mfw that post

Name: Anonymous 2010-12-18 2:11

mfw mfw

Name: Anonymous 2010-12-18 2:25

>implying mfw

Name: Anonymous 2010-12-18 2:27

>>26

mfw implying implications

Name: Anonymous 2010-12-18 2:27

`>mfw implying mfw

Name: Anonymous 2010-12-18 2:34

>>27
nice

Name: Anonymous 2010-12-18 2:34

>>28
I lol'd

Name: Anonymous 2010-12-18 2:36

>>29,30
Not this shit again.

Name: Anonymous 2010-12-18 3:30

I'd just like to point out that >>3 doesn't understand pointers, and would be equally lost in pure C as he is in ``C with news''.

Name: Anonymous 2010-12-18 3:55

>>31
nice.

Name: Anonymous 2010-12-18 3:55

>>31
I lol'd

Name: VIPPER 2010-12-18 5:28

>>32
C with JEWS

Name: Anonymous 2010-12-18 8:03

float *ReadData (int whatever) { return new float[whatever]; }

float *my_array = ReadData (1234);

Name: Anonymous 2010-12-18 9:26

float * ReadData (float **array, FILE * input); would be pretty sweet

Name: Anonymous 2010-12-18 12:03

>>32
With malloc() I never had such problems, all those news confuse me.

Name: Anonymous 2010-12-18 12:03

>>37
Isn't that a bit redundant?

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