9 Replies - 676 Views - Last Post: 15 May 2011 - 04:50 PM Rate Topic: ***** 1 Votes

#1 Patrunjel  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 17
  • View blog
  • Posts: 298
  • Joined: 28-October 10

Why limit an argument-array in a function

Posted 15 May 2011 - 04:06 AM

Well, considering that I suck at explaining stuff, I will start with an example :
I usually use this :
 void random_function (int a[]) {; } 


But in a book (and some pieces of code) I found this :
 void random_function (int a[random_int]) {; } 


Why would people limit the argument-array's size? And this branches into two sub-questions :
1) From your experience, what are the pros and cons of doing this? I guess that in a future-use intended library this can lead to some ugly buggers.
2) The book I mentioned above is an algorithms one.So there are pieces of code not longer than 2 pages. Why would one want to use in this kinda short-didactic purpose algorithm this limitation of the array? Is this a author's code-writing habit ? Or they just want to make the readers get used to this way of writing sturdy code for future big-scale projects? (and this takes us back to question 1 )

This post has been edited by Patrunjel: 15 May 2011 - 04:08 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Why limit an argument-array in a function

#2 muballitmitte  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 174
  • View blog
  • Posts: 470
  • Joined: 05-November 08

Re: Why limit an argument-array in a function

Posted 15 May 2011 - 04:52 AM

Consider you have something like this
void random_function (int a[]){}

int arr[10]={...};
random_function(arr);


When passed as a parameter to a function arr decays to a pointer. That means that while you can access all the elements of the array via that pointer, in the function you will need to know the size of the array to be able to use it correctly. Using the size of operator will fail so most commonly beside the array you will also pass the size void random_function (int a[], int size){}.

Now since in the function actually get the address of the first element in the array (which is fine since arrays are represented using a continuous block of memory) there is no real difference between a parameter int a[] and int a[100] because you can still access all the array elements (remember you have a pointer, you just need to know the size to be sure you don`t perform out-of-bounds operations). In your book the guy probably used void random_function (int a[random_int]){} because that was the max (fixed) size of the array. For the purpose o presenting a sample code implementation of an algorithm it might be fine to use fixed, statically allocated arrays in order to keep the implementation free from non essential memory handling stuff.

This post has been edited by muballitmitte: 15 May 2011 - 04:53 AM

Was This Post Helpful? 1
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 6979
  • View blog
  • Posts: 14,605
  • Joined: 16-October 07

Re: Why limit an argument-array in a function

Posted 15 May 2011 - 04:52 AM

The number in there is meaningless. It's also misleading, since the compiler doesn't do any validation checks either way.

Test it yourself:
void foo(int a[]) { }
void bar(int a[3]) { }
int main() {
	int a[] = {1,2,3,4,5,6,7,8,9};
	foo(a);
	bar(a);
	return 0;
}


Was This Post Helpful? 2
  • +
  • -

#4 Patrunjel  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 17
  • View blog
  • Posts: 298
  • Joined: 28-October 10

Re: Why limit an argument-array in a function

Posted 15 May 2011 - 04:58 AM

Thanks for the answers.
So, should I understand that limiting the size of an argument array is a (bad) coding habit of the author?
Was This Post Helpful? 0
  • +
  • -

#5 diego_pmc  Icon User is offline

  • D.I.C Addict

Reputation: 83
  • View blog
  • Posts: 565
  • Joined: 13-May 09

Re: Why limit an argument-array in a function

Posted 15 May 2011 - 05:13 AM

In that particular case, yes it's probably not used properly. There are a few cases where you should limit the size of the array: a chess table for example will always have a size of 8x8 squares. (This is just an example and there are better ways to define a chess table than by doing something like int table[8][8], because -- as baavgai said -- the compiler doesn't check array bounds.)
Was This Post Helpful? 1
  • +
  • -

#6 Patrunjel  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 17
  • View blog
  • Posts: 298
  • Joined: 28-October 10

Re: Why limit an argument-array in a function

Posted 15 May 2011 - 05:19 AM

So if the compiler doesn't check, why should we bother writing array limits?
Let's take your example of the chess table. Why would we want to limit that? From what I understood, limiting the table[][] variable will only make the code more readable, but it wouldn't affect it at all at runtime (or when compiling).
Was This Post Helpful? 0
  • +
  • -

#7 muballitmitte  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 174
  • View blog
  • Posts: 470
  • Joined: 05-November 08

Re: Why limit an argument-array in a function

Posted 15 May 2011 - 05:23 AM

View PostPatrunjel, on 15 May 2011 - 04:58 AM, said:

So, should I understand that limiting the size of an argument array

it`s not limiting! The only time you are actually forced by the compiler to give a size is when you pass a multi-dimensional arrays like so
void random_function (int a[][random_int]){} //here you have to give
                                             //the bound of the last dimension

void random_function (int a[][]){} //error


This post has been edited by muballitmitte: 15 May 2011 - 05:24 AM

Was This Post Helpful? 0
  • +
  • -

#8 Patrunjel  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 17
  • View blog
  • Posts: 298
  • Joined: 28-October 10

Re: Why limit an argument-array in a function

Posted 15 May 2011 - 05:28 AM

Yes, ok, not limiting, sorry, I meant ...I can't find a word :D... pseudo-limiting.
I meant specifying some values for the array size (but not limiting the array size :D )
Was This Post Helpful? 0
  • +
  • -

#9 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Why limit an argument-array in a function

Posted 15 May 2011 - 10:37 AM

Errors happen when you don't document assumptions. If I write a function that expects its argument array to have exactly 512 entries and you pass an array that has less or more then errors may ensue. So it is definitely NOT a bad thing to specify a size because it documents what is supposed to be passed. It makes an assumption on your part clear to the user of the function (even if it is just you).

In addition there are times when it does matter to the compiler because the compiler needs the data to know how to deal with the arguments. For example:
void foo(int a[][3]) { }
void bar(int a[][4]) { }
int main() {
	int a[][3] = {
        {1, 2, 3},
        {2, 3, 4},
    };
	foo(a);
	bar(a); //Error
	return 0;
}


The compiler needs to know the width of the array so that it can calculate where a[i][j] is (a+sizeof(a)*(i*width+j)).

Also in C++ the size of arrays can play a role in template functions (or just when passing arrays by reference):
#include <iostream>

using std::cout;
using std::endl;

template<size_t SIZE> 
void foo( int (&array)[SIZE]) {
    cout << SIZE << endl;
}

template<> 
void foo( int (&array)[4]) {
    cout << "Special case" << endl;
}

void bar(int (&array)[3]) { cout << "three" << endl; }
void bar(int (&array)[4]) { cout << "four" << endl; }

int main() {
    int a[3] = {1, 2, 3};
    int b[] = {1, 2, 3, 4};
    foo(a);
    foo(B)/>; 
    bar(a);
    bar(B)/>;
    return 0;
}


In programming in general I would say that explicit is better than implicit. I would say that it is better to specify the expected size of an array than not.
Was This Post Helpful? 0
  • +
  • -

#10 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 6979
  • View blog
  • Posts: 14,605
  • Joined: 16-October 07

Re: Why limit an argument-array in a function

Posted 15 May 2011 - 04:50 PM

Multidimensional arrays are a different beast and outside the scope of the question (yeah, there's a pun there.)

If you require a number for documentation in a standard array, you're really just declaring your intent to be consistent. In such a case, consider using a typedef:

const int MY_LIST_1_SIZE = 9;
typedef int MyList1[MY_LIST_1_SIZE];
void foo(MyList1 list);

MyList1 a1 = {1,2,3,4,5,6,7,8,9};



Unfortunately, this is still just going to degrade into an "int *" on passing, which means if you're doing the C++ thing, operator overloading is right out. Passing arrays is messy business in most cases. To do it right, you're pretty much always passing the size along. And they don't behave consistently with other complex types, because they're their own thing. Consider wrapping them it a struct for easy passing about.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1