Rakshit Kothari
Rakshit Kothari

Reputation: 416

Is passing a static array to a function efficient?

#define BUFF_SIZE 100000
unsigned char buffer[BUFF_SIZE];

void myfunc(unsigned char[],int,int);
void myfuncinfunc(unsigned char[],int,int);

int main()
{
int a = 10, b = 10;
myfunc(buffer,a,b);
}

void myfunc(unsigned char array[],int a,int b)
{
int m,n;
//blah blah
myfuncinfunc(array,m,n);
}

void myfuncinfunc(unsigned char array[],int a, int b)
{
//blah blah
}

I wish to know the following:

  1. I have created a static array as seen above the 'main' function. Is this efficient? Would it be better if I used a point and malloc instead?

  2. I know it doesn't use the stack, so when I pass the array into inner functions, would it create a copy of the whole array or just send the location of the first entry?

  3. When working on 'array' in the function 'myfunc', am I working directly with the static defined array or some local copy?

  4. Inside the function 'myfunc', when we pass the array into the function 'myfuncinfunc', would it again, send only the first location or a complete copy of the array into the stack?

Thanks for reading the question and would greatly appreciate any help! I'm new to C and trying to learn it off the internet.

Upvotes: 0

Views: 2306

Answers (2)

Lundin
Lundin

Reputation: 213513

  1. I have created a static array as seen above the 'main' function. Is this efficient? Would it be better if I used a point and malloc instead?

Define "efficient". Statically allocated arrays are always faster than dynamic ones, because of the runtime overhead for allocation/deallocation.

In this case, you allocate a huge amount of 100k bytes, which might be very memory-inefficient.

In addition, your process might not have that much static memory available, depending on OS. On desktop systems, it is therefore considered best practice to allocate on the heap whenever you are using large amounts of data.

  1. I know it doesn't use the stack, so when I pass the array into inner functions, would it create a copy of the whole array or just send the location of the first entry?

You can't pass arrays by value in C. So a pointer to the first element of the array will be saved on the stack and passed to the function.

  1. When working on 'array' in the function 'myfunc', am I working directly with the static defined array or some local copy?

Directly on the static array. Again, you can't pass arrays by value.

Inside the function 'myfunc', when we pass the array into the function 'myfuncinfunc', would it again, send only the first location or a complete copy of the array into the stack?

A pointer to the first element.

Upvotes: 1

Medinoc
Medinoc

Reputation: 6608

  1. I don't see how it would be more or less efficient than an array on the heap.
  2. It decays into a pointer to the first entry.
  3. Therefore it's not a local copy, it's the array itself.
  4. Ditto.

By the way, if a and b are indexes within the array, consider using the size_t type for them (it's an unsigned int guaranteed big enough for indexing arrays).

Upvotes: 2

Related Questions