• Advertisement
Sign in to follow this  

Vector in C++

This topic is 2738 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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 pointer
float * x;

// later when you need to allocate it
x = new float[size];

// do your stuff

// you're done, need to deallocate it
delete [] 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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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[ i ] = tris.x;
}

void my_calling_function()
{
function_x(&pAnArray);
}




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.

EDIT:
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.

Share this post


Link to post
Share on other sites
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 copy
void 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;
}

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement