 |
 |
Passing Arrays by Value in C++?
|
 |
|
 |
|
Posting Junkie
Join Date: Jun 2001
Location: Washington DC
Status:
Offline
|
|
Can it be done? If so, how?
|
|
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Oct 2001
Status:
Offline
|
|
Arrays are pointers so you can't pass by value unless you pass each individual member of the array.
|
|
I be that insane n***a from the psycho ward.
|
| |
|
|
|
 |
|
 |
|
Posting Junkie
Join Date: Jun 2001
Location: Washington DC
Status:
Offline
|
|
Damn, that's what I thought. Well then, in that case, is there any way that I can have a recursive function that passes an array to itself preserve the array at each level of recursion so that after the nth recursive step the (n-1)th step still knows what the array looked like it at the (n-1)th step?
I tried making it so that every step copied the array into a new array and then manipulated and passed that new one, but for some reason it didn't like that.
|
|
|
| |
|
|
|
 |
|
 |
|
Admin Emeritus 
Join Date: Oct 2000
Location: Boston, MA
Status:
Offline
|
|
You can just use a struct containing the array, I guess.
|
|
"Against stupidity, the gods themselves contend in vain" (Schiller)
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Jan 2001
Location: Boulder, CO, USA
Status:
Offline
|
|
Originally posted by nonhuman:
<STRONG>Damn, that's what I thought. Well then, in that case, is there any way that I can have a recursive function that passes an array to itself preserve the array at each level of recursion so that after the nth recursive step the (n-1)th step still knows what the array looked like it at the (n-1)th step?
I tried making it so that every step copied the array into a new array and then manipulated and passed that new one, but for some reason it didn't like that.</STRONG>
that last bit is how i would do it. what's it an array of? have you considered using one of the stl containers? that would make copying very simple.
|
|
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Apr 2000
Location: San Francisco, CA
Status:
Offline
|
|
Originally posted by nonhuman:
<STRONG>Damn, that's what I thought. Well then, in that case, is there any way that I can have a recursive function that passes an array to itself preserve the array at each level of recursion so that after the nth recursive step the (n-1)th step still knows what the array looked like it at the (n-1)th step?
I tried making it so that every step copied the array into a new array and then manipulated and passed that new one, but for some reason it didn't like that.</STRONG>
What do you mean by "didn't like that", as that is exactly what you'd want to do. How are you controlling the depth of recursion? Are you unwittingly making an infite loop? Such a recursive technique does have a real memory limitation.
Michael
|
|
--
Michael F. Kamprath
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Dec 2001
Location: somewhere
Status:
Offline
|
|
What on earth are you doing? You want an array of arrays? To what depth, and for what purpose? You might get a little more help if you give us an idea of what you are trying to accomplish.
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Jun 2000
Location: Dundas, Ontario, Canada
Status:
Offline
|
|
Copying at each step should work fine. Are you sure that you are doing it correctly? Make sure that you aren't just copying the array pointer since that is pointless (and is being done anyway, hence the original problem). At each step you will have to copy all of the elements from the passed array into the new one you just instantiated. This could be done rather quickly with a memcopy operation between the two array pointers (old to new) once the new one is created. If, however, the array contents are also pointers (ie, not just "int" or something) then you will have to copy them, too.
I like the above mention of using STL. If you used a vector, for example, it will be copied properly so long as it's elements are stack based. If they are heap based you will have the same problem you have now.
However, we could help more if we knew what you were doing.
Hope that helps,
Jeff.
|
|
|
| |
|
|
|
 |
|
 |
|
Posting Junkie
Join Date: Jun 2001
Location: Washington DC
Status:
Offline
|
|
Actually, as it turns out, the problem, which I was able to correct, was not caused solely by the array being passed by reference, although that was part of the problem. I'm not sure what I did wrong before, but I was able to get it to copy the array at each recursion without a problem, and then fix the other problem which was unrelated to the array, but the effects of which were consistent with the previous problem. All in all it was quite a bit of trouble to go to for such a simple program, and was caused mostly by a single, simple error that really required someone else to just look at my code with a fresh perspective to be found.
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|