Sign in to follow this  
Raeldor

Pass arrays of data as & or *?

Recommended Posts

Raeldor    254
Hi, I have a function that has a parameter that receives an array of data. Should I still declare this using the & format and then de-reference it as shown below, or is it preferred style to pass it as a pointer? Thanks
//
// build tree from input data which is an array the size of this quad
//
void QuadCompress::BuildTree(char &in_data)
{
	// loop through data to see if it is all the same
	int width=(int)m_topRight.x-(int)m_topLeft.x;
	bool isSame=true;
	m_data=(&in_data)[((int)m_topLeft.y*width)+(int)m_topLeft.x];
	for (int y=(int)m_topLeft.y; y < (int)m_bottomLeft.y && isSame; y++)
		for (int x=(int)m_topLeft.x; x < (int)m_topRight.x && isSame; x++)
			if ((&in_data)[(y*width)+x] != m_data)
				isSame=false;
}

Share this post


Link to post
Share on other sites
Raeldor    254
Quote:
Original post by xllx_relient_xllx
Maybe this can help.

Although this link does'nt pertain to C++. I think it's safely interchangeable with C++, given C++ is just a superset of C. If I'm wrong, Please feel free to correct me.

relient.


& as a parameter definition is not available in C as far as I know. I think it was added to C++ as a way to pass safe pointers. I know it is the preferred way for regular pass-by-reference parameters, but not sure about arrays.

Share this post


Link to post
Share on other sites
Quote:
Original post by Raeldor
& as a parameter definition is not available in C as far as I know. I think it was added to C++ as a way to pass safe pointers. I know it is the preferred way for regular pass-by-reference parameters, but not sure about arrays.


Yeah, You're right. I somehow thought you were talking about using [] or *.
My bad. I guess it's one of those brain-farts - oops =).. Time to get some good night sleep.

Share this post


Link to post
Share on other sites
Sneftel    1788
Pointers, in C, were a few things. Among them:

1. A way to refer to blocks of memory (i.e. arrays)
2. A way to do pass-by-reference (to avoid copying data, or to allow a parameter to be output to)

In C++, the second use has been replaced with references. Not the first.

Share this post


Link to post
Share on other sites
Raeldor    254
Quote:
Original post by Sneftel
Pointers, in C, were a few things. Among them:

1. A way to refer to blocks of memory (i.e. arrays)
2. A way to do pass-by-reference (to avoid copying data, or to allow a parameter to be output to)

In C++, the second use has been replaced with references. Not the first.


So I should use a pointer in this particular example instead of the pass-by-reference operator?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Quote:
Original post by Raeldor
So I should use a pointer in this particular example instead of the pass-by-reference operator?
.... Have you read any of Sneftel's replies?

Share this post


Link to post
Share on other sites
Unwise owl    158
Use a pointer, because a reference is always supposed to point to a valid object of its reference type. While you can argue that raw char data is always valid, I guess the object from conversion is not. Whenever you will need to do conversions it makes more sense to use pointers. Even if I convert a reference of type &CCar to a &CHouse but do not change the actual value, it doesn't implicitly mean the reference is now pointing to a valid CHouse object. Pointers that have a more "loose" definition should be used whenever there is no strict cohesion.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Perhaps as a suggestion even as overbloated as this may seem, you might consider encapsulating structures like this in classes and passing them as references, so rather than passing char** or char* to a function, use a container of some sort and pass the reference to the container. Avoid where
possible the use of pointers in cases where you expect an object to be passed
so that at least for the most part you don't have to worry about invalid
referencing to objects.

Share this post


Link to post
Share on other sites
Raeldor    254
Thanks for your help all. As usual I guess just because you CAN do something doesn't mean you should. Good to know the right way of doing it though... just starting to move over to c++ I don't want to get into too many bad habits.

Thank you!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this