![]() |
|
#1
|
|||
|
|||
How to retrieve arrays from functions?i get a an unusual no: by executing the following code.By execution it seems to me that by performing a return statement,the function re-executes itself.Can anyone explain that and the proper way to do it?
Thanks. CPP / C++ / C Code:
|
|||
|
#2
|
|||
|
|||
Re: How to retrieve arrays from functions?Quote:
Quote:
Quote:
If the value of count is larger than the size of the array, then depending upon the platform, the return address placed on the stack may be corrupted because it is being overwritten. This would account for why you are seeing execution go into the weeds upon executing the function. |
|
#3
|
|||
|
|||
Re: How to retrieve arrays from functions?Are you saying that the array i have created is not a dynamic one but a static array of a size unknown to me which may be larger or smaller than
CPP / C++ / C Code:
Quote:
|
|
#4
|
|||
|
|||
Re: How to retrieve arrays from functions?Regarding dire consequences, think of it this way:
You make an array of size 100 of ints. CPP / C++ / C Code:
The thing is, basically this is a chunk of memory that is the size of 100 ints. Now, do you know what comes after this chunk of memory? What piece of memory exists directly after the array? Nope. Neither do we. That's where the dire consequences are. Should you do something such as write at right after the point where that chunk of memory ends, you could be over writing something other variable that's critical to some other part of your program, or worse another program. Imagine you have your program running, and attempt to print the value of an int only to find garbage because another program overwrote it. Unrealistic real life simile: Imagine you're driving your car. Your car contains many mechanical pieces. Now imagine, for some reason, someone elses car is made and because of the way it's made, it suddenly causes your tire to pop while you drive at 60 miles per hour or become a smaller tire than you currently have on which just screws with the handling. Think of your programs like cars driving through your computer, and one just screws up the others tires. That's the dire consequence of not using an array properly (or at least the way I see it). |
|
#5
|
||||
|
||||
Re: How to retrieve arrays from functions?Quote:
Quote:
Quote:
CPP / C++ / C Code:
Quote:
Last edited by ocicat : 12-Sep-2008 at 14:12.
|
|
#6
|
|||
|
|||
Re: How to retrieve arrays from functions?Ok ,my mistake was that i was trying to overwrite into a memory location that is not even assigned to
CPP / C++ / C Code:
But Ocicat ,the function CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
by slightly altering your program like this:: CPP / C++ / C Code:
But you would still find on execution that there is still the presence of that unknown number.Could you tell me why? I am using a Dev compiler on a Windows XP platform. |
|
#7
|
|||
|
|||
Re: How to retrieve arrays from functions?Quote:
You also mention that you get some funky value. Look at what you are actually returning -- a[k] is the first element past the end of the array. If the number of array elements is 4, the range of valid subscripts is from 0 - 3. a[4] is invalid in that it has not legally been allocated nor has a value been assigned to this location. Errors made/assumed with 0-based subscripting is a common mistake made by newcomers to C and/or C++ alike. It appears that you have made a number of erroneous assumptions about how C++ treats arrays. Other languages deal with them differently by usually ascribing much more functionality. Given that C & C++ are both languages which intentionally abstract the underlying assembly language (& little more...), arrays are merely blocks of memory which are indexed from the array's beginning address. This is why 0-based indexing is used. All elements are described relative to the beginning address of the array. Last edited by ocicat : 13-Sep-2008 at 02:16.
|
|
#8
|
|||
|
|||
Re: How to retrieve arrays from functions?Returning
CPP / C++ / C Code:
CPP / C++ / C Code:
|
|
#9
|
|||
|
|||
Re: How to retrieve arrays from functions?Quote:
Quote:
The last statement in the function: CPP / C++ / C Code:
The statement:: CPP / C++ / C Code:
It appears that you are making the assumption that the following line of code: CPP / C++ / C Code:
I can only guess that what you are wanting to do is something similar to the following: CPP / C++ / C Code:
|
|
#10
|
|||
|
|||
Re: How to retrieve arrays from functions?Quote:
CPP / C++ / C Code:
It is not clear whether you understand that passing the array to the function is accomplished by merely passing its beginning address. This is why I question the value of returning the array from the function. |
Recent GIDBlog
Programming ebook direct download available by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Arrays as function arguments - return arrays from functions | pisuke | C Programming Language | 2 | 25-Jul-2007 12:03 |
| Pointers, Functions, arrays | allican57@yahoo | C++ Forum | 12 | 10-Nov-2006 15:17 |
| Shapes Functions Version 2 - Arrays! | Cecil | C Programming Language | 1 | 09-Jul-2006 21:39 |
| structure arrays... how to pass to functions? | tones1986 | C++ Forum | 2 | 08-May-2006 02:50 |
| Noob question on c arrays and functions | brett | C Programming Language | 1 | 20-Apr-2005 04:59 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The