float x[];
The compiler shows the following error:
Quote:error C2133: 'x' : unknown size
How do I do? Since I do not know what size should I allocate to the vector
float x[];
Quote:error C2133: 'x' : unknown size
// declare the pointerfloat * x;// later when you need to allocate itx = new float[size];// do your stuff// you're done, need to deallocate itdelete [] x;
float *x;void function_x(int xArray[]){ xArray = new float[num_tris]; for(int i=0; i < num_tris; i++) xArray = tris.x;}
float * pAnArray = NULL;void function_x(float ** ppArray){ *ppArray = new float[num_tris]; for(i = 0; i < num_tris; i ++) *ppArray = tris.x;<br>}<br><br><span class="cpp-keyword">void</span> my_calling_function()<br>{<br> function_x(&pAnArray);<br>}<br><br><br></pre></div><!–ENDSCRIPT–><br><br>Now, if you do this, you must be REALLY CAREFUL to make sure to delete the array when you're done with it. Otherwise, you'll have memory loss. If it's allocated in an object, put the deallocation in the destructor, and do it right after typing the new so you don't forget.<br><br>EDIT:<br>Or use std::vector like siCrane suggested. You'll only feel the performance loss if you use this really often. I had performance trouble with the STL in my draw call batching functions in a really huge maps with 2,304 tiles. In normal cases, it works just fine.
Quote:Original post by Bearhugger
I suppose you're used to the way array work in Java and C#. In C++, you make dynamically allocated arrays using pointers, and allocate (and deallocate) the array manually.
*** Source Snippet Removed ***
While I personally prefer doing it the old way, Boost and the STL provide classes to automate this process if you're not at ease with it, albeit at a slight performance cost.
std::vector<int> x; // Contiguous, resizable array of integers. Currently empty// Pass by reference, will modify vector being passed in as opposed to making a copyvoid function_x(std::vector<int>& y){ y.resize(num_tries); // Resizes the array to now have num_tries default-initialized integers // Fill in some data for(size_t i = 0; i < y.size(); i++) y = tris.x;}