2

I am trying to allocate space for vector of vector but after allocation gdb showing null in pVectorTemp_ but showing size 2

#include <iostream> 
#include<vector>
using namespace std; 
int main( )
{ 
    int index1 = 2;
    int index2 = 2;
    vector<vector<float>*>* pVectorTemp_ =  NULL;
    pVectorTemp_  = new vector<vector<float>*>();
    pVectorTemp_->resize(index1);
    for(unsigned int i=0 ;i< index1;i++)
    {
        vector<float>* pvecTemp = new vector<float>();
        pvecTemp->resize(index2);
        (*pVectorTemp_)[index1] = (pvecTemp);
    }
    return 0;
}

gdb :

(gdb) pvector  pVectorTemp_
elem[0]: $2 = (std::vector<float, std::allocator<float> > *) 0x0
elem[1]: $3 = (std::vector<float, std::allocator<float> > *) 0x0
Vector size = 2
Vector capacity = 2

So Am i doing anything wrong?

EmptyData
  • 2,386
  • 2
  • 26
  • 42

4 Answers4

3

Inside the for loop body, you have:

(*pVectorTemp_)[index1] = (pvecTemp);

But note that the index of the for loop is i (index1 is the upper bound).

So, I think you have a typo or bug, and you may want to use i (not index1) as index inside [...].

Note also that you have a signed/unsigned mismatch, since in the loop you have unsigned int as index, and you compare that with index1, which is a signed integer.


But, anyway, your code is uselessly complicated.

You don't need to allocate all these vectors on the heap with new.
Just use automatic ("stack") allocation, e.g.:

int main()
{ 
    int index1 = 2;
    int index2 = 2;

    // Your original code is commented out:
    //
    // vector<vector<float>*>* pVectorTemp_ =  NULL;
    // pVectorTemp_  = new vector<vector<float>*>();
    vector<vector<float>> vectorTemp;

    // pVectorTemp_->resize(index1);
    // Just consider calling push_back.

    for (int i = 0; i < index1; i++)
    {
        // vector<float>* pvecTemp = new vector<float>();        
        // pvecTemp->resize(index2);
        // (*pVectorTemp_)[index1] = (pvecTemp);
        vectorTemp.push_back(vector<float>(index2));
    }

    // No need for return 0 in main().
    // return 0;
}

See how the code gets simplified!

(Code without comments follows.)

int main()
{ 
    int index1 = 2;
    int index2 = 2;

    vector<vector<float>> vectorTemp;        
    for (int i = 0; i < index1; i++)
    {
        vectorTemp.push_back(vector<float>(index2));
    }
}

As a further improvement, assuming that your C++ STL implementation provides that, you may want to use emplace_back() instead of push_back(), to build the nested vectors:

// Instead of vectorTemp.push_back(vector<float>(index2));
//
vectorTemp.emplace_back(index2);

In this case, a vector of size index2 is built directly into the vectorTemp ("outer" vector) container, without temporaries.

You may want to read also this thread on StackOverflow for further details:

push_back vs. emplace_back

Community
  • 1
  • 1
Mr.C64
  • 41,637
  • 14
  • 86
  • 162
  • @Jefffrey: Raw loops are just fine and very clear in several cases. No need to obfuscate the code with the latest feature. – Mr.C64 Jul 14 '14 at 14:21
  • `std::vector`'s constructor is not "*the latest feature*" by all means. – Shoe Jul 14 '14 at 15:35
  • I was referring to your critics to my use of raw loops, which to me are fine and clear in several cases. – Mr.C64 Jul 14 '14 at 21:41
1

These are the things I can find.

vector<vector<float>*>* pVectorTemp1 =  new vector<vector<float>*>(); // (1)(4)
pVectorTemp1->resize(index1);
for (int i=0 ; i < index1; i++) { // (2)
    vector<float>* pvecTemp2 = new vector<float>(); /(4)
    pvecTemp2->resize(index2);
    (*pVectorTemp1)[i] = (pvecTemp2); // (3)
}
  1. Variables that differ only by trailing underscore is asking for trouble.
  2. unsigned adds nothing, except the risk of a signed/unsigned mistake
  3. The index should be i not index1.
  4. It would be better to use local variables rather than heap allocation and pointers, but that would change the code too much.
david.pfx
  • 10,520
  • 3
  • 30
  • 63
1

Change

(*pVectorTemp_)[index1] = (pvecTemp);

with

(*pVectorTemp_)[i] = (pvecTemp);
Zorgiev
  • 794
  • 1
  • 7
  • 19
1

So Am i doing anything wrong?

Yes, you are doing something wrong:

  • using dynamically allocation when not needed
  • using unnecessary raw loops

Here's a simplified version:

int main( )
{ 
    int index1 = 2;
    int index2 = 2;
    std::vector<std::vector<float>> pVectorTemp_
        ( index1
        , std::vector<float>(index2) );
}

Live demo

Shoe
  • 74,840
  • 36
  • 166
  • 272