From: Sebastian Huber <sebastian-huber@web.de>
To: gcc-help@gcc.gnu.org
Subject: Re: Error with basic string
Date: Fri, 24 Jan 2003 12:33:00 -0000 [thread overview]
Message-ID: <200301241330.14525.sebastian-huber@web.de> (raw)
In-Reply-To: <616BE6A276E3714788D2AC35C40CD18D5EC778@whale.softwire.co.uk>
Hello!
On Thursday 23 January 2003 19:31, Rupert Wood wrote:
> Ajay Bansal wrote:
> > char *func(string a, int n)
> > {
> > a.resize(n);
> > return a.begin();
> > }
>
> a.begin() is an iterator. You'll have to dereference it to get the
> character and then re-reference that for the pointer, i.e. (bracketed
> for clarity)
>
> char *func(string a, int n)
> {
> a.resize(n);
> return &(*(a.begin()));
> }
You should use 'return const_cast<char*>( a.data())' or 'a.c_str()'. But you
have to keep in mind, that every non-constant method of std::string may make
the data invalid.
> *However*
>
> 1) you're not passing your string by reference; what you're
> telling it to do is to take a copy of string a, resize the
> copy to n characters and then return a pointer to the first
> character in the copy - i.e. a pointer to memory owned by a
> dead object
>
> 2) (I'm not an STL guru so take this with a pinch of salt)
> I don't think there's a requirement that the string be
> stored continuously in memory, except for a read-only copy
> between c_str() the next non-const operation. It looks like
> you want func to allocate you an n-byte continuous character
> buffer in a basic_string and, whilst some implementations will
> give you this, I don't think you can assume all will. (But you
> probably know this - the comment about changing the return
> type?) I can't tell you if G++'s STL plays along.
>
> Rup.
next prev parent reply other threads:[~2003-01-24 12:33 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <616BE6A276E3714788D2AC35C40CD18D9EDBA4@whale.softwire.co.uk>
2003-01-23 18:32 ` Rupert Wood
2003-01-24 12:33 ` Sebastian Huber [this message]
2003-01-23 18:57 Ajay Bansal
-- strict thread matches above, loose matches on Subject: below --
2003-01-23 18:20 Ajay Bansal
2003-01-23 18:10 Ajay Bansal
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=200301241330.14525.sebastian-huber@web.de \
--to=sebastian-huber@web.de \
--cc=gcc-help@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).