8 Replies - 890 Views - Last Post: 12 March 2011 - 04:22 PM Rate Topic: -----

#1 marinus  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 138
  • View blog
  • Posts: 575
  • Joined: 14-April 10

Why is this returning wrong length?

Posted 12 March 2011 - 11:56 AM

Hi to all

int GetLength(int arr[])
{
int lng = sizeof(arr) / sizeof(int);
return lng;
}


int main()
{


	int x[7]; 

	//int lng = sizeof(x) / sizeof(int);
	
	int lng = 
        GetLength(x);




	for(int i = 0 ; i < lng ; i++)
	{
		x[i] = i;

		cout << x[i];

	}

	system("pause");
        return 0;



}




WHY is this above GetLength() function returning 1 instead of 7 ?

i am so confused !

Thx for you're time

I am also a beginner on C++ so be nice :)

This post has been edited by marinus: 12 March 2011 - 12:01 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Why is this returning wrong length?

#2 KYA  Icon User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3199
  • View blog
  • Posts: 19,230
  • Joined: 14-September 07

Re: Why is this returning wrong length?

Posted 12 March 2011 - 12:01 PM

C++ inherited this "feature" from C:

When an array is passed as a parameter, it is degraded to a pointer. So sizeof(arr) once it hits the function is 4 bytes (on most platforms).

4/4 = 1
Was This Post Helpful? 1
  • +
  • -

#3 marinus  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 138
  • View blog
  • Posts: 575
  • Joined: 14-April 10

Re: Why is this returning wrong length?

Posted 12 March 2011 - 12:03 PM

How can i reconvert the pointer ?
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3199
  • View blog
  • Posts: 19,230
  • Joined: 14-September 07

Re: Why is this returning wrong length?

Posted 12 March 2011 - 12:04 PM

Can't. Any information regarding it as an array is lost. This is why you'll see most functions pass in a size parameter in addition to the array.
Was This Post Helpful? 0
  • +
  • -

#5 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1397
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Why is this returning wrong length?

Posted 12 March 2011 - 12:08 PM

> int GetLength(int arr[])

int arr[] is the same as int *arr. You are passing in a pointer. You can't directly pass in an array to a function. Hint, there's no such thing as an array without a size. That should have been a clue.

You can use a template function to get the array length, but you probably don't know how to construct one. Google a bit and you will find it.

Here's why you get 1 for the length. sizeof(arr)/sizeof(int) is the same as sizeof(int*) / sizeof(int). So the questions are what are the implementation sizes of int* and int for your compiler? I imagine it is 4 and 4 (pointer is 4 bytes and integer is 4 bytes).
Was This Post Helpful? 1
  • +
  • -

#6 marinus  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 138
  • View blog
  • Posts: 575
  • Joined: 14-April 10

Re: Why is this returning wrong length?

Posted 12 March 2011 - 12:08 PM

Quote

This is why you'll see most functions pass in a size parameter in addition to the array.


Could you give me an example of what you mean ?

This post has been edited by marinus: 12 March 2011 - 12:09 PM

Was This Post Helpful? 0
  • +
  • -

#7 KYA  Icon User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3199
  • View blog
  • Posts: 19,230
  • Joined: 14-September 07

Re: Why is this returning wrong length?

Posted 12 March 2011 - 12:18 PM

int sumArray(int arr[], int size){
     int total = 0;
     for(int i = 0; i < size; i++){
          total += arr[i];
     }
     return total;
}


Was This Post Helpful? 0
  • +
  • -

#8 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 944
  • View blog
  • Posts: 2,464
  • Joined: 20-August 07

Re: Why is this returning wrong length?

Posted 12 March 2011 - 03:18 PM

One way you could do this is to pass the array by-reference instead of by-pointer. The template keyword can do a bit of magic for you to get the compiler to figure out how big your array is.
template <int N>
int GetLength(int (&arr)[N])
{
    return N;
}

#include <iostream>

int main()
{
    int x[7]; 

    int lng = GetLength(x);

    for(int i = 0 ; i < lng ; i++)
    {
        x[i] = i;
        std::cout << x[i];
    }
} 




In days gone by, C programmers would have done something similar using a macro
#define MAC__ARRAY__LENGTH(x) (sizeof(x)/sizeof(x[0])) 
although there are hidden 'gotchas' involved with macros like this, such as the fact that its not type-checked, so you could end up doing it on something which is not an array and getting weird results.


However, since you're using C++, you have an even better, simple, easy solution to all your problems called vector, which is a "C++ array" that knows its own size, and won't mess you around by converting to a pointer every time you look at it funny.
#include <iostream>
#include <vector>

int main()
{
    std::vector<int> x(7); 

    int lng = x.size();

    for(int i = 0 ; i < lng ; i++)
    {
        x[i] = i;
        std::cout << x[i];
    }
} 
In general (especially as a beginner) you should prefer vectors over arrays. If you are already familiar with the basic concept of arrays, then you already know almost enough to use vectors straight away (syntax is very similar, although there are differences).

This post has been edited by Bench: 12 March 2011 - 03:27 PM

Was This Post Helpful? 1
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: Why is this returning wrong length?

Posted 12 March 2011 - 04:22 PM

Also read the second tutorial link in my signature.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1