• Advertisement
Sign in to follow this  

array return value

This topic is 3839 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

ok, im a bit of a novice at this, so hope this isnt too simple a question. Im using calls to OpenGL in C to create a physics simulation of charged particles in an electromagnetic field. I need to return arrays of vertices after performing motion calculations. Previously, I merely changed the vertices using a for loop in a function, but at this juncture it would be considerably simpler to just have an array return value for the function as a whole. Does anybody have any experience with this? I need to be able to pass an array as one of many arguments to the function, perform mathematical alterations on its members based on forces, and then return that array's new value, then continue onto the next in a series of 24 arrays holding vertex data. Any help is much appreciated, thanks

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by noviceprogmr84
ok, im a bit of a novice at this, so hope this isnt too simple a question. Im using calls to OpenGL in C to create a physics simulation of charged particles in an electromagnetic field. I need to return arrays of vertices after performing motion calculations. Previously, I merely changed the vertices using a for loop in a function, but at this juncture it would be considerably simpler to just have an array return value for the function as a whole. Does anybody have any experience with this? I need to be able to pass an array as one of many arguments to the function, perform mathematical alterations on its members based on forces, and then return that array's new value, then continue onto the next in a series of 24 arrays holding vertex data. Any help is much appreciated, thanks
There are ways to return an array from a function, but they can be expensive (in both C and C++) and error-prone (especially in C).

Others might disagree, but I would suggest having the function modify the array in place, or, if you want to preserve the previous values, pass in both the 'source' and 'target' arrays as arguments and have the modified values written to the target array. On the caller's side, you can just maintain two arrays of the appropriate size, and alternate which is the 'source' array and which is the 'target' array.

In C++ you'd have a few more options, but the above method might still be preferable due to the potential cost involved in returning the array by value (I'm assuming the number of particles is not fixed).

Share this post


Link to post
Share on other sites
Neither C nor C++ are well suited to returning arrays, sadly. If you don't want to modify your existing array, have your function take two arrays (a source and a destination) as your arguments.

C++ Example:

void times2( std::vector<int> const& source, std::vector<int> & dest ) {
dest.resize(source.size());
for ( unsigned i = 0 ; i < source.size() ; ++i ) {
dest[i] = source[i]*2;
}
}



Using std::transform:


int times2f( int i ) { return i*2; }

void times2( std::vector<int> const& source, std::vector<int> & dest ) {
dest.resize(source.size());
std::transform( source.begin(), source.end(), dest.begin(), std::ptr_fun( &times2f ) );
}



Usage for either:

int main() {
std::vector<int> source, dest;

for ( unsigned i = 1 ; i <= 5 ; ++i ) { source.push_back(i); }
// source == { 1, 2, 3, 4, 5 }

times2( source, dest );
// dest == { 2, 4, 6, 8, 10 }
}


Share this post


Link to post
Share on other sites
Yes, although the compiler may be able to perform NRVO on a vector, I wouldn't want to be that dependant on it doing so.

MaulingMonkey demonstrates some good options well.

Share this post


Link to post
Share on other sites
Quote:
Originally posted via PM by noviceprogmr84:
hey, thanks for the reply to my post on getting a return value of array from a function, but do u have any example code in C that would work, thats what Im working with right now, and Im a beginner and havent learned C++ yet, just C, OpenGL and Java, so any sample code in C would be much appreciated


C is basically a retarded version of the already rather evil language C++. While I could show you the more complex, more error prone C version, it would feel like selling a pack of cigarettes to a toddler -- just not kosher. Especially if you can't reason out the translation yourself.

Since C++ is largely an extension of C, I really must recommend taking this opportunity to start learning the basics of C++. Having been in your position myself, my biggest regret is that I didn't do the same myself earlier.

Share this post


Link to post
Share on other sites

Hi,

The Right Way to do this depends upon if you need to save the values of the original array, or if you can just overwrite the old vertex array with the updated values.

If you can just overwrite the original vertex array with the new values, just do that inside your function without worrying about returning an entirely new array.

However, if you need to save the values, then you can do something like this (this is expensive to do in a loop, though):


Vertex* move_vertices(Vertex const* originals, unsigned num_vertices)
{
int i; /* Errm, just your friendly neighborhood loop index... */

/* Allocate a new array: */
Vertex* updated = (Vertex*) malloc(sizeof(Vertex) * num_vertices);
for(i = 0; i < num_vertices; ++i) {

/* Make a copy of the vertex (not strictly necessary, just for ease of presentation). */
Vertex v = originals[i];
updated[i] = move_vertex(v);
}

/* Done, return the pointer: */
return updated;
}

unsigned num_vertices = 0;
Vertex* originals = 0;
Vertex* updated = 0;

/* ... initialize 'originals' and 'num_vertices', then: */
updated = move_vertices(originals, num_vertices);

/* ... do some other stuff, then make sure to release the memory for 'updated': */
free(updated);




Hope this helps!
Cheers, Demian

Quote:
Original post by noviceprogmr84
ok, im a bit of a novice at this, so hope this isnt too simple a question. Im using calls to OpenGL in C to create a physics simulation of charged particles in an electromagnetic field. I need to return arrays of vertices after performing motion calculations. Previously, I merely changed the vertices using a for loop in a function, but at this juncture it would be considerably simpler to just have an array return value for the function as a whole. Does anybody have any experience with this? I need to be able to pass an array as one of many arguments to the function, perform mathematical alterations on its members based on forces, and then return that array's new value, then continue onto the next in a series of 24 arrays holding vertex data. Any help is much appreciated, thanks


Share this post


Link to post
Share on other sites
Quote:
Original post by noviceprogmr84
and then return that array's new value


Quote:
Original post by Michael Mackend
Return a pointer. :)


I think Michael might be on to something [wink]

If you really did change the array's value (as opposed to creating a completely new array inside the function), then returning a pointer to that array would be enough.

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
Quote:
Original post by noviceprogmr84
and then return that array's new value


Quote:
Original post by Michael Mackend
Return a pointer. :)


I think Michael might be on to something [wink]

If you really did change the array's value (as opposed to creating a completely new array inside the function), then returning a pointer to that array would be enough.


If you modified an array, you already know which array is that, so you don't really need to return anything, since you simply modify it and may then keep using that same one. It's like using the array as an argument. Returning a pointer would be the case of heap allocation inside the function or something that may point to different things.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement