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

匿名 (未验证) 提交于 2019-12-03 03:03:02

问题:

Possible Duplicates:
Sizeof an array in the C programming language?
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 

What am I doing wrong?

回答1:

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



回答2:

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  void makeVectorData(float (&p_vertData)[N]) {     int num = (sizeof(p_vertData)/sizeof(p_verData[0]));   cout 

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.



回答3:

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.



回答4:

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]).



回答5:

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.



回答6:

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      


回答7:

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  // 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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!