Hello again everyone,

Having a really annoying problem using pointers and arrays at the moment,

i'm trying to store a 4x4 matrix as a an array of 16 floats.

so i declare my array like so

float array1[] =	{1.0f,0.0f,0.0f,0.0f,

i then pass this into an object using my set function like so


code for setPositionMatrix()

void sceneObject::setPositionMatrix(float* nPositionMatrix)
	positionMatrix = nPositionMatrix;

it doesnt throw up any errors, but when it draws with the new matrix it doesnt draw properly, so i investigated and it seems that positionMatrix only holds the first value from nPositionMatrix which is being passed in.

and i'm really not sure what to do.

thanks in advance for anyone who can help me out here.

8 Years
Discussion Span
Last Post by midimatt

Looks like you are coping the pointer BUT not coping the array.
[This is often called weak-copy].
What happens is the memory for array1 is overwritten with something else and you have a copy of the memory location.
You need something like this

void sceneObject::setPositionMatrix(float* nPositionMatrix)
       if (!positionMatrix)
	  positionMatrix = new float[16];
        for(int i=0;i<16;i++)

Note: You can use memcpy but the loop is self explanatory.

If postionMatrix is defined float positionMatrix[] you will not need the if
construct. If it is not you must remember to delete [] it later

Votes + Comments
Explained this very well for me, Thank you

Thanks alot :)

Works perfectly, can't believe that had me confused for so long.

Thanks again.


This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.