Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


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

Re: String from C to C++

by Bart van Ingen Schenau <bart@[EMAIL PROTECTED] > Jul 14, 2008 at 07:53 PM

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)...

In C++, the std::string class does the managing of memory itself under
the hood.
The usage pattern of a C++ std::string looks more like Delphi's
AnsiString than like C-style char* strings.

> 
> 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. 

I can tell you, there are none.
The only memory/objects that you need to manage yourself (unless you
explicitly delegate the management) are those that are allocated with
new, new[] or *alloc().
As you don't use any of them directly in your program, you don't need to
worry about cleaning up.

The STL cl***** are well-behaved, in the sense that whatever resources
they reserve during their lifetime, they will also take care to return
those resources.

> 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?

This line creates two std::string objects.
One of them will, after construction, manage a buffer containing at
least the 7 characters 'A' 'b' 'c' 'd' 'e' 'f' 'g'.
The other will probably not manage any buffer yet, or it might manage an
empty buffer. The difference will not be (easily) observable.

If, at a later time, you assign new contents to one of those std::string
objects, it is the responsibility of that object to make sure its
buffer can hold the new contents.

> 
> (2) In the function giveMeXyz(), was any instantiation going on? 

Yes. Declaring a variable means that at runtime, something gets
instantiated when you execute that line.

> 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?

In C++, the rules are exactly the same.
The only difference is that in C++, when an object is destroyed, it gets
a chance to clean up after itself.

The last two lines of giveMeXyz (the return statement and the closing
brace) trigger the following set of actions (the compiler may optimise
this a bit, this is the conceptual case):
- copy res to the return value (return statement)
- destroy res (closing brace; res cleans up whatever extra memory it
used)
- return to the calling function (main)
<in the context of main>
- copy the return value of giveMeXyz to xyz

> 
> (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? 

Here is the equivalent to your function in C++:

void keepAskingUntilEnd()
{
  std::string userInput;
  std::vector<std::string> someContainer;
//or: std::list<std::string> someContainer;

  do {
    std::cin >> userInput;
    someContainer.push_back(userInput);
  } while (userInput != "END");
}
/* No explicit delete: someContainer will clean up all the strings it
contains automatically */
  
> 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. When I can overcome this basic issue, I'm
> hoping to use it on UnicodeString::UnicodeString of ICU.
> 
> ... Or should I just use char* to store the buffer instead?

If you are fond of the nightmares that go with debugging memory leaks
and/or corruption, go ahead. ;-)

> 
> Thanks a lot!

Bart v Ingen Schenau
-- 
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://c-faq.com/
c.l.c++ FAQ: http://www.para****ft.com/c++-faq-lite/
 




 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
tan12V112 Fri Sep 5 2:54:56 CDT 2008.