3D vector memory deallocation

Discussion in 'C++' started by madhu, Nov 11, 2006.

  1. madhu

    madhu Guest

    vector<vector<vector<long> > > Vector3D; // 3dvector.

    for (long k = 0; j < Depth; j++ )
    {
    Vector3D.push_back ( vector<vector<A_Type> >() );
    for (long j = 0; j < Height; j++ )
    {
    Vector3D[k].push_back ( vector<A_Type>() );
    for ( long i = 0; i < Width; i++ )
    {
    Vector3D[k][j].push_back ( i );
    }
    }
    }

    Vector3D.clear();
    //memory deallocation

    The program is crashed at run time.

    ?I have few questions.

    What may be the reason behind crash?
    Is Vector3D.clear() is sufficient to deallocate memory in all
    dimention?
    Is there any better way to deallocate the memory?
    Do we need to deallocate memory in a vector?
    madhu, Nov 11, 2006
    #1
    1. Advertising

  2. madhu wrote:
    > vector<vector<vector<long> > > Vector3D; // 3dvector.
    >
    > for (long k = 0; j < Depth; j++ )
    > {
    > Vector3D.push_back ( vector<vector<A_Type> >() );
    > for (long j = 0; j < Height; j++ )
    > {
    > Vector3D[k].push_back ( vector<A_Type>() );
    > for ( long i = 0; i < Width; i++ )
    > {
    > Vector3D[k][j].push_back ( i );
    > }
    > }
    > }
    >
    > Vector3D.clear();
    > //memory deallocation
    >
    > The program is crashed at run time.
    >
    > ?I have few questions.
    >
    > What may be the reason behind crash?
    > Is Vector3D.clear() is sufficient to deallocate memory in all
    > dimention?
    > Is there any better way to deallocate the memory?
    > Do we need to deallocate memory in a vector?
    >


    Please post a complete compilable program that exhibits your problem.
    You're almost there.
    Gianni Mariani, Nov 11, 2006
    #2
    1. Advertising

  3. "madhu" <> wrote in message
    news:...
    : vector<vector<vector<long> > > Vector3D; // 3dvector.
    :
    : for (long k = 0; j < Depth; j++ )
    Hmm, shouldn't j be replaced with k above ?
    This error may well be causing an infinite loop.

    : {
    : Vector3D.push_back ( vector<vector<A_Type> >() );
    : for (long j = 0; j < Height; j++ )
    : {
    : Vector3D[k].push_back ( vector<A_Type>() );
    : for ( long i = 0; i < Width; i++ )
    : {
    : Vector3D[k][j].push_back ( i );
    : }
    : }
    : }
    :
    : Vector3D.clear();
    : //memory deallocation
    :
    : The program is crashed at run time.
    Other than the problem reported above, I think this
    code snippet would run ok.

    Yet the code seems unnecessarily complex and slow.
    The following would initialize the same contents as
    above:

    vector<A_Type> leaf( Width );
    for( long i = 0 ; i < Width ; ++i ) leaf=i;
    vector<vector<vector<long> > >
    Vector3D( Depth, vector<vector<long> >( Height, leaf );


    Repeated calls to push_back are slow in comparison to
    a single allocation/initialization of a vector.


    : ?I have few questions.
    :
    : What may be the reason behind crash?
    I don't see a reason for a "crash", except for the infinite
    recursion which would cause memory exhaustion.

    : Is Vector3D.clear() is sufficient to deallocate memory in all
    : dimention?
    Yes (or maybe not quite). The single memory segment allocated
    by the instance itself will remain allocated (i.e.
    Depth*sizeof(vector), probably Depth*12 bytes on a 32-bit platform).
    It will be released when the variable gets out of scope.

    : Is there any better way to deallocate the memory?
    No.

    : Do we need to deallocate memory in a vector?
    You don't need to do it explicitly.
    When a vector variable goes out of scope, all the memory
    it has allocated will automatically be released.


    Ivan
    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
    Ivan Vecerina, Nov 11, 2006
    #3
  4. madhu

    madhu Guest

    Ivan Vecerina wrote:
    > "madhu" <> wrote in message
    > news:...
    > : vector<vector<vector<long> > > Vector3D; // 3dvector.
    > :
    > : for (long k = 0; j < Depth; j++ )
    > Hmm, shouldn't j be replaced with k above ?
    > This error may well be causing an infinite loop.
    >
    > : {
    > : Vector3D.push_back ( vector<vector<A_Type> >() );
    > : for (long j = 0; j < Height; j++ )
    > : {
    > : Vector3D[k].push_back ( vector<A_Type>() );
    > : for ( long i = 0; i < Width; i++ )
    > : {
    > : Vector3D[k][j].push_back ( i );
    > : }
    > : }
    > : }
    > :
    > : Vector3D.clear();
    > : //memory deallocation
    > :
    > : The program is crashed at run time.
    > Other than the problem reported above, I think this
    > code snippet would run ok.
    >
    > Yet the code seems unnecessarily complex and slow.
    > The following would initialize the same contents as
    > above:
    >
    > vector<A_Type> leaf( Width );
    > for( long i = 0 ; i < Width ; ++i ) leaf=i;
    > vector<vector<vector<long> > >
    > Vector3D( Depth, vector<vector<long> >( Height, leaf );
    >
    >
    > Repeated calls to push_back are slow in comparison to
    > a single allocation/initialization of a vector.
    >
    >
    > : ?I have few questions.
    > :
    > : What may be the reason behind crash?
    > I don't see a reason for a "crash", except for the infinite
    > recursion which would cause memory exhaustion.
    >
    > : Is Vector3D.clear() is sufficient to deallocate memory in all
    > : dimention?
    > Yes (or maybe not quite). The single memory segment allocated
    > by the instance itself will remain allocated (i.e.
    > Depth*sizeof(vector), probably Depth*12 bytes on a 32-bit platform).
    > It will be released when the variable gets out of scope.
    >
    > : Is there any better way to deallocate the memory?
    > No.
    >
    > : Do we need to deallocate memory in a vector?
    > You don't need to do it explicitly.
    > When a vector variable goes out of scope, all the memory
    > it has allocated will automatically be released.
    >
    >
    > Ivan
    > --
    > http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form




    But if we want to deallocate the memory explicitly, what should we do
    for it so that the single memory segment allocated.

    Thanks for all.
    madhu, Nov 11, 2006
    #4
  5. "madhu" <> wrote in message
    news:...
    : > : vector<vector<vector<long> > > Vector3D; // 3dvector.
    ....
    : > : Is Vector3D.clear() is sufficient to deallocate memory in all
    : > : dimention?
    : > Yes (or maybe not quite). The single memory segment allocated
    : > by the instance itself will remain allocated (i.e.
    : > Depth*sizeof(vector), probably Depth*12 bytes on a 32-bit platform).
    : > It will be released when the variable gets out of scope.
    ....
    : > : Do we need to deallocate memory in a vector?
    : > You don't need to do it explicitly.
    : > When a vector variable goes out of scope, all the memory
    : > it has allocated will automatically be released.
    :
    : But if we want to deallocate the memory explicitly, what should
    : we do for it so that the single memory segment allocated.

    Not sure I understand your question correctly.
    But if you want to deallocate the memory block allocated
    by the "root" vector of the program you posted, the
    following trick will typically work (although it is
    not formally guaranteed to do so):

    vector<vector<vector<long> > >().swap( Vector3D );

    This swaps the memory blocks allocated by a new and empty
    temporary instance with that of Vector3D. The temporary
    is the destroyed and releases all the memory previously
    owned by Vector3D.

    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
    Ivan Vecerina, Nov 11, 2006
    #5
  6. madhu

    BobR Guest

    madhu wrote in message
    <>...
    >vector<vector<vector<long> > > Vector3D; // 3dvector.
    >for (long k = 0; j < Depth; j++ ){
    > Vector3D.push_back ( vector<vector<A_Type> >() );
    > for (long j = 0; j < Height; j++ ){
    > Vector3D[k].push_back ( vector<A_Type>() );
    > for ( long i = 0; i < Width; i++ ){
    > Vector3D[k][j].push_back ( i );
    > }
    > }
    >}
    >Vector3D.clear(); //memory deallocation
    >
    >The program is crashed at run time.
    >?I have few questions.


    Since they were answered, I'll give you a tip.

    // tip 1
    #include <iostream>
    #include <ostream>
    #include <vector>
    #include <stdexcept>

    {
    vector<vector<vector<long> > > Vector3D; // 3dvector.

    try{
    for (long k = 0; k < Depth; ++k ){
    Vector3D.push_back ( vector<vector<A_Type> >() );
    for (long j = 0; j < Height; ++j ){
    Vector3D.at( k ).push_back ( vector<A_Type>() );
    for ( long i = 0; i < Width; ++i ){
    Vector3D.at( k ).at( j ).push_back ( i );
    } // for(i)
    } // for(j)
    } // for(k)
    } // try
    catch( std::eek:ut_of_range &Oor ){
    std::cout<<"caught "<<Oor.what()<<std::endl;
    }
    }
    // ------------
    Now if an index is out of range, you'll see it.
    // First for(k) loop index error ( j != k ) fixed.


    // tip 2
    {
    typedef std::vector<std::vector<std::vector<int> > > vec3d;
    // instance: 3x3x3 all init'ed to 7.
    vec3d vec3D(3, std::vector<std::vector<int> >(3, std::vector<int>(3,
    int(7))));

    for(size_t x(0); x < vec3D.size(); ++x){
    for(size_t y(0); y < vec3D.at(x).size(); ++y){
    for(size_t z(0); z < vec3D.at(x).at(y).size(); ++z){
    std::cout<<" vec3D.at("<<x<<").at("<<y<<").at("<<z<<")= "
    <<vec3D.at(x).at(y).at(z)<<" // before"<<std::endl;
    vec3D.at(x).at(y).at(z) = z;
    std::cout<<" vec3D.at("<<x<<").at("<<y<<").at("<<z<<")= "
    <<vec3D.at(x).at(y).at(z)<<" // after"<<std::endl;
    } // for(z)
    } // for(y)
    std::cout<<std::endl;
    } // for(x)
    std::cout<<std::endl;
    }
    // ------------

    --
    Bob R
    POVrookie
    BobR, Nov 11, 2006
    #6
  7. madhu

    madhu Guest

    Well Ivan Thanks 4 ur help. Yourr suggetion was helpful to us.

    Madhukar Arvind

    Ivan Vecerina wrote:
    > "madhu" <> wrote in message
    > news:...
    > : vector<vector<vector<long> > > Vector3D; // 3dvector.
    > :
    > : for (long k = 0; j < Depth; j++ )
    > Hmm, shouldn't j be replaced with k above ?
    > This error may well be causing an infinite loop.
    >
    > : {
    > : Vector3D.push_back ( vector<vector<A_Type> >() );
    > : for (long j = 0; j < Height; j++ )
    > : {
    > : Vector3D[k].push_back ( vector<A_Type>() );
    > : for ( long i = 0; i < Width; i++ )
    > : {
    > : Vector3D[k][j].push_back ( i );
    > : }
    > : }
    > : }
    > :
    > : Vector3D.clear();
    > : //memory deallocation
    > :
    > : The program is crashed at run time.
    > Other than the problem reported above, I think this
    > code snippet would run ok.
    >
    > Yet the code seems unnecessarily complex and slow.
    > The following would initialize the same contents as
    > above:
    >
    > vector<A_Type> leaf( Width );
    > for( long i = 0 ; i < Width ; ++i ) leaf=i;
    > vector<vector<vector<long> > >
    > Vector3D( Depth, vector<vector<long> >( Height, leaf );
    >
    >
    > Repeated calls to push_back are slow in comparison to
    > a single allocation/initialization of a vector.
    >
    >
    > : ?I have few questions.
    > :
    > : What may be the reason behind crash?
    > I don't see a reason for a "crash", except for the infinite
    > recursion which would cause memory exhaustion.
    >
    > : Is Vector3D.clear() is sufficient to deallocate memory in all
    > : dimention?
    > Yes (or maybe not quite). The single memory segment allocated
    > by the instance itself will remain allocated (i.e.
    > Depth*sizeof(vector), probably Depth*12 bytes on a 32-bit platform).
    > It will be released when the variable gets out of scope.
    >
    > : Is there any better way to deallocate the memory?
    > No.
    >
    > : Do we need to deallocate memory in a vector?
    > You don't need to do it explicitly.
    > When a vector variable goes out of scope, all the memory
    > it has allocated will automatically be released.
    >
    >
    > Ivan
    > --
    > http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
    madhu, Nov 13, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. qazmlp
    Replies:
    1
    Views:
    741
    John Harrison
    Mar 7, 2004
  2. Replies:
    2
    Views:
    720
    Paavo Helde
    Dec 18, 2004
  3. Andy Chang

    Vector pointer and deallocation

    Andy Chang, Jan 4, 2005, in forum: C++
    Replies:
    7
    Views:
    8,199
    Ron Natalie
    Jan 5, 2005
  4. Replies:
    8
    Views:
    1,913
    Csaba
    Feb 18, 2006
  5. Cristiano

    vector and struct deallocation

    Cristiano, Sep 1, 2012, in forum: C++
    Replies:
    28
    Views:
    780
    Jorgen Grahn
    Sep 4, 2012
Loading...

Share This Page