| | |
Arrays and Pointers
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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
i then pass this into an object using my set function like so
code for setPositionMatrix()
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
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
C++ Syntax (Toggle Plain Text)
float array1[] = {1.0f,0.0f,0.0f,0.0f, 0.0f,1.0f,0.0f,0.0f, 0.0f,0.0f,1.0f,0.0f, 50.0f,0.0f,0.0f,1.0f};
i then pass this into an object using my set function like so
c++ Syntax (Toggle Plain Text)
baseNode->setPositionMatrix(array1);
c++ Syntax (Toggle Plain Text)
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.
-Midi
Last edited by midimatt; Dec 19th, 2008 at 7:12 pm.
•
•
Join Date: Nov 2008
Posts: 397
Reputation:
Solved Threads: 72
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
Note: You can use memcpy but the loop is self explanatory.
If postionMatrix is defined
construct. If it is not you must remember to
[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
c++ Syntax (Toggle Plain Text)
void sceneObject::setPositionMatrix(float* nPositionMatrix) { if (!positionMatrix) positionMatrix = new float[16]; for(int i=0;i<16;i++) positionMatrix[i]=nPositionMatrix[i]; }
Note: You can use memcpy but the loop is self explanatory.
If postionMatrix is defined
float positionMatrix[] you will not need the ifconstruct. If it is not you must remember to
delete [] it later experience is the most expensive way to learn anything
![]() |
Similar Threads
- What relation does **indirection operator have with Multidimensional Arrays (C++)
- Sorting arrays of pointers with function? (C)
- Comparing arrays (C++)
Other Threads in the C++ Forum
- Previous Thread: retrieving handles to windows
- Next Thread: Help with implementing the binary ADT using a vector, C++
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib library linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return string strings struct studio system template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






