ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
I've looked up, that a function can return without actually specifiyng what to return. All I have entered is "return" in my recursive function. The function is meant to return an array, but as far as i understand, I cannot return an array by entering: return array. Correct? So, using "return" on its own is used to stop the function. Correct?
It's that a function can return without actually specifying what to return, but only if it's a void function. If you define the function as returning a value then you should always return a value from the function. Maybe if you show your function we can give you some helpful hints.
And you really should be passing pointers around instead of arrays. Passing arrays to functions is very inefficient. This is because every time you call a function the parameters actually are a local copy of the origional (meaning they are destroyed when the function returns). So, in example:
int myfunction(int myint)
myint++; //value increased
return 0; //myint destroyed
int main(int argc, char **argv)
int i = 1; //i created and assigned the value of 1
myfunction(i); //myint created and assigned the value of i
printf("value: %d", i); //i still = 1
the output will be:
The reason is because when you call myfunction(i) a copy of i is created (myint). So you are actually working with a copy, not the origional variable. This is fine in allot of cases, but it is inefficient. It starts being a problem when you really do need to modify the origional variable, and/or when you are passing arrays. Lets say you pass an array to your function that is 1000 ints long (4000 bytes if sizeof(int) = 4). That means your program will have to copy 4000 bytes every time the function is called. It can start getting really slow and start being a big memory hog.
That is why it is better to use pointers when passing arrays. A pointer to an array will point to the start of the array in memory. So, if you did this:
int selectionSort(int *a, int f, int t)
if (a == NULL) return 0; //make sure it isn't a invalid pointer
if(f == t)
for(i = 0; i < N; i++)
if(i == 0)
hold = a[i];
smallest = i;
if(a[i] < hold)
hold = a[i];
smallest = i;
else if(i == 4)
a[smallest] = a[f];
a[f] = hold;
selectionSort(a, f + 1, t);
Then you are only making a copy of the pointer (which is either 4, 8 bytes... depending on the system and compiler). The size of the pointer never changes. So even if your array is 10000 ints long, you still only copy 4/8 bytes.
Originally posted by The_Nerd
[B]And you really should be passing pointers around instead of arrays. Passing arrays to functions is very inefficient. This is because every time you call a function the parameters actually are a local copy of the origional (meaning they are destroyed when the function returns).
Languages such as C++ and Pascal provide for this notion very easily by distinguishing between passing parameters by value and by reference. The "C" language obliges you to use explicit pointers for this purpose, while C++/Pascal do the same task without changing the notation.
When a parameter is passed "by value," a copy of the value is placed on the stack and it becomes in-effect a private-variable of the function. The function can change the value at-will because only the copy, on the stack, is ever being manipulated. These changes are not and cannot be seen by the caller.
When a parameter is passed "by reference," a pointer to the original value is placed on the stack. (Just as you did, explicitly, in "C.") All of the references to this value are accomplished indirectly, by means of the pointer on the stack. When the language (C++, Pascal) supports this concept, you simply refer to the parameter the same way whether it's being passed by value or by reference. When it does not ("C"), you must write different source-code.
In any case, when you are passing a "complex object," such as an array, you probably always want to pass it "by reference." Although implementations vary, "stack space" should be considered a limited resource... you can run out of it. As a categorical rule, "large" things should be dynamically allocated, so that the space for them is not in the stack but in the heap. This rule holds both for parameters and for local variables, both of which live in the stack.
Are you creating a sorted copy of the array or destructively sorting it?
Well, C is not *really* great at doing recursive functions.
You can do it, obviously, but it gets messy.
It's not as easy as lisp for example. You have to organise all your own storage space
You can't really return an array as such except the pointer to one passed
I notice from your code that you don't (seem to) pass in the size of the array.
Another C drawback is you can't determine the size of an array passed
as a parameter so you'll need to pass the size in too.