Arrays and Pointers

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 40
Reputation: midimatt is an unknown quantity at this point 
Solved Threads: 2
midimatt's Avatar
midimatt midimatt is offline Offline
Light Poster

Arrays and Pointers

 
0
  #1
Dec 19th, 2008
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

  1.  
  2. float array1[] = {1.0f,0.0f,0.0f,0.0f,
  3. 0.0f,1.0f,0.0f,0.0f,
  4. 0.0f,0.0f,1.0f,0.0f,
  5. 50.0f,0.0f,0.0f,1.0f};

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

  1. baseNode->setPositionMatrix(array1);
code for setPositionMatrix()
  1. void sceneObject::setPositionMatrix(float* nPositionMatrix)
  2. {
  3. positionMatrix = nPositionMatrix;
  4. }

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.
-Midi
Last edited by midimatt; Dec 19th, 2008 at 7:12 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 392
Reputation: StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light 
Solved Threads: 72
StuXYZ StuXYZ is offline Offline
Posting Whiz

Re: Arrays and Pointers

 
1
  #2
Dec 19th, 2008
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

  1.  
  2. void sceneObject::setPositionMatrix(float* nPositionMatrix)
  3. {
  4. if (!positionMatrix)
  5. positionMatrix = new float[16];
  6. for(int i=0;i<16;i++)
  7. positionMatrix[i]=nPositionMatrix[i];
  8. }

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
experience is the most expensive way to learn anything
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 40
Reputation: midimatt is an unknown quantity at this point 
Solved Threads: 2
midimatt's Avatar
midimatt midimatt is offline Offline
Light Poster

Re: Arrays and Pointers

 
0
  #3
Dec 20th, 2008
Thanks alot

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

Thanks again.

-Midi
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC