开发者

size of array passed to C++ function? [duplicate]

This question already has answers here: 开发者_StackOverflow中文版 Closed 12 years ago.

Possible Duplicate:

determine size of array if passed to function

How can I get the size of a C++ array that is passed to a function?

In the following code, the sizeof(p_vertData) does not return the correct size of the array.

float verts[] = {
 -1.0,1.0,1.0,
 1.0,1.0,1.0,
 1.0,-1.0,1.0,
 -1.0,-1.0,1.0,

 -1.0,1.0,-1.0,
 1.0,1.0,-1.0,
 1.0,-1.0,-1.0,
 -1.0,-1.0,-1.0
};

void makeVectorData(float p_vertData[]) {   
 int num = (sizeof(p_vertData)/sizeof(int)); 
 cout << "output: " << num << endl;
}; 

What am I doing wrong?


You can't - arrays decay to pointers when passed as function parameters so sizeof is not going to help you.


If you don't mind templates, you can do as follows. Note that it will work only if the array size is known at compile time.

template <int N>
void makeVectorData(float (&p_vertData)[N]) {   
 int num = (sizeof(p_vertData)/sizeof(p_verData[0])); 
 cout << "output: " << num << endl;
};

Beware also that you should divide sizeof(array) by an array element size. In your example, you're dividing the size of an array of float by the size of an integer.


In your example, it is effectively passing a pointer to the method; there is no size information. It would be necessary to pass the size from the calling method.


You can't divide by sizeof(float) if the array is not generated at compile time. However, in this case, it is, so the compiler is aware of the size of the array. Something to keep in mind.


Agree with Mark and Paul. You will have to pass the size of the array along with the array function argument itself.

As a side note, when declaring static constant arrays, I tend to group the definition and another "size" value together. For example, say if I have your float array defined as a static constant global (e.g., scoped to a local .cpp file), then I would define a corresponding "size" value as follows:


static const float VERTS[] = {
 -1.0, 1.0, 1.0,
  1.0, 1.0, 1.0,
  1.0,-1.0, 1.0,
  1.0,-1.0, 1.0,
 -1.0, 1.0,-1.0,
  1.0, 1.0,-1.0,
  1.0,-1.0,-1.0,
 -1.0,-1.0,-1.0
};
static const unsigned int VERTS_SIZE = sizeof(VERTS) / sizeof(VERTS[0]);
This will allow me to easily iterate over the contents of the vertices without having determine the size every time.

for (unsigned int i = 0; i < VERTS_SIZE; i++)
     float fValue = VERTS[i];


By using templates:

// old function, the type is the same as Type pointer[]
// you need extra size parameter
void do_something( Type* pointer, size_t size ){ };  

//this template will catch reference to array, rather then just bar pointer:
template < int size > // size must be compile time expression
inline void do_something( Type (&array) [ size ] ) // this is tricky, this & is needed 
{
    do_something( array, size ); // array is implicitly cast to pointer
}

//now you can use it:
Type data[10];
do_something(data);

//but when using dynamic arrays, you need to add size parameter:
int some_size = getSomeSize();
Type *pointer_to_data= new Type[some_size];
do_something(pointer_to_data,some_size);
delete[] pointer_to_data; //newer forget  to delete, or use smart pointers. 

You need this trick with reference to prevent array being implicit cast to pointer. Extra Template is to prevent original function being compiled many times when only the size changed.


You cannot pass a vector as argument to a function.

Actually, you can, but the function always accepts it as a pointer (to the first element), regardless of whether you write (float *data) or (float data[]).

Pass the size as a separate argument, using sizeof(verts)/sizeof(verts[0]).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜