return c arrays

Started by
12 comments, last by Brother Bob 16 years, 1 month ago
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.
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.
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.
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;}


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.
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.

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; }
}

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.
@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]
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.

This topic is closed to new replies.

Advertisement