A vector of structure with vectors inside it .

I'm a little lost:
I have the next structure
1
2
3
4
5
Struct A {
int num;
vector<structB> vector_B;   or maybe  vector<structB> *vector_B;
vector<float> vector_float; or maybe  vector<float> *vector_float;
}


I create a vector to store A's
vector <A> * vector_A = new vector(A);

Ok . Inside A, vectors have to be created normal or as a pointer ?
So, what is your opinion of my code :
1.- create a new A and add it to the vector: vector_A->push_back (*(new A));
2.- Accessing item 0/num var (*vector_A)[0].num=33;
3.- Create a new B and add it to the vector_B (*(*vector_A)[0]).vector_B.pus_ack(*(new structB));
4.- adding floats to the vector_float : (*(*vector_A)[0]).vector_float.push_back(2.0f);
etc .
I dont know if I'm writing a madness code ....
Any help ?


Last edited on
vector <A> * vector_A = new vector(A); should be new vector<A>;

1. no. push_back will do allocation and copying for you. vector_A->push_back( A() );
2. yes.
3. no. as in 1, you don't need to allocate it yourself. (*vector_A)[0].vector_B.push_back( structB() );
4. no. as in 3, you have too much dereferencing. (*vector_A)[0] is not a pointer.

Instead of A() and structB() there would usually be names of your variables.
If you choose vector_B and vector_float to be pointers, the .push_back becomes ->push_back in 3 and 4.
My advice, don't use a pointer unless you know why you need a pointer and only when a pointer is required.

Generally speaking this is the way to go:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct A
{
    int num;
    vector<structB> vector_B;
    vector<float> vector_float;
}

void func(const structB& b)
{
    A a;
    a.vector_B.push_back(b);
    a.vector_float.push_back(2.7f);
    // ... etc.
}
Last edited on
I think I need pointers.
My classes spend a lot of memory so, If I want dynamic memory I have to create them using "new" , ins't it?

Another question:
I create the instance of classA on the heap, and a class B on the stack .
What is happen when I add the classB to the vector_of_b's that is stored by classA ?
Somebody copies the information from the stack to the heap? Is it not possible ,is it possible and normal ?
(This is the main reason because I create all on the stack...)
Thanks


tonnot wrote:
I think I need pointers.
My classes spend a lot of memory so, If I want dynamic memory I have to create them using "new" , ins't it?


You don't need pointers because the vector<> uses pointers internally to save you the trouble. So it doesn't matter how many items you put into a vector, it will not make your object any larger because they are allocated using new internally.

tonnot wrote:
Another question:
I create the instance of classA on the heap, and a class B on the stack .
What is happen when I add the classB to the vector_of_b's that is stored by classA ?
Somebody copies the information from the stack to the heap? Is it not possible ,is it possible and normal ?
(This is the main reason because I create all on the stack...)


As I said before when you have a vector<> it keeps all its objects on the heap. So even if you put your vector<> on the stack, everything it contains will be stored on the heap.

So when you add an object of classB to the vector<> it does get copied from the stack to the heap.
Ok, but in my case I have a main class that stores by itself a lot of data, including vectors inside it.
So this main class instance has to be created on the heap , isn't it ?
I believe by default you are given 1 MB of memory for your stack. Do you think you won't fit into that?
Topic archived. No new replies allowed.