Vector in C++

Started by
5 comments, last by boogyman19946 13 years, 5 months ago
When i try to do so:
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

http://mateusvitali.wordpress.com/
Advertisement
Well, the obvious answer would be to use a std::vector.
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.
// 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;


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.
but how to use it in a function?

Exemple:
float *x;void function_x(int xArray[]){    xArray = new float[num_tris];    for(int i=0; i < num_tris; i++)        xArray = tris.x;}


Right?
http://mateusvitali.wordpress.com/
Well you'd need to use a pointer to an array, which is actually a pointer to a pointer. It would be something like this:
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(&amp;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 &#111;nly 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.

In C++, you use std::vector. It's in the standard library and part of the language, boost and STL aren't even relevant. While I'd question your own lack of using std::vector, that's your prerogative, however it is the wrong thing to recommend to someone who is clearly new to this area of the language and shouldn't even be making any performance considerations right now. That is, assuming there are any with the correct compiler settings.

@OP: You code would look something like this:

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;}
Static arrays are allocated as a program starts and are of set size. This means that in order to allocate an array, you need to specify how big it is so the program can set aside the memory for it. If you don't want to allocate the array right away (if it's really big) or you simply don't know how much space you'll need (array size must be specified with a constant so it has to be a set value), you can postpone the allocation of the array and use pointers to allocate it on the fly. If the array size changes all the time, using std::vector (or other forms of containers) is usually the way to deal with the situation.

What you're doing is a static allocation and the compiler is missing the size specifier. You have this:

float x[];

but you need this

float x[size]; // Where "size" is a constant

If you want to use a vector, you need this:

std::vector<float> x;

and the compiler will be happy ^.^

:D Good luck!

Yo dawg, don't even trip.

This topic is closed to new replies.

Advertisement