passing a vector as an argument

Nov 21, 2011 at 12:31am
Hi, all -

I'm experiencing an odd error. I'm attempting to pass a vector (by reference) to a routine. The vector contains 8 elements, but the called routine receives an empty vector (as evinced by the _M_start and _M_finish addresses being the same). Since I'm passing by reference, it's not invoking the copy constructor (right?), so that can't be the problem.

Here's the calling class and the call itself:

1
2
3
4
5
6
7
8
9
10
11
12
class DemodNyq {
private:
	vector<DemodNyqCell>	cells;
	vector<int32_t>			inpBuffer;
	vector<int32_t>			coeffBuffer;
.
.
.
			cells.at(i).cycle(inpBuffer,
							  coeffBuffer,
							  clockEnable,
							  resetFlag);


and here's the function declaration:

1
2
3
4
5
void 	DemodNyqCell::cycle(
		vector<int32_t>&	inp,
		vector<int32_t>&	coeffs,
		bool clockEnable,
		bool resetFlag)


Any idea what I might be getting wrong here? Thanks.
Nov 21, 2011 at 12:36am
How did you determine that the element count differs?
Nov 21, 2011 at 12:42am
When I step into the routine, the debugger first puts me into some system code that checks the range on the vector. Using the debugger, I looked at the start and end addresses, and they're the same (indicative of an empty vector). From the calling routine, start and end differ by 0x20, which is correct for a vector of 8 elements.

Also, the program immediately throws a range error when I try to execute the called routine.
Nov 21, 2011 at 1:59am
Another data point: I discovered that the addresses for the vector in the call not only point to a null vector, but they also point to the starting address of the data in the calling object, ie, the address of cells.

In other words, in the DemodNyq object, the data is stored: cells, inpBuffer, coeffBuffer, which makes sense, since that's how they're ordered in the class definition.

BUT:

The starting address of the first argument received by DemodNyqCell::cycle is that off cells, NOT inpBuffer.

Any ideas? This one has me a bit baffled.
Nov 21, 2011 at 2:12am
Could be anything. Debugger malfunction, a misinterpretation, undefined behavior... hard to say without seeing all the relevant code.
I suggest adding various asserts where it makes sense, such as assert(!inpBuffer.empty()); both before the call and inside the function and possibly printing vector sizes and line numbers at various places.
Nov 21, 2011 at 2:20am
I've never used asserts, but I just looked at the reference page on them. I can't put any inside the function, because the function never gets called: the system intercepts it when it range-checks the vector.

I'll put some stuff ahead of the function call and report back.

EDIT:

Nothing new revealed by the asserts or telltales. The program also bombs when running in non-debug mode, so I don't think I can blame the debugger.

Am I formatting my call correctly? I don't have a lot of experience passing vectors as parameters. Should I be passing something like inpBuffer::begin()?
Last edited on Nov 21, 2011 at 3:07am
Nov 21, 2011 at 5:09am
It sounds a lot like you are looking at the range check in the 'at(int)' function of std::vector, not anything to do with cycle(). My guess is the error is being generated because there is no DemodNyqCell object at cells[i], although we can't see how cells is being populated, nor what 'i' is.
Nov 21, 2011 at 5:36am
Rollie -

You nailed it. I stupidly forgot to populate the cells vector in my DemodNyq constructor.

Thanks a ton...may I ask how you were able to determine this with the limited information I provided?
Nov 21, 2011 at 8:32am
Well, you said that a range exception was thrown, which could only have come from at().
However, an assertion before the call should have failed if the vector were empty, revealing the problem.
Nov 21, 2011 at 2:43pm
You're right, Athar. When you suggested using the asserts, I thought you were referring to using it on the parameters of the call, not the object of the call itself. Oh, well, live and learn.

Thanks to both of you guys.
Topic archived. No new replies allowed.