Jump to content
  • Advertisement
Sign in to follow this  
Mizipzor

return c arrays

This topic is 3890 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

What would be the best way to do this?
int* foo()
{
	int my[4];
	for ( int i = 0; i < 4; i++ ) { my = i; }
	return my;
}

int main ()
{
	int my[4] = foo(); // <-- compile error here
	for ( int i = 0; i < 4; i++ ) { std::cout << my << std::endl; }

	return 1;
}


How to avoid the compile error? Can I return an array? I have also considered a struct and a typedef.

Share this post


Link to post
Share on other sites
Advertisement
You cannot return arrays in C or C++. Solutions, by order of practicality:

  • Return a boost::array<int,4>
  • Return an std::vector<int>
  • Make your function a template function, take an iterator range as arguments.
  • Take a container by reference and fill it.

Share this post


Link to post
Share on other sites
You should really be passing the pointer that needs to be populated, to the function, something like this:



void Populate( int* pArray, unsigned int length )
{
if ( pArray == NULL )
{
return;
}

int count( 0 );

for ( unsigned int index( 0 ); index < length; ++index )
{
pArray[ index ] = count;

++count;
}
}

int main ()
{
Log::setFile("editor.log");
Log::purge();

int my[4];

Populate( &my[0], 4 );

for ( int i = 0; i < 4; i++ ) { std::cout << my << std::endl; }

return 1;
}



Or, if you are using C++, use std::vector or std::deque.

Share this post


Link to post
Share on other sites
Quote:
Original post by Dave
You should really be passing the pointer that needs to be populated, to the function, something like this:


In this case, since he seems to have fixed the size of the array, this would be just as plausible:

void foo(int (&arr)[4])
{
for (int i = 0; i < 4; ++i) arr = i;
}


Share this post


Link to post
Share on other sites
Yes, the array is of fixed size. Im currently doing this:


void foo( int* a )
{
for( int i = 0; i < 4, i++ ) a = i;
}

int main ()
{
int my[4];
foo( my );
return 1;
}


I dont want to use boost if I dont have to.
I could return a std::vector, but since the array is of fixed size, something like this seemed more appropriate.

My biggest concern right now is that I get 4 numbers in an array. The function Im writing calculates a bounding box, so those 4 numbers are 2 coordinates. But writing a[0], a[1] for the topleft corner and a[2], a[3] for the bottomright doesnt really have the readability I would like.

So Im thinking of defining two structs for the bounding box, point and box (box holds two points). But having two extra structs for just one function seems... odd. But maybe Im going to much into design here.

Share this post


Link to post
Share on other sites
Quote:
Original post by Mizipzor
So Im thinking of defining two structs for the bounding box, point and box (box holds two points). But having two extra structs for just one function seems... odd.


Actually, this would make more sense, since you could then group bounding-box-related functionality inside the box class. Remember, you're not defining them just for one function: you're defining them for every place in your code which works with bounding boxes.

Share this post


Link to post
Share on other sites
Sure, in c/c++ you can return a pointer here is how

int *foo ( int size )
{
int *ptr;
ptr= new int[ size ];
for ( int i=0; i<size; i++ )
ptr=i;
return ptr;
}

void main ( void )
{
int *ptr;

ptr= foo( 10 );

for ( int i = 0; i < 10; i++ ) { std::cout << ptr << std::endl; }
}

Share this post


Link to post
Share on other sites
Quote:
Original post by openglaussie
Sure, in c/c++ you can return a pointer here is how

int *foo ( int size )
{
int *ptr;
ptr= new int[ size ];
for ( int i=0; i<size; i++ )
ptr=i;
return ptr;
}

void main ( void )
{
int *ptr;

ptr= foo( 10 );

for ( int i = 0; i < 10; i++ ) { std::cout << ptr << std::endl; }
}


Often is the case that you don't need or want to allocate on the heap. Also, foo() now has two duties:

1. Allocate the array.
2. Assign the values to it.

This is quite limiting.

Share this post


Link to post
Share on other sites
@openglaussie

You code is a perfect example of why this is a bad idea, because you have demonstrated how easy it is to leak memory with this technique [smile]

Share this post


Link to post
Share on other sites
In C or C++, I find it's better to use "malloc" when dynamically allocating pointers. It's more stable. Also, before the end of your program, use the "free()" function to free your pointer. Otherwise, you will have a memory leak.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!