Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,207 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,381 people online right now. Registration is fast and FREE... Join Now!




Get array size when the array is a parameter toa method

 
Reply to this topicStart new topic

Get array size when the array is a parameter toa method

Upka
29 May, 2008 - 04:12 PM
Post #1

New D.I.C Head
*

Joined: 29 May, 2008
Posts: 5

CODE

double bounds[6];
aRowPlant.GetBounds(bounds);
    
    ...
    
void RowPlant::GetBounds(double bounds[])
{
int d1 = sizeof(bounds);    // returns 4, not 6*8=48!!!    
int d2 = sizeof(bounds[0]);    // returns 8
int arrSize = d1 / d2;        // returns 0
    
bounds[0] = m_Xmax;
bounds[1] = m_Xmin;
bounds[2] = m_Ymax;
bounds[3] = m_Ymin;
bounds[4] = m_Zmax;
bounds[5] = m_Zmin;
}    


My code dosn't work because d1 is the size of the pointer to the array, not te size of the array. Is is possible to get the size in the function without setting the last element to -1, specifying the size in the 0th element or supplying it as another parameter?
User is offlineProfile CardPM
+Quote Post

Cerolobo
RE: Get Array Size When The Array Is A Parameter Toa Method
29 May, 2008 - 04:19 PM
Post #2

D.I.C Regular
Group Icon

Joined: 5 Apr, 2008
Posts: 440



Thanked: 31 times
My Contributions
The best thing to do is to just supply the total number of elements as another parameter. Using sizeof() to get the total number of elements in a array is never a good idea. When you declare a function like so
CODE
void RowPlant::GetBounds(double bounds[])

bounds is pretty much just a pointer. Hence, sizeof(bounds) == 4.
User is offlineProfile CardPM
+Quote Post

Upka
RE: Get Array Size When The Array Is A Parameter Toa Method
29 May, 2008 - 04:25 PM
Post #3

New D.I.C Head
*

Joined: 29 May, 2008
Posts: 5

That's what I suspect. But the code works if not in a method:
CODE


RowPlant::TagDecr m_tags[] =
    {
        {"AdditionalTag",    STRING},
        {"File",            FILE},
        {"City",            CITY}
    };
    ...
int nTags = sizeof(m_tags)/sizeof(m_tags[0]);

User is offlineProfile CardPM
+Quote Post

Cerolobo
RE: Get Array Size When The Array Is A Parameter Toa Method
29 May, 2008 - 04:31 PM
Post #4

D.I.C Regular
Group Icon

Joined: 5 Apr, 2008
Posts: 440



Thanked: 31 times
My Contributions
sizeof() is actually done at compile time, and not runtime. If you hard code a array, then sizeof() will give you the numbers you want. If it isn't hard coded, then you will be dealing with a pointer, so calling sizeof(<pointer>) will always return 4 (assuming 32bit). Regardless if you hard code the size of of a array or not, you will have the actual number of elements without using sizeof().

Since you hard coded m_tags, sizeof() will work properly, but only in that function.
User is offlineProfile CardPM
+Quote Post

perfectly.insane
RE: Get Array Size When The Array Is A Parameter Toa Method
29 May, 2008 - 04:48 PM
Post #5

D.I.C Addict
Group Icon

Joined: 22 Mar, 2008
Posts: 558



Thanked: 46 times
Dream Kudos: 25
Expert In: C/C++

My Contributions

You could make your function a template functon in order to get this to work if you wanted to:
cpp

template <int N>
void RowPlant::GetBounds(double (&bounds)[N])
{
int arrSize = N; // Returns size of fixed size array.

bounds[0] = m_Xmax;
bounds[1] = m_Xmin;
bounds[2] = m_Ymax;
bounds[3] = m_Ymin;
bounds[4] = m_Zmax;
bounds[5] = m_Zmin;
}

double bounds[6];
aRowPlant.GetBounds(bounds);


Please note that this only works with fixed size arrays where the size is known at compile time. Also note that once the type degrades to a simple pointer (inside another function that takes a double* argument), this function will not work.

If you need runtime flexibility, use std::vector.
User is offlineProfile CardPM
+Quote Post

Cerolobo
RE: Get Array Size When The Array Is A Parameter Toa Method
29 May, 2008 - 04:56 PM
Post #6

D.I.C Regular
Group Icon

Joined: 5 Apr, 2008
Posts: 440



Thanked: 31 times
My Contributions
A template will work, but why do it that way? Passing the number in as a parameter will give you the same effect, but without the additional code (a copy of the function will be generated for each value of N that is entered).
User is offlineProfile CardPM
+Quote Post

perfectly.insane
RE: Get Array Size When The Array Is A Parameter Toa Method
29 May, 2008 - 05:28 PM
Post #7

D.I.C Addict
Group Icon

Joined: 22 Mar, 2008
Posts: 558



Thanked: 46 times
Dream Kudos: 25
Expert In: C/C++

My Contributions
Possibility does not imply efficiency. I would agree with you that using an int along with would be more space-efficient provided that the number of instances in the program is greater than 1. There would also be an additional degree of freedom for error though (using std::vector would get around this).

This post has been edited by perfectly.insane: 29 May, 2008 - 05:28 PM
User is offlineProfile CardPM
+Quote Post

Upka
RE: Get Array Size When The Array Is A Parameter Toa Method
29 May, 2008 - 05:41 PM
Post #8

New D.I.C Head
*

Joined: 29 May, 2008
Posts: 5

Dear Cerolobo, I agree. A copy of the function will be generated for each value, and this is a disadvantage. But I like the template approach better - it is for lazy people. Write once and forget about the second argument.

Dear Perfectly, couldn't you please explain this (&bounds) stuff? It works, and without it, i.e. int GetBounds(double bounds[N]) it does not.

CODE

double bounds6[6];  
int n6 = GetBounds(bounds6);  /* generates the following error:
Error    1    error C2784: 'int GetBounds(double [N])' : could not deduce template argument for 'double [N]' from 'double [6]'
*/


User is offlineProfile CardPM
+Quote Post

perfectly.insane
RE: Get Array Size When The Array Is A Parameter Toa Method
29 May, 2008 - 06:01 PM
Post #9

D.I.C Addict
Group Icon

Joined: 22 Mar, 2008
Posts: 558



Thanked: 46 times
Dream Kudos: 25
Expert In: C/C++

My Contributions
QUOTE(Upka @ 29 May, 2008 - 06:41 PM) *

Dear Cerolobo, I agree. A copy of the function will be generated for each value, and this is a disadvantage. But I like the template approach better - it is for lazy people. Write once and forget about the second argument.

Dear Perfectly, couldn't you please explain this (&bounds) stuff? It works, and without it, i.e. int GetBounds(double bounds[N]) it does not.

CODE

double bounds6[6];  
int n6 = GetBounds(bounds6);  /* generates the following error:
Error    1    error C2784: 'int GetBounds(double [N])' : could not deduce template argument for 'double [N]' from 'double [6]'
*/



Well, I believe it's because if you have a template function that in which any of the template arguments are not a part of the prototype, such as in:

CODE
template <typename T, int N>
int func(const T x)
{
    return N;
}


You cannot call func(22);, as the parameter N (an int) cannot be inferred upon based on the provided arguments.

What does this have to do with this situation?

The compiler discards the array index unless the array is passed by reference in the type specification.

For example (on GCC):

cpp

void func(double x[20])
{
x[0] = 4;
}

void func2(double (&x)[20])
{
x[0] = 4;
}


int main() {
double y[3];

// This compiles
func(y);

// This does not
func2(y);

return 0;
}



So, since the compiler ignores the size given in that instance, the instantiation is also ambiguous as the N is thrown out in that instance. You could make it work by using:

GetBounds<sizeof(bounds6)/sizeof(bounds6[0])>(bounds6);

Providing the parameter explicitly, and the template parameter can be determined at compile-time. However, this is not useful.
User is offlineProfile CardPM
+Quote Post

Upka
RE: Get Array Size When The Array Is A Parameter Toa Method
30 May, 2008 - 03:34 PM
Post #10

New D.I.C Head
*

Joined: 29 May, 2008
Posts: 5

Great, thank you!
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 12:41PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month