Sunday, June 7, 2015

Pass an array to a function when you're allowed to pass only one parameter?

A colleague asked me this, and from my experience, if you pass an array to a function, there's no way for the function to know the array size because it does not know at which point of memory the array ends. So the only way seemed to be to do this:

You'd ask then how does the compiler know where an array ends when you create:int* myArray = new myArray[10];

and then delete it with:delete[] myArray;

Apparently, when the memory is allocated with new myArray[10], the compiler will allocate space to store the size of the array + memory to store the array itself. What is returned, is the position where the array begins.
So when it's time to delete, the compiler searches for the array size in array position - 1. This is one way to implement it.

So for ordinary arrays being passed as parameters to a function, one way is to create an array of size 11 if you actually wanted to create an array of size 10. Then use the first cell to store the array size.#include<iostream>void func(int arr[]) { int s = arr[0]; for(int i=1; i<s+1; ++i) { std::cout<<arr[i]<<"\n"; }}int main(){ int a[11] = {10, 1,2,3,4,5,6,7,8,9,0}; func(a);}

No comments:

Special Posts

Etcetera

You can call me a versatile creator. I'm known for quickly adopting new technologies and creating good quality working products with them. Apart from spending most of my life getting people to pronounce my name correctly, I also have a taste for fun, adventure and unprejudiced thought.