Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > vector and struct deallocation

Reply
Thread Tools

vector and struct deallocation

 
 
none
Guest
Posts: n/a
 
      09-03-2012
In article <k1vbq8$2v5$(E-Mail Removed)>,
Juha Nieminen <(E-Mail Removed)> wrote:
>Paavo Helde <(E-Mail Removed)> wrote:
>> Why not? std::string is easier to work with than a raw array. For example
>> as it has operator==, I can easily test if(wpt.id=="123456") instead of
>> using memcmp() or strcmp() or whatever is more appropriate. Also the
>> assumptions like "'id' member can have at most 6 characters" are often
>> prone to failing in some more or less distant future.

>
>Unless the particular std::string implementation happens to use short
>string optimization (which really isn't a given), it will be horribly
>inefficient and wastes memory. Even if it *does* implement short string
>optimization, it will still be a lot larger than 6 bytes. std::string
>doesn't protect you from out-of-bounds accesses either (except in the
>debug mode of some compilers).
>
>If you really need an operator== for it, either write it as a member of
>that struct or use std::array instead.
>
>There's an advantage to std::string over a static array only if the size
>of the string could grow. (Even then you should be aware of the possible
>efficiency problems.)


Hmm, not quite as simple as that. Even without going deep into the details:

std::string::size() will be faster than strlen on a static array
std::string:perator==() is faster than strcmp if length differ.

Particularly relevant since OP stated that the id vary in length.

So depending on the data and the usage, the space overhead of
std::string may be compensated by a speed improvements.

Yannick
 
Reply With Quote
 
 
 
 
Rui Maciel
Guest
Posts: n/a
 
      09-03-2012
Richard Damon wrote:

> On 9/2/12 2:46 PM, Rui Maciel wrote:
>> Juha Nieminen wrote:
>>
>>> If the 'id' member can have at most 6 characters, and especially if this
>>> is checked when it's assigned to, why on earth would you want to use
>>> std::string instead of char[6]?

>>
>> http://www.parashift.com/c++-faq/use...-char-ptr.html
>>
>>
>> Rui Maciel
>>

>
> That shows why std:string is better than raw char* pointers, not better
> than char[] arrays.


The very first sentence in that site reads as follows:

<quote>
If what you really want to do is work with strings, don't use an array of
char in the first place, since arrays are evil. Instead use an object of
some string-like class.
</quote>


Rui Maciel
 
Reply With Quote
 
 
 
 
Juha Nieminen
Guest
Posts: n/a
 
      09-04-2012
Yannick Tremblay <yatremblay@bel1lin202.(none)> wrote:
> std::string::size() will be faster than strlen on a static array
> std::string:perator==() is faster than strcmp if length differ.


If you need the length to be as fast as possible, you could just store
it as another member variable (and then write some member functions to
make it easy to do any operations related to it).

In fact, since the length cannot ever be larger than 6, you can make the
integral variable a small one, such as a short or a char, in which case
the entire id string (contents and size) will take a nice round 8 bytes
from the struct object.
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      09-04-2012
Rui Maciel <(E-Mail Removed)> wrote:
> The very first sentence in that site reads as follows:
>
> <quote>
> If what you really want to do is work with strings, don't use an array of
> char in the first place, since arrays are evil. Instead use an object of
> some string-like class.
> </quote>


"Array" is an ambiguous term in C/C++ because there are two types of
arrays: Static arrays and dynamically allocated arrays. These are two
completely different beasts (even though a pointer of the same type can
point to either one, and the former can be implicitly casted to such a
pointer).

From the context, and from the rest of the article, it's clear that the
first paragraph is talking about dynamically allocated char arrays.

In general, in the vast majority of cases, there's little reason to not
to prefer std::string over handling "raw" dynamically allocated char arrays
if you are handling strings. The former is just a lot safer, offers more,
and is often more efficient (for example std::string::size() is faster than
the C equivalent strlen().)

However, when we are talking about static arrays, the problem is not as
straightforward. A beginner should prefer using std::string even in this
case because static arrays are pretty low-level and require for you to
know what you are doing. However, for experienced programmers static arrays
offer a great tool for optimization (both in terms of memory consumption and
speed). Of course a big emhpasis on "you have to know what you are doing".
Using static arrays requires more care and attention to detail than using
std::string, but when correctly used, the result can be significantly more
efficient. (Of course such optimization is worth only in cases where it
really matters, eg. if you need to instantiate a struct containing such
a string millions of times. In a one-shot case it's often not worth the
extra effort.)
 
Reply With Quote
 
none
Guest
Posts: n/a
 
      09-04-2012
In article <k202sn$qlp$(E-Mail Removed)>,
Cristiano <(E-Mail Removed)> wrote:
>On 01/09/2012 15:55, Cristiano wrote:
>> Is the deallocation procedure correct?

>
>I would like to thank all of you, but nobody answered my question.
>Now I use a totally different approach, but it could be still
>interesting to know the correct deallocation procedure.


Divide and conquer.

You have a struct WPT and a struct ROUTE.
Create a function "FreeStructWPT(WPT *) and
a function FreeStructROUTE(ROUTE *)

Make sure that the functions are safe and work independently

Now if you want to free a collection of WPT *, you call
FreeStructWPT() for each of the contained pointers. Etc.

Once you have done it and are confortable with it, rename your
function FreeStructWPT(WPT *) to WPT::~WPT(). you've just
invented destructor

Yannick






 
Reply With Quote
 
none
Guest
Posts: n/a
 
      09-04-2012
In article <k24ce7$sqe$(E-Mail Removed)>,
Juha Nieminen <(E-Mail Removed)> wrote:
>Yannick Tremblay <yatremblay@bel1lin202.(none)> wrote:
>> std::string::size() will be faster than strlen on a static array
>> std::string:perator==() is faster than strcmp if length differ.

>
>If you need the length to be as fast as possible, you could just store
>it as another member variable (and then write some member functions to
>make it easy to do any operations related to it).
>
>In fact, since the length cannot ever be larger than 6, you can make the
>integral variable a small one, such as a short or a char, in which case
>the entire id string (contents and size) will take a nice round 8 bytes
>from the struct object.


Agree. But my point remains: there are other advantages to
std::string over static arrays. Not just if the size can grow
dynamically.

Yannick

 
Reply With Quote
 
Krice
Guest
Posts: n/a
 
      09-04-2012
On 1 syys, 16:55, Cristiano <(E-Mail Removed)> wrote:
> * * if(wpt[i]->route) {


Try to remove this check and see what happens. If you always allocate
route it's not needed anyway.
 
Reply With Quote
 
Cristiano
Guest
Posts: n/a
 
      09-04-2012
On 04/09/2012 13:26, Krice wrote:
> On 1 syys, 16:55, Cristiano <(E-Mail Removed)> wrote:
>> if(wpt[i]->route) {

>
> Try to remove this check and see what happens.


Unfortunately I totally changed the code to use std::string (when I
don't use char ID[6]).

> If you always allocate route it's not needed anyway.


Some WPTs don't need route.
You see
WPT *wp= new WPT; wpt.push_back(wp);
ROUTE *rou= new ROUTE; wp->route= rou;
just because I posted the "typical" program flow (without branches).
It is possible that I did some "strange" things inside the branches, but
now that I deleted the original code, we'll never know.

Cristiano
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      09-04-2012
On Sun, 2012-09-02, Cristiano wrote:
> On 01/09/2012 20:19, Victor Bazarov wrote:

....
>> Get yourself a good book on Standard Library.

>
> I have two old books,


Old books on C++ are often worse than no books at all.
Like Victor writes, get a good one.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
3D vector memory deallocation madhu C++ 6 11-13-2006 07:02 AM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
Vector pointer and deallocation Andy Chang C++ 7 01-05-2005 03:52 PM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM



Advertisments