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: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]);

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