Talk About Network

Google





Programming > C - C++ Learning > Re: String from...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 15 Topic 4202 of 4400
Post > Topic >>

Re: String from C to C++

by "Daniel T." <daniel_t@[EMAIL PROTECTED] > Jul 14, 2008 at 09:05 AM

In article 
<dd4e9b5a-502b-49af-a572-a5251f6763e5@[EMAIL PROTECTED]
>,
 lau.dvd@[EMAIL PROTECTED]
 wrote:

> I am starting to write C++ programs this summer, and I have background
> in C
> and Delphi (Object pascal).. When I wrote in C, strings are passed by
> char*,
> and I malloc/free all strings I need.. In Delphi, all strings are
> managed (off topic)...
> 
> I'm compiling using gcc 4.0.1 on Mac OS X,  but I am trying to stick
> to STL and none of the platform-specific facilities.
> 
> First, in C++.. I have this program:
> 
> #include <iostream>
> #include <string>
> 
> std::string giveMeXyz()
> {
>    std::string res;
>    res = std::string("Here is Xyz");
>    return res;
> }
> 
> int main (int argc, char * const argv[])
> {
>    std::string abc("Abcdefg"), xyz;
> 
>    xyz = giveMeXyz() ;
>    std::cout << abc << std::endl;
>    std::cout << xyz << std::endl;
> 
>    return 0;
> }
> 
> 
> This program runs well, but I don't know if there are any memory
> leaks/unfreed objects.

If you didn't call 'new', 'new[]', 'malloc' or 'calloc', then you don't 
have ny memory leaks/unfreed objects.

> So my questions are:
> 
> (1) During compile time, this line
> 
>    std::string abc("Abcdefg"), xyz;
> 
> Allocates two strings for two strings already, so I don't need to
> instantiate another string?

I'm not sure what you are asking. What other string are you 
instantiating in main?

> (2) In the function giveMeXyz(), was any instantiation going on? Since
> I
> declared "res" within the function, so when the function terminates,
> "res"
> would be lost, right? In C, if I have a function within whose scope,
> some
> variable "localVar" is declared, and at the end of the scope,
> "localVar"
> would be gone.. How would it fit in the C++ case?

Yes, 'res' is lost, but a copy of 'res' is passed to the caller. Your 
giveMeXyz works much like it would if 'string' was an int. 'res' would 
be lost, but a copy of it would still get passed to calling function.

> (3) How can I manually instantiate a string? For example, I'd like to
> keep
> asking the user for "names" until "END" is entered, in C, I would do
> something like this:
> 
> void keepAskingUntilEnd()
> {
>    char* userInput=NULL;
>    do {
>        userInput = (char*)calloc( 81, sizeof(char) );
>        fscanf(stdin, "%s", userInput);
>        addCharToSomeSortOfStorage(userInput, 81);
>    } while( 0==strcmp(userInput,"END") );
> 
>    // ... call free() individually to free up the memory allocated
> 
> }
> 
> In C++, I am told that use std::string wherever possible over char*.
> How can I do the above with std::string?

void addCharToSomeSortOfStorage( const string& s );

void keepAskingUntilEnd()
{
   string userInput;
   while ( cin >> userInput && userInput != "END" ) {
      addCharToSomeSortOfStorage( userInput );
   }
}

> I'm hoping to get some way to
> explicitly "instantiate" a new string object and then either add it to
> a list..linked-list...etc.

list< string > myLinkedList;

void addCharToSomeSortOfStorage( const string& s ) {
   myLinkedList.push_back( s );
}

> ... Or should I just use char* to store the buffer instead?

No.
 




 15 Posts in Topic:
String from C to C++
lau.dvd@[EMAIL PROTECTED]  2008-07-14 05:34:12 
Re: String from C to C++
"Daniel T." <  2008-07-14 09:05:20 
Re: String from C to C++
Bart van Ingen Schenau &l  2008-07-14 19:53:17 
Re: String from C to C++
lau.dvd@[EMAIL PROTECTED]  2008-07-14 19:40:10 
Re: String from C to C++
LR <lruss@[EMAIL PROTE  2008-07-15 01:49:45 
Re: String from C to C++
Bart van Ingen Schenau &l  2008-07-15 19:32:58 
Re: String from C to C++
Jerry Coffin <jcoffin@  2008-07-15 12:23:29 
Re: String from C to C++
Bart van Ingen Schenau &l  2008-07-16 17:43:55 
Re: String from C to C++
Francis Glassborow <fr  2008-07-15 09:03:38 
Re: String from C to C++
lau.dvd@[EMAIL PROTECTED]  2008-07-15 20:47:33 
Re: String from C to C++
Bart van Ingen Schenau &l  2008-07-16 17:53:34 
Re: String from C to C++
LR <lruss@[EMAIL PROTE  2008-07-16 13:32:02 
Re: String from C to C++
"Daniel T." <  2008-07-16 21:48:31 
Re: String from C to C++
lau.dvd@[EMAIL PROTECTED]  2008-07-17 19:09:56 
Re: String from C to C++
Bart van Ingen Schenau &l  2008-07-18 17:10:24 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
localhost-V2008-12-19 Fri Jan 9 6:17:19 PST 2009.