• Advertisement
Sign in to follow this  

return c arrays

This topic is 3624 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
Don't use malloc in C++, it does not call constructors.
Don't use malloc if you aren't going to handle out-of-memory errors
Don't use malloc to handle local arrays of known length.

Obviously the extra function isn't required, it is to illustrate the problem.

edit: some of these points applied to a source snippet that was removed.

Share this post


Link to post
Share on other sites
Quote:
Original post by MartAlt
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.
How is malloc() more stable than new? And what exactly do you mean by 'stability' in this context?

Share this post


Link to post
Share on other sites
Quote:
Original post by openglaussie
I was more concerned on stressing the 'impossible' thing rather than memory leaks or efficiency , or elegance.

And at the same time, you introduced, possibly to a beginner, how to allocate memory and failed to mention you had to release it aswell. Effectively showing them how to allocate a buffer and then leak it. Such an important thing as introducing how to allocate a memory buffer should alywas introduce how to release it aswell.

Share this post


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

  • Advertisement