Skip to main content
gamedev.net
🔒 Locked

C++ array help

Started by Frank Montanaro Oct 1, 2008 at 3:00 PM 3 replies 800+ views
Original Post
Frank Montanaro
Frank Montanaro
Hey, I'm having a bit of array problems in this C++ thing, I was brought up on Java so this is really puzzling me as to why I am getting these results. Here is the code (just the basic functions used) followed by my output. My Methods: GLfloat* mulMatrix( GLfloat m1[], GLfloat m2[]){ GLfloat p[16]; int i; for(i = 0; i < 16; i++){ p = m1 * m2; printf("m1[%d] * m2[%d] = %f * %f = %f, ",i,i,m1,m2,p); } return p; } void print(GLfloat m[], int s){ int i; for(i = 0; i < s; i++){ printf("%f ,",m); if(i%4 == 0) printf("\n"); } } --------------- Calling the methods: GLfloat m1[16]= {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}; GLfloat m2[16]= {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}; print(mulMatrix(m1,m2),16); ----------------- OUPUT: m1[0] * m2[0] = 1.000000 * 2.000000 = 2.000000, m1[1] * m2[1] = 2.000000 * 2.000000 = 4.000000, m1[2] * m2[2] = 3.000000 * 2.000000 = 6.000000, m1[3] * m2[3] = 4.000000 * 2.000000 = 8.000000, m1[4] * m2[4] = 5.000000 * 2.000000 = 10.000000, m1[5] * m2[5] = 6.000000 * 2.000000 = 12.000000, m1[6] * m2[6] = 7.000000 * 2.000000 = 14.000000, m1[7] * m2[7] = 8.000000 * 2.000000 = 16.000000, m1[8] * m2[8] = 9.000000 * 2.000000 = 18.000000, m1[9] * m2[9] = 10.000000 * 2.000000 = 20.000000, m1[10] * m2[10] = 11.000000 * 2.000000 = 22.000000, m1[11] * m2[11] = 12.000000 * 2.000000 = 24.000000, m1[12] * m2[12] = 13.000000 * 2.000000 = 26.000000, m1[13] * m2[13] = 14.000000 * 2.000000 = 28.000000, m1[14] * m2[14] = 15.000000 * 2.000000 = 30.000000, m1[15] * m2[15] = 16.000000 * 2.000000 = 32.000000, ----------seperated here for reading's sake 2.000000 , 0.000000 ,-1.939702 ,-1.939702 ,-1.939730 , -1.939706 ,0.000000 ,0.000000 ,0.000000 , 0.007812 ,22.000000 ,24.000000 ,26.000000 , freeglut (./plant): Unable to create direct context rendering for window 'plant' This may hurt performance. 28.000000 ,30.000000 ,32.000000 , ----------------------------- So, It seems to be doing all the stuff right inside of the method, but then when calling the print method, I get some funky answers except for the last 6 spaces in the array. I'm pretty confused as to why it isn't working. it works for another method I have that multiples a vector x matrix but not for this one which is matrix x matrix. Thanks, Frank
rufus74
rufus74
GLfloat* mulMatrix( GLfloat m1[], GLfloat m2[]){GLfloat p[16];int i;for(i = 0; i < 16; i++){p = m1 * m2;printf("m1[%d] * m2[%d] = %f * %f = %f, ",i,i,m1,m2,p);}return p;} 


You are returning a variable that is temporary created inside your method. It goes out of scope.

I would do something like this:
void mulMatrix( GLfloat m1[], GLfloat m2[], GLfloat* p){int i;for(i = 0; i < 16; i++){p = m1 * m2;printf("m1[%d] * m2[%d] = %f * %f = %f, ",i,i,m1,m2,p);}} // .....GLfloat p[16];mulMatrix(m1,m2, p);print(p,16);


cheers.
Frank Montanaro
Frank Montanaro
Ahh so you pass the pointer instead of returning one! Sweet, thanks man!

-Frank
KulSeran
KulSeran
You should make note that that isn't how matrices multiply together.
Wiki on Matrix
owiley
owiley
you return one but you have to grab the whole memory else its just a value and not a reference. I think is how it works but i know its possible cause this is how how you return arrays
Bring more Pain

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.