If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Modifying an existing array with a function

I have an array of char representing pixels in a bitmap, which I want to modify. I don't think I can just iterate over the array and pass chars into a function individually, because the function needs to take into account the neighboring pixels, too.

I thought of two ways to do this. The first would be to pass the array to the function as an argument, then have the function change it and return it. The trouble is I'm not exactly sure what happens internally when you pass an array to a function and return it. Is it the same array, modified? Or is it a copy of the array, so now you're using twice as much memory?

Alternatively I guess I could have a function with a void return type and pass a pointer to the array. I'm somewhat new to this, but the way I understand it is that a pointer is like the address of a house, while the array is the actual house. So if I give the function the address, it can go to that address and rearrange the furniture inside the house. Then, after the function returns, I can go to that address myself and see all the rearranged furniture, even though the function has already returned.

Is there a problem with the second way? It seems a bit neater, but maybe I'm understanding pointers wrong.

Re: Modifying an existing array with a function

Originally Posted by MrViggy

The stack is limited (I forget the specific amount), [...].

Under Windows (except on Itanium machines) it has a default size of 1 MB, but that can be changed (see http://msdn.microsoft.com/en-us/libr...=vs.100).aspx). But, as you already suggested, it's probably better to leave the stack size as-is and use a std::vector instead.

I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Re: Modifying an existing array with a function

Originally Posted by fiodis

I get a stack overflow error.

You could avoid this overflow (as has been suggested) by allocating the array on the heap instead. But if the array is to be present during the whole of the program's lifetime there's a third option, static memory. Simply move the array declaration outside of main (and any other function) and it will be in static memory. So apart from stack and heap there's also static memory to consider.

Note that if you switch from an array to a vector (as has been suggested) you must be careful not to end up doing what you were worried about, namely passing a copy of the whole thing. There are two major parameter passing modes to consider: by-reference and by-value. And there are two common ways to avoid passing a copy of a data structure to a function. The first is to pass the data structure by-reference (commonly used for vectors). The second is to pass a pointer to the data structure by-value (commonly used for arrays).

Re: Modifying an existing array with a function

"static" has different meaning depending on how you use it.

at global scope it means a variable (or function) is local to the module only (it is not being exported outside of the compilation unit), so it cannot be referred to from another source file (not even with an extern declaration).

At function scope it means that the variable is visible inside the function only, and in addition the contents will persist across multiple calls to the function so you can do something like this:

Re: Modifying an existing array with a function

Not exactly. You can kinda think of it as the vector being passed as a pointer to the vector itself (not the start of the data), if I understand your question. You still access the vector as if it were an object (use '.' not '->'). Passing by reference doesn't make a copy of the data, allowing the function to change the data in the vector (or whatever data type your function takes). Because the compiler doesn't make a copy of the data, a lot of times you'll see vectors being passed as const references, which enforces the notion that the data in the vector should not be changed by the compiler, but you won't have a copy of the vector made.

Re: Modifying an existing array with a function

And I looked up passing vectors by reference. I see the syntax for it is vector<Type>& but what is a reference, exactly? Is it a pointer to the start of the vector in memory?

Sidebar: How do you get the quote icon and the name of the person who said it inside the box?

This is only my opinion and I know a lot of people, including the designers of the STL, disagree with me but I think references are vastly overused. In most cases, all they are doing is hiding a pointer so the programmer doesn't have to explicity use * and ->.

I think when I was learning C++ (and coming from a C background was quite comfortable with pointers) I had a bad experience that set me against references and I guess I've never really gotten over it; at the time, the STL was poorly supported and one of my predecessors on a certain project had implemented his own queue. I was debugging a block of code and found something like this:

I immediately thought I had found a memory leak. Clearly, pWidget was not deallocated anywhere. It turns out the Enque function took its argument by reference, internally took the address of it, and thereby took ownership of the object. I got bit by this a second time when, after enough time had transpired for me to forget about it, I ran across it again.

To my thinking, the intent is far more clear if ownership issues are communicated with pointers and pointers-to-const. In my opinion, std::swap(Type & a, Type & b); should instead be std::swap(Type * pa, Type * pb); I do like references, a lot, when necessary for emulating the semantics of primitive types, as in the code necessary for something like this to work:

* The Perfect Platform for Game Developers: Android
Developing rich, high performance Android games from the ground up is a daunting task. Intel has provided Android developers with a number of tools that can be leveraged by Android game developers.

* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.