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:
#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 = newint;
*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]);
}
}
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*.
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:
# 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);
}*/
}
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.
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.
#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.