Thursday, July 30, 2009

Why can't functions in C return an Array without using the pointer method??

Because the C language has no built-in "Array" data type.





To mimic a "collection of things" in C, the address (pointer) to the first in a line of items is passed around. This is also very efficient, since moving large amounts of memory as a single array of like types is much slower than simply passing around the memory address of the items, and they stay put.





To know the end of an array, either the length is also passed around, or as with strings, a terminating null is appended to the end. This is quite convienent, so that in many programs, one will find an array has a NULL (0) in it's last position instead of a full data type (say, if you were using an array of custom struct's). this allows one to skip passing the array pointer and the length everywhere.





Arrays are traversed by the compiler's machine code by knowing 2 things: The index you're looking for, and the size of each item in the array. By adding up the bytes of a single entry, say "size", it performs the simple math of "index * size" to jump to the position in memory that is Array[index].





In C, arrays are no different than performing this arithmetic yourself (called pointer arithmetic): If you have a an array:





SomeDataType* arraySDT = malloc( sizeof(SomeDataType) * 100);





[ 100 spaces reserved for something of "SomeDataType" size]





...then...





SomeDataType* pointToPosition10 = arraySDT * (sizeof(SomeDataType) * 9);





SomeDataType* pointAlsoToPosition10 = arraySDT[9];





both of the above are equivalent, since in the first you do the math to jump ahead of the base pointer into the array's memory. In the second, the compiler has done the math. Using the second syntax is advised.





[ remember, to subtract one from that position you want since all arrays begin at position 0. ]

Why can't functions in C return an Array without using the pointer method??
It has to deal with the origins of C, and lower level assembly language. C was originally meant to be an abstracted cross platform version of ASM with a lot of simplification. Now when a function returns in C (as with most programming languages) on x86 processors, the return value is set in the eax register (a register is a 32 or 64 bit peice of memory that the processor has, it only has a few, but they work extremely fast, much faster than computer ram).





Now, the register can either contain a value, or the address of data. The reason why you can't return an array, is because you're controlling the computer at a much lower level than you realize (unlike PHP or VB). Doing things at a lower level give you control over how things are implemented, and allow you to greater refine your code for speed.
Reply:That would be too much data to put on the Return stack. The pointer method is a much better way to control and conserver memory space, and prevent programming errors caused by leaky RAM segments and left over memory space that is left allocated.





Good luck
Reply:Thats because an when you reference an array name you are actually referencing a memory location.The only way to reference a memory location is via a pointer,hence you will need to use a pointer to access the value stored in the array
Reply:Unlike in more modern languages, C does not actually contain a what we know today as an array. Rather, it is a smoke-and-mirrors trick by the compiler involving pointer arithmetic using given offsets. Thus, the only way to reference an array (that is, a chunk of memory that can be accessed in a convenient way) is by passing the base pointer.


No comments:

Post a Comment