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.


|