Size / Length of primary array

Hi there.

Actually a Java programmer I am dealing with C++ again. Haven't done so in 15 years, so please be a little easy on me when I start off with the probably most asked question about C++ possible:

That is the neverending story of dynamically figuring out the size of primary type arrays. (I know, I know...sorry)
I was well-behaved and searched the forum first. I see that things haven't changed and it's still not possible to dynamically figure out the size of an primary type array as can be seen here: http://www.cplusplus.com/forum/beginner/18760/
Unfortunately I am about to make use of an lib that needs this type of array. That is: TYPE[n] with n being unkown at running time. Concret:
1
2
3
double xDoubles[] = <list of n double values gathered somewhere else>
...
xValues.setcontent(0, numberOfValues, xDoubles);

I'd make use of std::vector<T>
http://www.cppreference.com/wiki/stl/vector/start
but I can't find a way (method) to convert from std::vector<T> back to double[]. Hm, no this shouldn't work anyway.

So how to solve this problem?
Greatly appreciate any hints (links, etc.)!

Cheers


http://cplusplus.com/forum/articles/20881/

Scroll to the very last post and tell me if that helps you. I'm not sure if I understand the question.
What exactly is your problem? You want to get a double[] from a std::vector<double> ?

As long as you are using a std::vector you can get the array address like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <vector>

void func(double d[], int len)
{
	for(int i = 0; i < len; ++i)
	{
		std::cout << d[i] << std::endl;
	}
}

int main()
{
	std::vector<double> v;
	v.push_back(0.2);
	v.push_back(1.4);
	v.push_back(7.9);

	func(&v[0], v.size());

	return 0;
}
0.2
1.4
7.9
Last edited on
Thank you kempofighter and Galik!

You guys have prompted me with the same solution, I guess. And actually the one I had in mind and feared to be the only one. That is to iterate through std::vector and build up a double[]. Okay then, I go for this one.

Many thanks!!

PS: Hoped that C++ would keep track of these things (length) so costs of figuring this out would be 1. Now it is n. But okay, I am dealing with array sizes around 50 < n < 200. So it'll be hardly costly.
Last edited on
I don't understand your fear because I don't understand the original question. The vector does keep track of its own length. You want to make a double from a vector? Why? If you are going to build a new object and copy then of course you have to copy each element n times. What we have said is that the address of the first element is a pointer to type T (double, int, Foo, or whatever T is). Just to be clear, in our examples you are passing a pointer to the first element of the array to the func. you are not copying the vector unless the func does a copy internally. If we are misunderstanding your requirement please clarify your question with a better example.
Last edited on
CPPNewbie wrote:
PS: Hoped that C++ would keep track of these things (length) so costs of figuring this out would be 1. Now it is n. But okay, I am dealing with array sizes around 50 < n < 200. So it'll be hardly costly.


The std::vector does keep track of its length. So the cost of figuring it out is 1, not n. ;o)
I was missing the fact that Galik's code isn't actually building up a double[]. Instead vector<double> is treated like double[]. This is great. Because limiting any effort to zero. Sorry about my then confusing last post. Didn't know vector<double> IS or at least can be treated like a double[].

Many thanks.

Reference:
1
2
3
4
5
void func(double d[], int len);
...
std::vector<double> v;
...
func(&v[0], v.size());
CPPNewbie wrote:
I was missing the fact that Galik's code isn't actually building up a double[]. Instead vector<double> is treated like double[]. This is great. Because limiting any effort to zero. Sorry about my then confusing last post. Didn't know vector<double> IS or at least can be treated like a double[].


The reason this is possible is because the C++ standard guarantees that the storage in a std::vector is contiguous (all in one block). This means that a std::vector must be implemented as an array internally. However, beware that taking the address of that array using &v[0] will not be valid if you modify your std::vector because it may re-allocate its internal array giving it a new address.

So this is a useful technique for interfacing with legacy code that still uses arrays but is otherwise best avoided.
Topic archived. No new replies allowed.