473,603 Members | 2,635 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

3D vector memory deallocation

vector<vector<v ector<long Vector3D; // 3dvector.

for (long k = 0; j < Depth; j++ )
{
Vector3D.push_b ack ( 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?

Nov 11 '06 #1
6 2356
madhu wrote:
vector<vector<v ector<long Vector3D; // 3dvector.

for (long k = 0; j < Depth; j++ )
{
Vector3D.push_b ack ( 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.

Nov 11 '06 #2

"madhu" <ma*******@gmai l.comwrote in message
news:11******** **************@ h48g2000cwc.goo glegroups.com.. .
: vector<vector<v ector<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_b ack ( 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_Typele af( Width );
for( long i = 0 ; i < Width ; ++i ) leaf[i]=i;
vector<vector<v ector<long >
Vector3D( Depth, vector<vector<l ong( 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(ve ctor), 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

Nov 11 '06 #3

Ivan Vecerina wrote:
"madhu" <ma*******@gmai l.comwrote in message
news:11******** **************@ h48g2000cwc.goo glegroups.com.. .
: vector<vector<v ector<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_b ack ( 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_Typele af( Width );
for( long i = 0 ; i < Width ; ++i ) leaf[i]=i;
vector<vector<v ector<long >
Vector3D( Depth, vector<vector<l ong( 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(ve ctor), 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.

Nov 11 '06 #4
"madhu" <ma*******@gmai l.comwrote in message
news:11******** **************@ m7g2000cwm.goog legroups.com...
: : vector<vector<v ector<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(ve ctor), 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<v ector<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

Nov 11 '06 #5

madhu wrote in message
<11************ **********@h48g 2000cwc.googleg roups.com>...
>vector<vector< vector<long Vector3D; // 3dvector.
for (long k = 0; j < Depth; j++ ){
Vector3D.push_b ack ( 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<v ector<long Vector3D; // 3dvector.

try{
for (long k = 0; k < Depth; ++k ){
Vector3D.push_b ack ( 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::out_of_ran ge &Oor ){
std::cout<<"cau ght "<<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::v ector<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).siz e(); ++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).a t(y).at(z)<<" // before"<<std::e ndl;
vec3D.at(x).at( y).at(z) = z;
std::cout<<" vec3D.at("<<x<< ").at("<<y<<"). at("<<z<<")= "
<<vec3D.at(x).a t(y).at(z)<<" // after"<<std::en dl;
} // for(z)
} // for(y)
std::cout<<std: :endl;
} // for(x)
std::cout<<std: :endl;
}
// ------------

--
Bob R
POVrookie
Nov 11 '06 #6
Well Ivan Thanks 4 ur help. Yourr suggetion was helpful to us.

Madhukar Arvind

Ivan Vecerina wrote:
"madhu" <ma*******@gmai l.comwrote in message
news:11******** **************@ h48g2000cwc.goo glegroups.com.. .
: vector<vector<v ector<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_b ack ( 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_Typele af( Width );
for( long i = 0 ; i < Width ; ++i ) leaf[i]=i;
vector<vector<v ector<long >
Vector3D( Depth, vector<vector<l ong( 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(ve ctor), 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
Nov 13 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2299
by: Tino | last post by:
I have a std::vector<int> which, after some initialization, has a fixed number of elements...after initialization I must do the following repeatedly: I remove an element which could be anywhere in the vector, and add another element which will always be at the end, ie. vector<int> v; int i, x; .... initialization
4
1681
by: John | last post by:
Hi all: I write the following code about vector and list. Why the result is not correct? Thanks. John --------------------------------------------------------------
11
2649
by: aaaaa | last post by:
Hi all, Does anybody know if STLPort or SGI STL standard allocators do memory pooling for the list, map and set? Also I have had a look at the BOOST pool_alloc (to be used as a pooling allocator for lists), but looking into the code it doesn't seem to ever release (to the global ::free) the memory that was once allocated. I can understand that such memory can be re-used if I have another list
7
2402
by: Andy Chang | last post by:
Hi, If I have typedef struct { std::vector <float> vSomeVal; int i; } VEC_T; Then is VEC_T * ptr a valid pointer to the struct accessing the vector? Also do I need to explicitly deallocate the vector by calling the clear()
5
2330
by: christophe (dot) poucet (at) gmail (dot) com | last post by:
Hello, I noticed there is a flaw in the vector implementation of g++ (3.4). Basically when you erase an element from a vector, it calls the wrong destructor. In most cases this is not such a big issue, but if one were to store a class holding a resource, then the wrong resource would be freed. Here is a test case to show what I mean: (Disregard the destructors called for the temporaries).
5
6142
by: yancheng.cheok | last post by:
after reading http://www.codeproject.com/vcpp/stl/vector_vs_deque.asp, i realize that deque has its own speed advantage over vector in all the aspect (except for memory deallocation). does it mean that we should prefer deque over vector? (in contrast with c++ standard, which recommence vector over deque) thanks!
3
1782
by: Dave | last post by:
A quick question: vector <T *test_vec; T testT; test_vec.push_back(&testT); T * testT2p = new T; test_vec.push_back(testT2p); Now at the end do I need to explicitly delete the vector (test_vec).
13
2872
by: smp | last post by:
Does anyone know why making a vector of pointers is so much less efficient than a vector of objects? For a simple example: int num = 20; vector<int*v_int_ptr; v_int_ptr.reserve(num); for(int i = 0; i < num; i++) { int* my_int_ptr = new int;
42
4510
by: barcaroller | last post by:
In the boost::program_options tutorial, the author included the following code: cout << "Input files are: " << vm.as< vector<string() << "\n"; Basically, he is trying to print a vector of string, in one line. I could
0
7996
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8415
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8405
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8060
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8273
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5441
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3903
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2430
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1259
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.