-
March 11th, 2009, 03:42 PM
#1
destroying STL vectors explicitly?
Hi,
I have a rather large, memory intensive program that I'm trying to slim down. Within this program are class instances parts of which have to persist to the end of the run and parts of which could be deleted earlier in order to slim things down. I know it may sound like a bad idea from a extensibility and reusability perspective, but I'd love to be able to destroy some STL vectors that become unnecessary to maintain. More specifically, I've got a "node" class with floats (x,y,z coords) and an stl vector that contains int indices of tetrahedra to which the nodes belong. STL works well here, because the number of tetrahedra varies from node to node.
Anyway, the code creates a vector of these nodes. After I do some things, I no longer need the vector of ints described above for each node. With millions of nodes, that space is precious to me.
So here's the question. Can I do something
Code:
vector<int> *inTetr;
inTetr = new vector<int>;
...
delete inTetr;
Is there a better way? And if I do it this way, how do I randomly access the entries? Thanks ahead of time for any help!
-
March 11th, 2009, 04:23 PM
#2
Re: destroying STL vectors explicitly?
You can do it that way. Access the members with the '->' operator, or:
Code:
int someVal = (*inTetr)[3];
Another options is to enclose this section in it's own block:
Code:
int main()
{
int someVal = 0;
{
std::vector<int> someVec;
...
}
// someVec is now out of scope, and destroyed
}
Viggy
Last edited by MrViggy; March 11th, 2009 at 04:25 PM.
-
March 11th, 2009, 04:26 PM
#3
Re: destroying STL vectors explicitly?
1) Yes, it will work, but I prefer not to have a pointer to a vector.
2) random access is done using operator [] or the .at() member function
(but somehow, I don't think that is what you were asking).
3) Another way is to clear the vector and use the "swap trick" to reduce
its capacity in addition to just its size.
Code:
vector<int> v;
// add to v ...
// clear and minimize its capacity
vector<int>().swap(v);
-
March 12th, 2009, 10:24 AM
#4
Re: destroying STL vectors explicitly?
Originally Posted by MrViggy
You can do it that way. Access the members with the '->' operator, or:
Code:
int someVal = (*inTetr)[3];
Another options is to enclose this section in it's own block:
Code:
int main()
{
int someVal = 0;
{
std::vector<int> someVec;
...
}
// someVec is now out of scope, and destroyed
}
Viggy
Ah, okay. That's a head-slapper. I was using the reference operator, but I wasn't placing the parentheses. Though it makes perfect sense why that would be necessary.
I knew about the swap trick, but that just felt...dirty somehow. I wanted to completely delete the vector.
-
March 12th, 2009, 10:34 AM
#5
Re: destroying STL vectors explicitly?
Originally Posted by fadecomic
I knew about the swap trick, but that just felt...dirty somehow. I wanted to completely delete the vector.
Along the lines of MrViggy's second suggestion, I suspect that your code could do with more extensive use of functions that do one thing and do it well.
-
March 12th, 2009, 11:39 AM
#6
Re: destroying STL vectors explicitly?
The functions are fairly singular in task. The problem is that logically, inTetr is part of the description of a node. It's a piece of information that's actually needed by a multitude of subroutines. However, the code rather dramatically "shifts gears". It's a finite element modeling code. inTetr is needed to create the mesh and populate the A and b of an Ax=b linear system. However, after that's done, I no longer need it. I do, however, need the x,y, and z coordinates of each node, so I cannot simply delete the nodes or allow them to go out of scope.
I could simply take inTetr out of the node class and eliminate the problem all together, but that sort of defeats the whole idea of objects and data encapsulation, which are perfect for a code like this.
Code proceeds as 1. Create model, which creates a mesh, which creates and connects together nodes and tetrahedra. 2. Populate A and b. 3. Solve Ax=b 4. do things that need mesh information, but not ALL mesh information. I could follow MrViggy's second suggestion by placing (1) in a scope, but the mesh and node objects cannot go out of scope. The logical choice seems to be to delete choice parts of node (and others, inTetr is just a single example). The solver for Ax=b is intensive, and could use the memory.
Incidentally, I originally wrote this code in F77, which I must admit made the math a lot more readable given the high level of F77. However, having a bunch of free floating vectors lying around like inTetr outside of node is exactly one of the things I'm trying to avoid using C++.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|