Init array in function

Jan 31, 2012 at 11:36am
1
2
3
4
5
6
7
8
template<int _size>
Matrix<_size> Matrix<_size>::operator + (const Matrix< _size> &rhs) const
{
	Matrix<_size> m;
	for (int i = 0; i < (_size*_size); ++i)
		m.elements[i] = elements[i] + rhs.elements[i];
	return m;
}


The function above inits a new object in a function and returns it. Doesnt this mean that as soon as the program has executed this method,the memory used to store Matrix m, can be garbage collected?

I thought it was bad practice to create pointers/objects in a method and return them in c++?
Jan 31, 2012 at 12:21pm
calypso wrote:
Doesnt this mean that as soon as the program has executed this method,the memory used to store Matrix m, can be garbage collected? (sic)

If the default constructor does its job and initialises all members, then no. Or have I missed the point?

calypso wrote:
I thought it was bad practice to create pointers/objects in a method and return them in c++? (sic)

It is, but here, you're returning a copy, not m itself. This method doesn't return a reference, nor does it return a pointer.

Wazzak
Jan 31, 2012 at 12:40pm
OK I think I may have been a bit confused. So are you saying if I was to use the + operator to add matrix's e.g

Matrix mc = ma + mb;

The + operator method would be called to add them, and then the copy constrcutor would be called to return it to mc?


//is this fine?
int add(int a, int b)
{
int c = a +b;
return c;
}

//is this poor coding because the copy constructor is not called to return the value?
int* add(int a, int b)
{
return &(a +b);
}

//this is what I normally do
int add(int a, int b, int*c)
{
*c = a + b;
}
Jan 31, 2012 at 12:51pm
calypso wrote:
1
2
3
4
5
6
//is this fine?
int add(int a, int b)
{
int c = a +b;
return c;
}
(sic)

Yes, that's fine.

calypso wrote:
1
2
3
4
5
//is this poor coding because the copy constructor is not called to return the value?
int* add(int a, int b)
{
return &(a +b);
}
(sic)

Don't do that. The expression (a + b) creates a temporary int, who's address is returned. That's the same as returning a pointer to a temporary.

1
2
3
4
5
//this is what I normally do
int add(int a, int b, int*c)
{
*c = a + b;
}
(sic)

Make sure you check for null. Also, you forgot to return.

Wazzak
Last edited on Jan 31, 2012 at 12:51pm
Jan 31, 2012 at 1:19pm
You can use the c++ equivalent that it's prettier (partially that's way it was introduced anyway):
1
2
3
4
int add(int a, int b, int &c)
{
c = a + b;
}


that works as pointers do but in a more intuitive way
Jan 31, 2012 at 1:20pm
Thanks, finally can you just remind me the reasons why I should not return pointer/function from a method again?
Jan 31, 2012 at 3:35pm
You can return a pointer or reference from a function, just make sure it's not a reference/pointer to a local object. Since local objects go out of scope when the function call ends, a pointer/reference to that object would be left dangling; not a good thing, especially for references.

Wazzak
Jan 31, 2012 at 4:41pm
Thanks again
Topic archived. No new replies allowed.