Vitual memory problem

K

Klaib

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;
}
 
M

mr.xiaofan.li

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).
 
D

Daniel T.

Klaib said:
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.
 
J

James Kanze

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.)
 
K

Klaib

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;
 
R

Ralph D. Ungermann

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top