0
void quickSort(int a[],int s,int e){
    if(s>e){
        return;
    }

    int pivot = parti(a,s,e);
    cout<<"size "<<sizeof(a)<<endl;

When i am passing the array as argument to function, an array of size more than 4 is being treated as an array of size 4. why is it so?

Meraki
  • 3
  • 3

2 Answers2

0

You cannot "pass the array to a function". It looks like you can, but you can't.

This:

void quickSort(int a[],int s, int e)

is actually:

void quickSort(int* a,int s, int e)

And, on your system, the size of a pointer is apparently four bytes.

This is why we shall not use sizeof to determine the size of a native array, because it's easy to accidentally take the size of something else instead.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Well, you can pass an array to a function, but the syntax is horrific and you have to know the size at compile time anyway. See http://en.cppreference.com/w/cpp/language/array#Array-to-pointer_decay – N. Shead Jul 02 '16 at 13:58
  • @N.Shead: No, that's passing a reference-to-an-array to a function. – Lightness Races in Orbit Jul 02 '16 at 13:58
0

Array-to-pointer decay happens here (passing an array as function argument). It means whatever array you passed to quickSort, sizeof(a) will always return the size of the pointer to int.

songyuanyao
  • 169,198
  • 16
  • 310
  • 405