0

I am using STXXL Library's stxxl::vector in my code as :

struct B
{
    typedef stxxl::VECTOR_GENERATOR<float>::result vector;
    vector content; 
};

And then creating many instances of the above declared Structure in a loop using the following code snippet :

 for(i=0;i<50;i++)
 {
     B* newVect= new B();
     // Passing the above '*newVect' to some other function
 }

But this snippet will not create 'newVect' beyond certain number(30: in this case)

However, I tried the same thing by just replacing "stxxl:Vector" with some other In Memory datatypes as :

struct B
{
    float a,b,c;
    int  f,g,h;
};

Above created Structure works fine even for "100000" new instances as:

for(i=0;i<100000;i++)
{
    B* newVect= new B();
    // Passing the above '*newVect' to some other function
}

with every system resource remaining same.

Please help me with this.

Can "stxxl:Iterators" help here or work as an alternative?

What kind of behavior do 'stxxl:vector' have in this case?

UPDATE

Tried removing the function call from each iteration and putting it altogether outside the loop but no help. Example Code:

#include <stxxl/vector>
#include <iostream>
using namespace std;

struct buff
{
    typedef stxxl::VECTOR_GENERATOR<float>::result vector;

    vector content; 
};

struct par
{
    buff* b[35];
};

void f(par *p)
{
    for(int h=0;h<35;h++)
    {
        std::cout<<endl<<"In func: "<<(*p).b[h];    
    }
}

int main()
{
    par parent;
    for(int h=0;h<35;h++)
    {
        buff* b=new buff();

        parent.b[h]=b;
        cout<<endl<<"IN main: "<<parent.b[h];
    }

    cout << endl << endl;
    f(&parent);

    return 0;
}
sfjac
  • 7,119
  • 5
  • 45
  • 69
Akif Khan
  • 53
  • 8
  • What's the error you get when trying to allocate more than 30 `B`s? Also, are you copying the pointer to the new `B` somewhere within the loop, or deleting it, or is it leaking memory? – Carlton Jun 03 '15 at 17:25
  • Program run stops when trying to allocate 30 B's throwing the following exception : "Unhandled exception at 0x6326326C (msvcr110.dll) in test2.exe: Fatal program exit requested." Within the loop, I am passing the "Pointer to the new B" to another method "foo()" as: foo(newVect); in each iteration of the Loop. – Akif Khan Jun 03 '15 at 17:35
  • Try removing that other function call, it might be causing the problem. After removing it, can you create more than the 30 you're currently having a problem with? – Steve Jun 03 '15 at 17:39
  • First try what @Steve said, then also enclose the contents of your loop in a `try...catch` block and see what the thrown exception is. `std::exception::what()` should give you a clue as to what exactly the problem is. – Carlton Jun 03 '15 at 17:49
  • @Steve, I tried removing the function call from the loop but it didn't help. Check my example code snippet in the UPDATE above. – Akif Khan Jun 03 '15 at 17:51
  • Yeah, but did the crash this time happen in `f()` or inside your loop in `main()`? – Steve Jun 03 '15 at 17:53
  • Crash happen in main() itself when it tries to create the 31st 'buff' in : for(int h=0;h<35;h++) { buff* b=new buff(); parent.b[h]=b; } – Akif Khan Jun 04 '15 at 04:27
  • @nos , Need your help... – Akif Khan Jun 04 '15 at 05:28
  • 1
    Exception is : "bad allocation". Is there any alternative for this problem? – Akif Khan Jun 04 '15 at 14:32

1 Answers1

1

Each stxxl::vector costs a specific amount of internal memory, since it is basically a paging system for blocks in external memory.

With the default settings, this is 8 (CachePages) * 4 (PageSize) * 2 MiB (BlockSize) = 64 MiB of RAM per stxxl::vector.

So, you are basically running out of RAM.

Timo Bingmann
  • 294
  • 1
  • 4