Pass Vector of pointers to function expecting an array

Hi,

I have a vector of pointers to objects, I need to pass this into a third party function which is expecting an array of objects, the function prototype goes like this:

void func(int *data, int size);

Which updates everything in data.

Here's an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <stdio.h>
#include <vector>

void func(int *data, int size);

int main()
{
	std::vector <int*> things;
	int a[10];

	for(int i = 0; i < 10; i++)
	{
		int *d = new int;
		*d = i + 100;

		things.push_back(d);

		a[i] = i + 100;
	}

	func(things[0], 10);
	printf("\n");

	func(a, 10);
}

void func(int *data, int size)
{
        //in real world this function does something to modify the data
	for(int i = 0; i < size; i++)
	{
		printf("%d\n", data[i]);
	}
}


You'll see I've tried passing an array (a) to func which shows the correct output. The output is:


100
-33686019
98893109
-2013260835
18001016
17987184
1540103372
556
4
2

100
101
102
103
104
105
106
107
108
109


My question is why doesn't func produce the expected output when passing in my vector of pointers? It appears to handle the first item correctly but then it all goes wrong. I thought vectors stored data like an array (the next item immediately after the previous). How do I put this right?

-------------------------------------------------------------------------------
Bit of background:

I'm using a vector of pointers as I have a base class (gameObject) and a few derived classes (enemy, player, etc) so I can have a single list of all objects and call methods at certain times (e.g update function for each object etc) and I can add and remove new gameObjects when I like. In my example int* is just a replacement for gameObject*.

Any thoughts or feedback appreciated.

Thanks!
A std::vector is of another type than an array. But since it uses an array internally and it provides the data() function which returns that array, you can solve it like this:
 
func( things.data(), things.size() );
Last edited on
closed account (28poGNh0)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# include <stdio.h>
# include <vector>

void func(int *data, int size);

int main()
{
	std::vector <int> things;

	for(int i = 0; i < 10; i++)
	{
		int d = i + 100;

		things.push_back(d);
	}

	func(&things[0], 10);
	printf("\n");
}

void func(int *data, int size)
{
    // call elements of things this way
    for(int i = 0; i < size; i++)
	{
		printf("%d\n", data[i]);
	}

	// Or this way
	/*for(int i = 0; i < size; i++,data++)
	{
		printf("%d\n", *data);
	}*/
}
Hi,

Thanks for the replies.

Unfortunately things.data() doesn't work as data() returns a pointer so I end up trying to pass a pointer to a pointer (int**) when an int* is required. So this causes an error.

I want to keep the vector as a vector of pointers rather than a vector of ints.

Thanks,
so just dereference it like such:
*things.data();
That results in the same output as originally listed, I don't understand why?
Hmm, testing it out myself I had similar results.
Changed the printing line to
printf("%d\n", *data+i);
and it works just fine
If func is expecting an array of ints and you have a vector of pointers to int, there is no way to feed that to func and get the expected output. The same would be true if you had an array of pointers to int. There would be no way to pass that to func, either. If func expects an array of int, you must give it an array of int.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h>
#include <vector>

void func(int *data, int size);

int main()
{
	std::vector <int> things;
	int a[10];

	for(int i = 0; i < 10; i++)
	{
        a[i] = i+100 ;
        things.push_back(a[i]) ;
	}

	func(things.data(), things.size());
	printf("\n");

	func(a, 10);
}

void func(int *data, int size)
{
        //in real world this function does something to modify the data
	for(int i = 0; i < size; i++)
	{
		printf("%d\n", data[i]);
	}
}


Hmm, testing it out myself I had similar results.
Changed the printing line to
 
printf("%d\n", *data+i);

and it works just fine


changing things randomly until they seem to work is not a recipe for success. The only reason this output is what you expect is because the singular int at *data is 100. All you're doing is adding i to that number each iteration of the loop.

*data+i is the same as (*data)+i
Last edited on
You're absolutely right, my bad.
As mentioned, a different approach required then. Thanks for explanation about this.
Topic archived. No new replies allowed.