Returning an array

Guess this question's been asked a billion times, but since this forum has a strange search engine I can't find anything about it.
Anyways, to the question:

why doesn't this work?

1
2
3
4
5
6
7
8
9
float* Matrix4::getRow(int i)
{
	float* temp = new float[4];
	for(int j=0;j<4;j++)
	{
		temp[j] = m[i][j];// this is a float array
	}
	return temp;
}

for some reason the temp doesn't understand that it is an array because when I debug and check the value of temp before I return it, it only contains 1 value, the first that I added.
Last edited on
closed account (S6k9GNh0)
Yeah, I just noticed that. Maybe that should be changed to have a more specific search?

http://cplusplus.com/forum/articles/17108/

This is a recent article made for just this. It HAS been asked a billion times.
that code does work @ crimshaft.

The problem with it is it leaves delete[] up to the calling function, and is therefore undesireable:

1
2
3
float* foo = myMatrix.getRow(0);  // this will work

delete[] foo; // but then you have to delete it 


Note this is bad because it complicates the interface. The user has no way of knowing that getRow() allocates memory that needs to be deleted (unless of course the user wrote the class).

A better way would be to just return a pointer to the row:

1
2
3
4
float* Matrix4::getRow(int i)
{
  return &m[i][0];  // works
}


The catch to this is that changes made to the returned array will change the matrix (or is that desired?). If that's not desired you could change the funciton to return a const float*.

But I question whether or not a getRow function is necessary/wise to begin with.
Thanks for the replies.
Disch, I also thought about the fact that I'm allocating memory so I thought I'd change it to this:
1
2
3
4
5
6
7
8
9
float* Matrix4::getRow(int i)
{
    float temp[4];
    for(int j=0;j<4;j++)
    {
        temp[j] = m[i][j];//m.d float array
    }
    return temp;
}

I don't want to change the values after I've returned it so I won't use the & but thanks anyway.
I don't really know if I have a problem at all but if I do execute.

float* foo = myMatrix.getRow(0);

The debug say that foo only contain one value, while I want it to be an array.
I tried to store the m values in other variables first, eg. float t1,t2,t3,t4 and then assign them to temp, same result.
Last edited on
Disch, I also thought about the fact that I'm allocating memory so I thought I'd change it to this:


That won't work. Because 'temp' is stored locally on the stack, it is destroyed as soon as the function returns, which means you're returning a bad pointer.

I don't want to change the values after I've returned it so I won't use the & but thanks anyway.


You might've misunderstood me.

This will work and will make it so the values cannot be changed:
1
2
3
4
const float* Matrix4::getRow(int i) const // note the  'const's
{
  return &m[i][0];  // works, won't be changed
}


But then of course you'll need to use a const pointer:

 
const float* foo = myMatrix.getRow(0);


But i still question the necessity of such a function.
closed account (S6k9GNh0)
Also, another common solution for getting a pointer to stay allocated is to declare it static or create it on the heap dynamically using malloc or new.
Nobody really addressed the original question so here is the answer. Every debugger that I have used gives the same result. temp is declared as a pointer to a float. The fact that it happens to point to the first element of a dynamically allocated array is unknown to the debugger. There is nothing wrong with the code, other than the design issues that others have pointed out. The answer to your original question is that it is a limitation of that debugger. As far as the debugger is concerned temp is a pointer to a float not an array because that is how you declared that variable. This is one reason why it is better to use std containers for dynamic arrays. Modern debuggers shouldn't have any problem giving you the elements within the std container arrays. Also it is better for the caller to pass an empty container by reference and have the callee fill it with data. That avoids the copy which would occur if you returned a container by value and would also avoid the possibility of memory leaks.
That recent article 17108 isn't really related. The question isn't about making or using multidimensional arrays but about why the debugger shows only one value for the variable in question which was a pointer to a 1 dimensional array. In my opinion you never really need to return an array. It is always better to pass arrays as out parameters to the function and have the function fill the array. Use std containers when the caller doesn't know how big the array might be.
One more thing: use google. I agree that the search engine embedded within this site sucks. However if you use google it will find threads much more efficiently. I searched on the following phrase, "return array from function c++". Below the first cplusplus.com link there is a button that you can return more results from cpluplus.com and it will give you a bunch of specifically related threads. Perhaps there should be an article that explains this work around since the embedded forum search is so terrible.

I found the following threads easily using google and all are somewhat relevant. You'll find much more relevant articles searching directly on google and google will also find relevant threads of other forums.
http://www.cplusplus.com/forum/beginner/11344/
http://www.cplusplus.com/forum/beginner/12281/
http://www.cplusplus.com/forum/general/14196/
Last edited on
Thanks a lot kempofighter^^
So if I got It right visual studio debugger doesn't know that getRow() returns an array but the program will know that it is that. And there was no problem in the first place just that I did spam memmory. ( and of course the error in my program is somwhere else:-( )

That recent article 17108 isn't really related

allso thought so:P

But i still question the necessity of such a function.

why would this function be unnecessary? how could I solve someother way?

I'm making an openGL program where i use a Matrix class which i've made myself.
I store the current GL matrix in my Matrix before i load a new one so that i can reuse it later.
and now I want to send my matrix's different rows into different gl commands.

closed account (S6k9GNh0)
That wasn't your original question. Your question was and I quote:

why doesn't this work?


We tried to answer that. Please be clear next time.
Topic archived. No new replies allowed.