Vitual memory problem

Discussion in 'C++' started by Klaib, Feb 10, 2008.

  1. Klaib

    Klaib Guest

    Hi
    I am a student doing my project, my program needs a big arrays size.
    i used the following functions to create and delete the pointers, but
    i have problems based on virtual memory during the the run time "
    after 2 or 3 hour", Please help me to increase cBuilder virtual
    memory or solving this problem.
    Note: i can't reduce the arrays
    Thanks a lot for helping

    Errors type:
    * std:: bad-alloc
    * Assertion faild: !" Bad error code", file c:\\helena\\bcc\\util-
    common\\vMem.c, line 714

    This is the part of my code that i used:

    char*** create3Darray(int m, int n, int l)
    {
    char*** array;
    int i;

    array = new char**[m];
    for (i = 0; i < m; i++)
    array = create2Darray(n, l);
    return array;
    }

    //-----------------------------------------------------------
    //-------------------------------------------------------------

    char** create2Darray(int n, int l)
    {
    char** array;
    int j;

    array = new char*[n];
    for (j = 0; j < n; j++)
    array[j] = new char[l];

    return array;
    }

    //--------------------------------------------------------
    //--------------------------------------------------


    char* create1Darray(int n)
    {
    char* array;
    array = new char[n];

    return array;
    }
    //---------------------------------------------------------
    //----------------------------------------------------

    void delete2Darray(char** array, int m)
    {
    int j;

    for(j = 0; j < m; j++)delete [] array[j];
    delete [] array;
    array=NULL;
    }

    //---------------------------------------------------------
    //----------------------------------------------------
    void delete1Darray(char* array)
    {
    delete [] array;
    array=NULL;
    }
    //-------------------------------------------------

    //---------------------------------------------
    void delete3Darray(char*** array, int m, int n)
    {
    int i, j;
    for(i = 0; i < m; i++)
    {
    for(j = 0; j < n; j++)delete [] array[j];
    delete [] array;
    }

    delete [] array;
    }
     
    Klaib, Feb 10, 2008
    #1
    1. Advertising

  2. Klaib

    Guest

    On Feb 10, 4:20 pm, Klaib <> wrote:
    > Hi
    > I am a student doing my project, my program needs a big arrays size.
    > i used the following functions to create and delete the pointers, but
    > i have problems based on virtual memory during the the run time "
    > after 2 or 3 hour", Please help me to increase cBuilder virtual
    > memory or solving this problem.
    > Note: i can't reduce the arrays
    > Thanks a lot for helping
    >
    > Errors type:
    > * std:: bad-alloc
    > * Assertion faild: !" Bad error code", file c:\\helena\\bcc\\util-
    > common\\vMem.c, line 714
    >
    > This is the part of my code that i used:
    >
    > char*** create3Darray(int m, int n, int l)
    > {
    > char*** array;
    > int i;
    >
    > array = new char**[m];
    > for (i = 0; i < m; i++)
    > array = create2Darray(n, l);
    > return array;
    > }
    >
    > //-----------------------------------------------------------
    > //-------------------------------------------------------------
    >
    > char** create2Darray(int n, int l)
    > {
    > char** array;
    > int j;
    >
    > array = new char*[n];
    > for (j = 0; j < n; j++)
    > array[j] = new char[l];
    >
    > return array;
    > }
    >
    > //--------------------------------------------------------
    > //--------------------------------------------------
    >
    > char* create1Darray(int n)
    > {
    > char* array;
    > array = new char[n];
    >
    > return array;
    > }
    > //---------------------------------------------------------
    > //----------------------------------------------------
    >
    > void delete2Darray(char** array, int m)
    > {
    > int j;
    >
    > for(j = 0; j < m; j++)delete [] array[j];
    > delete [] array;
    > array=NULL;
    >
    > }
    >
    > //---------------------------------------------------------
    > //----------------------------------------------------
    > void delete1Darray(char* array)
    > {
    > delete [] array;
    > array=NULL;}
    >
    > //-------------------------------------------------
    >
    > //---------------------------------------------
    > void delete3Darray(char*** array, int m, int n)
    > {
    > int i, j;
    > for(i = 0; i < m; i++)
    > {
    > for(j = 0; j < n; j++)delete [] array[j];
    > delete [] array;
    > }
    >
    > delete [] array;
    >
    > }


    You can easily allocate the whole bunch of memory at once by creating
    a char array of size m*n*... (whatever the dimension is). Wrap it up
    to a class and provide access methods such as my_array(i,j).
     
    , Feb 10, 2008
    #2
    1. Advertising

  3. Klaib

    Daniel T. Guest

    Klaib <> wrote:

    > I am a student doing my project, my program needs a big arrays size.
    > i used the following functions to create and delete the pointers, but
    > i have problems based on virtual memory during the the run time "
    > after 2 or 3 hour", Please help me to increase cBuilder virtual
    > memory or solving this problem.


    If your program crashes due to starved memory after 2 or 3 hours, then
    you probably are not deleting your arrays.

    You can avoid this problem by wrapping your arrays in classes and using
    RAII to make sure the memory is deleted when you are done with it.

    A simple class to start with:

    class array3D {
    int m, n, i;
    std::deque<char> data;
    public:
    array3D( int m, int n, int i ):
    m( m ),
    n( n ),
    i( i ),
    data( m * n * i )
    { }

    char at( int x, int y, int z ) const {
    // can someone double check my math in the below?
    return data[x * n * i + y * i + z];
    }

    char& at( int m, int n, int i ) {
    return data[x * n * i + y * i + z];
    }
    };

    I use a deque in the above instead of a vector, because the OP is
    implying that his data is very large and a deque is less likely to fail
    when the memory is fragmented.
     
    Daniel T., Feb 10, 2008
    #3
  4. Klaib

    James Kanze Guest

    On Feb 10, 9:13 pm, "Daniel T." <> wrote:
    > Klaib <> wrote:
    > > I am a student doing my project, my program needs a big
    > > arrays size. i used the following functions to create and
    > > delete the pointers, but i have problems based on virtual
    > > memory during the the run time " after 2 or 3 hour", Please
    > > help me to increase cBuilder virtual memory or solving this
    > > problem.


    > If your program crashes due to starved memory after 2 or 3
    > hours, then you probably are not deleting your arrays.


    > You can avoid this problem by wrapping your arrays in classes
    > and using RAII to make sure the memory is deleted when you are
    > done with it.


    > A simple class to start with:


    > class array3D {
    > int m, n, i;
    > std::deque<char> data;
    > public:
    > array3D( int m, int n, int i ):
    > m( m ),
    > n( n ),
    > i( i ),
    > data( m * n * i )
    > { }


    > char at( int x, int y, int z ) const {
    > // can someone double check my math in the below?
    > return data[x * n * i + y * i + z];
    > }


    > char& at( int m, int n, int i ) {
    > return data[x * n * i + y * i + z];
    > }


    The usual algorithm is:

    return data[ (x*n + y)*i + z ] ;

    I think. I'd also throw in an assert:

    assert( x < m && y < n && z << this->i ) ;

    (I'd also change the names so that I didn't have two i's.)

    I'd also either overload operator()(), or overload operator[] to
    return proxies which called this function, so that he could use
    a more usual syntax. (I prefer the [][][] syntax, but that's
    probably because I've used it so much in C.)

    > };


    > I use a deque in the above instead of a vector, because the OP is
    > implying that his data is very large and a deque is less likely to fail
    > when the memory is fragmented.


    On the other hand, it requires globally somewhat more memory.
    I'd go with std::vector until I knew fragmentation was the
    problem. (Another nice thing about encapsulating everything in
    such a class, of course, is that you can easily change the
    implementation without any modifications in the client code.)

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Feb 11, 2008
    #4
  5. Klaib

    Klaib Guest

    First I would like to apply my grateful Thanks to all persons who sent
    a reply.

    really until now I didn't solve my problem, I tried to create a class.
    but same problem coming. Hope if u have additional suggestion or
    notes to send if u can.

    What I did exactly , I put the function (fun1) which calls other
    functions that responsible to create arrays ( create2Darray,
    create3Darray, create1Darray) in class.

    Example:

    Class RunningLongTime
    {
    public:
    void func1()
    {
    Loop{
    // Create arrays by calling the special
    functions out the class
    // Do a lot of process
    // Delete arrays
    // Resize arrays dimensions to smaller

    } // End of the long loop

    } //end of func1
    }; // end of class

    main(){
    finc1();
    func2();
    .
    .
    funcN();
    }

    func2(){..............................}

    func3(){...............................}


    char*** create3Darray(int m, int n, int l)
    {
    char*** array;
    int i;
    array = new char**[m];
    for (i = 0; i < m; i++)
    array = create2Darray(n, l);
    return array;
    }
    //-----------------------------------------------------------

    char** create2Darray(int n, int l)
    {
    char** array;
    int j;
    array = new char*[n];
    for (j = 0; j < n; j++)
    array[j] = new char[l];
    return array;
    }
    //--------------------------------------------------------
    char* create1Darray(int n)
    {
    char* array;
    array = new char[n];
    return array;
    }
    //---------------------------------------------------------
    void delete2Darray(char** array, int m)
    {
    int j;
    for(j = 0; j < m; j++)delete [] array[j];
    delete [] array;
    array=NULL;
    }

    //----------------------------------------------------
    void delete1Darray(char* array)
    {
    delete [] array;
    array=NULL;
    }
    //---------------------------------------------------
    void delete3Darray(char*** array, int m, int n)
    {
    int i, j;
    for(i = 0; i < m; i++)
    {
    for(j = 0; j < n; j++)delete [] array[j];
    delete [] array;
    }
    delete [] array;
     
    Klaib, Feb 14, 2008
    #5
  6. Klaib wrote:
    [...]
    > i have problems based on virtual memory during the the run time "
    > after 2 or 3 hour".


    Some notes on your code fragments. You should take them as a strong
    advice to wrap all this stuff in a class, as Daniel and James pointed out.

    > char*** create3Darray(int m, int n, int l)
    > {
    > char*** array;
    > int i;


    Lots of unitilialized variables here. This is likely to cause errors.
    You can write it shorter and clearer.

    > array = new char**[m];
    > for (i = 0; i < m; i++)
    > array = create2Darray(n, l);


    // for debugging:
    std::cout << "Create [" << m << "," << n << ",l]" << std::endl;

    > return array;
    > }



    Above function allocates a complex structure. What I'm missing _here_,
    is the corresponding destruction function. Yours is hidden far below,
    and it does _not_ look symmetric to create3Darray(). I've moved it up to
    here:

    > void delete3Darray(char*** array, int m, int n)
    > {
    > int i, j;
    > for(i = 0; i < m; i++)
    > {
    > for(j = 0; j < n; j++)delete [] array[j];
    > delete [] array;
    > }


    shouldn't it be delete2Darray( array, n ) for symmetry?

    // for debugging:
    std::cout << "Delete [" << m << "," << n << ",l]" << std::endl;

    > delete [] array;
    > }



    > char** create2Darray(int n, int l)
    > {
    > char** array;
    > int j;
    >
    > array = new char*[n];
    > for (j = 0; j < n; j++)
    > array[j] = new char[l];


    You wrote a function create1Darray(), but don't use it?

    > return array;
    > }



    The following functions are defined, but never used :(

    > char* create1Darray(int n)
    > {
    > char* array;
    > array = new char[n];
    >
    > return array;
    > }
    > //---------------------------------------------------------
    > //----------------------------------------------------
    >
    > void delete2Darray(char** array, int m)
    > {
    > int j;
    >
    > for(j = 0; j < m; j++)delete [] array[j];
    > delete [] array;
    > array=NULL;


    Nice try, but the pointer supplied in a call remains unchanged:

    delete2Darray(my_array, m);
    // my_array still points to (deleted) memory!
    > }
    >
    > //---------------------------------------------------------
    > //----------------------------------------------------
    > void delete1Darray(char* array)
    > {
    > delete [] array;
    > array=NULL;

    Same here.
    > }



    I bet you, that the debug output will either show less "Delete[]" than
    "Create[]" lines, or unveil a "Delete[]" with wrong sizes.

    After you have located the memory leak, I hope you won't patch some
    lines to fix it, but re-design your code, so that these problems will
    never happen again.

    -- ralph
     
    Ralph D. Ungermann, Feb 14, 2008
    #6
    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. Mahesh Prasad
    Replies:
    1
    Views:
    709
    Tom Wells
    Feb 22, 2004
  2. Cy Huckaba
    Replies:
    1
    Views:
    1,152
    Xie Xiao
    Jun 26, 2003
  3. Julián Sanz García

    RAM Memory or virual memory

    Julián Sanz García, Nov 12, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    899
    Julián Sanz García
    Nov 12, 2004
  4. Replies:
    11
    Views:
    833
    Ian Collins
    Feb 9, 2009
  5. Manjunatha

    Cookies not working in japanese vitual folder.

    Manjunatha, Aug 3, 2004, in forum: ASP General
    Replies:
    0
    Views:
    115
    Manjunatha
    Aug 3, 2004
Loading...

Share This Page