Re: vector and struct deallocation
On 9/1/2012 1:39 PM, Cristiano wrote:
On 01/09/2012 17:30, Paavo Helde wrote:
Why? What's wrong with:
struct Route {
std::string name, pid, nid;
};
struct WPT {
std::string id;
Route route;
};
std::vector<WPT> wpt;
See, the question of proper deallocation does not even arise here.
I've never used std:string.
There is always the first time for everything.
I implemented your code and it works, but considering that there are
around 30,000 to 50,000 WPT and that the "real" structures are:
struct ROUTE {
short type;
std::string name;
std::string Pid; short Pmea;
std::string Nid; short Nmea;
};
struct WPT {
std::string ID;
char type;
double lat, lon;
short var;
std::vector<ROUTE> route;
};
there could be any speed penalty or memory problems with those structures?
Before any yellow-bellied newbie starts to argue for the 'yes' answer to
that, let me quote the old wisdom: "first make it work, THEN make it
fast". When you write code, there will always be some kind of
performance "penalty". Code takes some time to run. Now, library code
is developed by people who are much better programmers than you and I.
Using their code means you can program more efficiently, get your code
to work sooner, make it more extensible and reusable. And it can be a
tiny bit slower or have a slightly larger footprint than, say, the same
functionality written in assembly.
It's called "trade-off". If you keep your code simple and easy to
understand and maintain, you will be able to speed it up at some point,
hopefully. But if you write spaghetti that you yourself barely can make
out, or get all the bugs out of, what speed improvement can be worth all
the trouble of keeping that code functional? Or do you practice
"write-only code"? "Fire and forget"?
You could compare the code that uses Standard library strings and
containers to your manual naked pointer allocation/deallocation, but you
have to get *your* code working first. Once you get there (and at this
point I am not really sure you have it all together), you will probably
see that the "performance penalty" is either negligible, or you're not
measuring it right. Is it necessary to go into trouble of "reinventing
the wheel" and writing your own allocations for what seems to be a bunch
of simple strings and an array of them (vector)?
Get yourself a good book on Standard Library. Nicolai Josuttis just
released the second edition of his great tutorial. Once you get it,
*study*. The biggest problem novices have is *believing that they know
enough*. Sorry, my friends, you *don't*. Yet. And it can only be
corrected by a *systematic learning*. So, tap into the good sources,
and feed your brain, it will appreciate it. Good luck!
V
P.S. Doubt is a good thing. If you doubt the performance, test it,
don't assume, and don't trust anybody's saying that "it's as fast" or
"it carries a penalty". People will tell you that because they want you
to respect them, or trust them, or for some other selfish reasons. The
only sensible thing to do in that situation, however, is to *measure*
the performance and compare it to the *requirements* that you establish
*ahead of time*. So, make sure you have the standard to which you want
to compare, and make sure that what you measure actually *matters*.
--
I do not respond to top-posted replies, please don't ask