"madhu" <ma*******@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
: 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_Typeleaf( Width );
for( long i = 0 ; i < Width ; ++i ) leaf[i]=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