double& array[] valid?

Dear all,
I am now trying to write a void function which would give an array of 360 elements as output for other parts of the program. I wrote something like this.

void plot_cycle_data(vector<int> composite, double& sum[], double& count[])
{
...
}

However, the compiler would not compile the program.
What is wrong? Ideally, I would imagine after doing the ... things the function will output an array of 360 sums and an array of 360 counts for the next part of my program...

Many thanks.

Regards,
Bosco
closed account (zb0S216C)
The C++ standard explicitly says there shall be no arrays of references, only references to arrays. Why? A reference must be initialised with a valid object. Since there's no guarantee that all references will be initialised, it would break the rules of the standard. Therefore, such a declaration is deemed illegal.

If you meant to declare a reference to an array, then the syntax is a little different:

1
2
int Array[] = {1, 2, 3 };
int(&RefToAnArray)[3](Array);

Note the parentheses enclosing the ampersand. This allows the compiler to distinguish between an array of references from a reference to an array.

Wazzak
Last edited on
If you're trying to reference an array, it should look like this:
1
2
void plot_cycle_data(vector<int> composite, double (&sum)[360], double (&count)[360])
{}


But this is probably not what you want. In which case just leave it at:
1
2
void plot_cycle_data(vector<int> composite, double sum[], double count[])
{}

Just

void plot_cycle_data(vector<int> composite, double sum[], double count[])

without the &s will allow you to populate the sum and count arrays.

But as you're using a vector for the first parameter, why not use arrays for the other two?

Also, if the first param is just for input, I would swap it to a const ref.

1
2
void plot_cycle_data(const vector<int>& composite,
vector<double>& sum, vector<double>& count)


with the vectors, you do need the &s
Last edited on
Thanks all I guess I will just take Andy's approach. I will just use vector instead of array, initialize the vectors by pushing back 360 zeros into each of the vectors, then use the two vectors as arrays.

Thank you all.

P.S. Vectors seems so much more convenient than arrays. Why would people still use arrays then actually...?
closed account (zb0S216C)
Differences:

- Efficiency. std::vectors are situated on the heap/free-store, whereas arrays are situated on the stack
- Efficiency, again. std::vector allocates memory for many objects of type T (usually more that 100,000)

Wazzak
You don't have to push_back 360 zeros; use the contructor with the size argument : it's the second overload in list on.
http://www.cplusplus.com/reference/stl/vector/vector/

Note it will zero all elems if you just give the size. You can also give a different fill value if you need to, as the second param.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
...

void plot_cycle_data(const vector<int>& composite,
	vector<double>& sum, vector<double>& count);

int main () {
	const size_t my_size = 360;

	vector<int> composite;
	vector<double> sum(my_size );
	vector<double> count(my_size );

	// fill composite, etc

	plot_cycle_data(composite, sum, count);

	return 0;
}
Last edited on
Hello guys,
I know this sound a bit stupid, can I define a class type VALUE, with this_value of type VALUE, and that this class type VALUE basically only consists of constructors and that array[] which i want to output from the function, and then I just do...

void plot_cycle_data(vector<int> composite, VALUE& the_value)

like this?

i know this must be really stupid, but i don't quite understand what you guys mean by call by value, call by reference and this thing below:

int(&RefToAnArray)[3](Array);
can I define a class type VALUE, with this_value of type VALUE, and that this class type VALUE basically only consists of constructors and that array[] which i want to output from the function, and then I just do...


Has anyone really been far even as decided to use even go want to do look more like?

Yes, I guess you can do that, but why?

i don't quite understand what you guys mean by call by value, call by reference and this thing below:

int(&RefToAnArray)[3](Array);


That "thing below" is just a reference to an array of 3 int's.
A reference can be thought of as a different name for a variable and not a variable on its own.

For how passing by value or reference works in functions, see here:
http://www.cplusplus.com/doc/tutorial/functions2/
Framework wrote:

- Efficiency. std::vectors are situated on the heap/free-store, whereas arrays are situated on the stack

Vectors are situated where the programmer wants them to be situated (although yes, in free store when using std::allocator)

- Efficiency, again. std::vector allocates memory for many objects of type T (usually more that 100,000)

If your compiler allocates a 400KB block for a vector<int>(1), file a bug report.
If your compiler allocates a 400KB block for a vector<int>(1), file a bug report.


I don't know much about vectors, but the heap seems to work differently than the stack.

On my school's Win7
1
2
3
4
5
6
int *array;
array = new int[100000];  // Memory might be allocated, but it isn't used

for (int i = 0; i < 100000; i++)
  array[i] = i;  // Now the program uses 400k


Like I said, vectors might be different, and I might be wrong, but this is the behavor we noticed.
Last edited on
Topic archived. No new replies allowed.