Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Passing Arrays by Value in C++?

Passing Arrays by Value in C++?
Thread Tools
Posting Junkie
Join Date: Jun 2001
Location: Washington DC
Status: Offline
Reply With Quote
Feb 5, 2002, 05:09 AM
 
Can it be done? If so, how?
     
Junior Member
Join Date: Oct 2001
Status: Offline
Reply With Quote
Feb 5, 2002, 05:21 AM
 
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
Reply With Quote
Feb 5, 2002, 05:48 AM
 
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
Reply With Quote
Feb 5, 2002, 06:32 AM
 
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
Reply With Quote
Feb 5, 2002, 07:14 AM
 
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
Reply With Quote
Feb 5, 2002, 10:26 AM
 
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
Reply With Quote
Feb 5, 2002, 07:59 PM
 
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
Reply With Quote
Feb 6, 2002, 07:21 AM
 
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.
Spectral Class
"Shedding Light on Innovation"
     
Posting Junkie
Join Date: Jun 2001
Location: Washington DC
Status: Offline
Reply With Quote
Feb 6, 2002, 09:49 AM
 
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.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 12:39 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2