Copy an array to a vector, it doesn't work in all cases.

Hi everyone, I have the following functions and they're applied in the following manner:

1
2
3
4
5
int GetUsers(XnUsersID users[])
{
  getAllUsers(users,....)

}


Where.. geAlltUsers(XnUsersID * users, ..)

Then I have a function which belong to a class A that uses the previous functions:

1
2
3
4
5
6
7
void getUsuariosVisibles()
{ 
       vector<XnUsersID> usersID(2);
       GetUsers(&usersID[0]);
       //
       // I iterate the vector usersID using its information without any problem
}


However if the vector belongs to the public part of the class A, the same class that owns the void getUsuariosVisibles() method:


1
2
3
4
5
6
7
void getUsuariosVisibles()
{ 
 
       GetUsers(&usersID[0]); // vector initialized in the constructor class
       //
       // PROBLEMS!!! I get an empty vector!.
}


I tryed to get the function returns the vector, but with the same result, it is empty. It seems there's a problem where I try to copy the array to the vector, when the vector is declared outside the function.


Compiler: gcc 4.6.3
OS: Ubuntu 12.04 64 bits.

Thanks
Last edited on
> vector initialized in the constructor class
> PROBLEMS!!! I get an empty vector!.
¿did the function modify the vector size?
I suspect out of bounds access.

http://www.eelis.net/iso-c++/testcase.xhtml
Thanks for the link, but it's not the case. I tested it with the same values, when the vector belongs to the class it remains empty meanwhile when is declared in the function it returns a vector with values.
The problem is before accessing its values, just checking the size, so it's prior to bound problems.


Last edited on
A vector is not the same as an array. It manages its own data, remembers the size, and has a bunch of associated methods to handle the data.

If you want to convert a vector to an array, I suggest vector<>::data():
http://www.cplusplus.com/reference/vector/vector/data/
Then do it like this:
1
2
3
4
void getUsuariosVisibles()
{ 
       GetUsers( usersID.data() );
}


Note that this is only applicable for C++11. I've done lots of work with sound-files where I wanted to use vectors to store each frame of data, but used arrays so that I could just pass the address of the first element into a C API.

> Thanks for the link, but it's not the case.
The link is about the information that you should provide when asking for help.

> when the vector belongs to the class it remains empty
¿remains? ¿it was always empty?

> The problem is before accessing its values, just checking the size, so it's prior to bound problems.
¿what does GetUsers() do?
¿what's the point of the function if it does not access the elements of the passed array?
It's quite rare that it just need a pointer and not its size.
Its just data dude.
If its a bunch of char then it a bunch of chars. If there are more or fewer, then that's equivalent to a different type.
Such as sizeof( XnUsersID ) should always yield the same size for that type.
Vectors are linear arrays meaning its a block of memory and for that your types have to be the same size for each allotted increment just like an array .
If your XnUsersID instance is changing in size, that memory has to be managed else you have a memory leak.
If you are taking on the responsibility of memory management then use your vector to house a host of XnUsersID pointers.
All containers are dependent upon proper management and declaration of usage according to how the STL expects them. A vector deletes according to the class's destructor.... etc. etc.. etc.. etc..
So much for my Yul Brenner imitation.
Topic archived. No new replies allowed.