0
#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.

Rakshit Kothari
  • 399
  • 1
  • 5
  • 16
  • 2
    In your example, `buffer` is global so you don't need to pass it as a parameter to your other functions. – Tom Fenech Mar 03 '14 at 13:41
  • @TomFenech - Even if the function is defined in other C files? – Rakshit Kothari Mar 03 '14 at 13:44
  • If the function is defined elsewhere then the situation changes. You could pass it as a parameter, or declare it in a shared header that both files `#include`. – Tom Fenech Mar 03 '14 at 13:59
  • @TomFenech - But then repeatedly calling the array as a parameter into functions would be inefficient. Say I have 500 calls of 'myfunc' and 1000 calls of 'myfuncinfunc'. Wouldn't it be better to NOT pass it as a parameter for all those iterations? As someone below answered, it creates a pointer to the first location in our stack. If this operation is repeated for so many calls, wouldn't it be inefficient? – Rakshit Kothari Mar 03 '14 at 14:02
  • 1
    @RakshitKothari Only if you have 1000 recursive function calls. Then it would be terribly inefficient. For 1000 normal function calls, you will only reserve space for one pointer. – Lundin Mar 03 '14 at 14:22
  • I think you're running the risk of premature optimisation. It is unlikely that passing a pointer around would be the bottleneck in any nontrivial example. – Tom Fenech Mar 03 '14 at 14:24

2 Answers2

2
  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).

Medinoc
  • 6,577
  • 20
  • 42
1
  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.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • Got it! As I've asked in my comment above, can you tell me if it's better if I globally attach my array to all C files? passing array as a parameter on every iteration uses the stack again and again; could I call the parameter directly into any C file which uses array? – Rakshit Kothari Mar 03 '14 at 14:09
  • 1
    @RakshitKothari It is very bad practice to use global variables, pass it as parameter. The only thing allocated to the stack is a single pointer variable, which takes so little space that it is neglectable. The compiler might even optimize it away, so let the compiler worry about the efficiency. – Lundin Mar 03 '14 at 14:21