|
From: Per W. <pw...@ia...> - 2005-04-26 16:35:03
|
When talking about performance, it isn't the quality of the C++ compiler
that is affecting the runtime. It's the algorithmic differences that
matter - number of memory allocations, or the number of string copies.
A number of memory allocations needed when using std::string can be
avoided when using char[] constructs - a lot of strings do have a known
maximum length.
On the other hand, std::string can perform assigns from other std::string
objects without allocating/copying any data, since multiple std::string
objects can share a common string buffer.
/Per W
On Tue, 26 Apr 2005 Mat...@Ve... wrote:
> Yes, it works, but char* is not a very good thing to use any more. It
> creates arbitrary limits on lengths that lead to bugs and security problems.
> On top of this, you get a minimal performance increase because C++ compilers
> and compiler optimization are a great deal better than they used to be not
> long ago.
>
> The whole point of C++ is avoiding these issues with generic programming and
> classes. Best of all the performance hit is minimal. If you look at
> basically every other language in use today besides C, strings are handled
> automatically.
>
> GAWK, perl, C++, etc. all do it using string types.
>
> Sometimes it's not good enough for it just to work. There are other factors
> to consider.
>
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...]On Behalf Of Verne H.
> Bohlender
> Sent: Saturday, February 26, 2005 8:42 AM
> To: Dev C++; Rafael Charnovscki
> Subject: Re: [Dev-C++] Sentences in a array
>
>
> The program works fine once you use what comes with Dev C++ and not
> substitued the headings. I just clipped the code and used the console
> settings and it worked perfectly. Now I'm not sure what he wants it to do
> but it gave me no problems at all.
> Verne the real amateur at C++
>
> > > Alexsandro Meireles wrote:
> >> array[0] = "sentence 1";
> >> array[1] = "sentence 2"
> >> and so on...
> >
> > There are some ways I can remember.
> > The point is defining your array as
> > an array to char pointers.
> >
> > //initializing an array of strings
> > char* sentences[2]={"sentence 1", "sentence 2"};
> >
> > //assigning strings to the array
> > char* sentences[2];
> > sentences[0]="another sentence 1";
> > sentences[1]="another sentence 2";
> >
> > /* allocating memory dinamically and copying
> > the string */
> > strings[0]=(char *)malloc(sentenceLength + 1);
> > strcpy(strings[0],"sentence 1 again");
> >
> > It could be also an bidimensional array of char, but
> > I'd rather use pointers:
> > char sentences[20][101]; //20 sentences with a limit of 100 characters
> >
> > Maybe other developers have more suggestions.
> > I sent a program (attached) just as an example.
> >
> > Hope it helps,
> > Rafael
> >
>
>
> ----------------------------------------------------------------------------
> ----
>
>
> > #include <stdlib.h>
> > #include <string.h>
> >
> > int main (){
> > //initializing an array of strings
> > char* sentences[2]={"sentence 1", "sentence 2"};
> > char* strings[2];
> > int sentenceLength; //(number of characters of your sentence)
> > int i;
> > for (i=0;i<2;i++)
> > printf("%s\n", sentences[i]);
> > system("PAUSE");
> > sentences[0]="another sentence 1";
> > sentences[1]="another sentence 2";
> > for (i=0;i<2;i++)
> > printf("%s\n", sentences[i]);
> > system("PAUSE");
> > sentenceLength = 30; //just an example
> > strings[0]=(char *)malloc(sentenceLength + 1);
> > strcpy(strings[0],"sentence 1 again");
> > sentenceLength = 60; //just an example
> > strings[1]=(char *)malloc(sentenceLength + 1);
> > strcpy(strings[1],"sentence 2 again");
> > for (i=0;i<2;i++)
> > printf("%s\n", strings[i]);
> > system("PAUSE");
> > return 0;
> > }
> >
> >
> >
>
>
> ----------------------------------------------------------------------------
> ----
>
>
> No virus found in this outgoing message.
> Checked by AVG Anti-Virus.
> Version: 7.0.308 / Virus Database: 266.10.3 - Release Date: 25/4/2005
|