473,387 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

how is string::c_str() implemented?

Hello,

I'm doing an exercise that defines a new abstract class "Str" that has
the same functions as "string" class, which holds a vector of chars.
But since I'm also trying to define a "c_str()" function, which copies
the chars from the vector into an array and return the pointer, I
think I need add another char array to my "Str" class. Now, the
problem is the size of the array has to be known in advance, but
obviously, the array's size changes when the corresponding
vector<charchanges. Any suggestions on how I should implement the
c_str() function?

Thanks,
Jess

May 6 '07 #1
8 5930
Jess wrote:
Hello,

I'm doing an exercise that defines a new abstract class "Str" that has
the same functions as "string" class, which holds a vector of chars.
But since I'm also trying to define a "c_str()" function, which copies
the chars from the vector into an array and return the pointer, I
think I need add another char array to my "Str" class. Now, the
problem is the size of the array has to be known in advance, but
obviously, the array's size changes when the corresponding
vector<charchanges. Any suggestions on how I should implement the
c_str() function?
c_str() simply needs to ensure that the character at one beyond the end
of the string contains a null. (i.e. '\000').

One way is to always allocate one more char than is needed and when
c_str() is called, plonk a nul at the end and return a pointer to your
internal buffer. This is probably the simplest solution.

Other solutions involve creating an auxiliary buffer as you noted.
May 6 '07 #2
Jess wrote:
Hello,

I'm doing an exercise that defines a new abstract class "Str" that has
the same functions as "string" class, which holds a vector of chars.
But since I'm also trying to define a "c_str()" function, which copies
the chars from the vector into an array and return the pointer, I
think I need add another char array to my "Str" class.
If you already have it as a vector of char, there is no need to copy it.
Now, the problem is the size of the array has to be known in advance, but
obviously, the array's size changes when the corresponding
vector<charchanges. Any suggestions on how I should implement the
c_str() function?
You could use a dynamically allocated array.

May 6 '07 #3
On May 6, 9:29 pm, Gianni Mariani <gi3nos...@mariani.wswrote:
c_str() simply needs to ensure that the character at one beyond the end
of the string contains a null. (i.e. '\000').

One way is to always allocate one more char than is needed and when
c_str() is called, plonk a nul at the end and return a pointer to your
internal buffer. This is probably the simplest solution.
My internal buffer isn't an array, so perhaps I can't return a pointer
to it?
Other solutions involve creating an auxiliary buffer as you noted.
To create an auxiliary buffer, then I think I need to have a member
that is a char*, because I can't create a static array (since I don't
know its size in advance)...

Thanks,
Jess

May 7 '07 #4
On May 6, 9:38 pm, Rolf Magnus <ramag...@t-online.dewrote:
If you already have it as a vector of char, there is no need to copy it.
Since c_str() returns an array of chars, do you mean I need to convert
the vector into an array?
>
Now, the problem is the size of the array has to be known in advance, but
obviously, the array's size changes when the corresponding
vector<charchanges. Any suggestions on how I should implement the
c_str() function?

You could use a dynamically allocated array.
Is there a way to keep a dynamically allocated array as a member of a
class? I guess it's impossible. Then I think I can keep a char* as a
member and let it point to a dynamically allocated array (after I do
"new char[...]"). Is this the correct approach?

Thanks,
Jess

May 7 '07 #5
On 7 Maj, 08:55, Jess <w...@hotmail.comwrote:
On May 6, 9:38 pm, Rolf Magnus <ramag...@t-online.dewrote:
If you already have it as a vector of char, there is no need to copy it.

Since c_str() returns an array of chars, do you mean I need to convert
the vector into an array?
Yes, a special property of std::vector is that all the elements are
guaranteed to be stored in contiguous memory (this means that all(?)
implementations of vector uses an array to store the elements. This
means that if you have a vector vec, then &vec[0] will give you a
pointer to the first element of the array used to store the elements.

So all you have to do is to make sure that you always keep an 0-char
at the end of the vector used to store your string in and then c_str()
will be as simple as 'return &vec[0];'. However remember that you have
the 0-char when you perform certain operations such as append so that
you don't add the new text after it.
Now, the problem is the size of the array has to be known in advance,but
obviously, the array's size changes when the corresponding
vector<charchanges. Any suggestions on how I should implement the
c_str() function?
You could use a dynamically allocated array.

Is there a way to keep a dynamically allocated array as a member of a
class? I guess it's impossible. Then I think I can keep a char* as a
member and let it point to a dynamically allocated array (after I do
"new char[...]"). Is this the correct approach?
Yes, you can have a char* as a member and each time the string changes
you delete[] the old one and new[] a new char array. However as
described above this is not necessary, nor is it a good solution since
it would mean that you kept two instances of the string, on in the
vector and one in the char*.

--
Erik Wikström

May 7 '07 #6

"Erik Wikström" <er****@student.chalmers.sewrote in message
news:11**********************@w5g2000hsg.googlegro ups.com...
On 7 Maj, 08:55, Jess <w...@hotmail.comwrote:
On May 6, 9:38 pm, Rolf Magnus <ramag...@t-online.dewrote:
If you already have it as a vector of char, there is no need to copy
it.
Since c_str() returns an array of chars, do you mean I need to convert
the vector into an array?

Yes, a special property of std::vector is that all the elements are
guaranteed to be stored in contiguous memory (this means that all(?)
implementations of vector uses an array to store the elements. This
means that if you have a vector vec, then &vec[0] will give you a
pointer to the first element of the array used to store the elements.
The horrendous vector<boolof course being the sole exception to this rule.
;)

- Sylvester
May 7 '07 #7
Thanks!

Jess

On May 7, 5:23 pm, Erik Wikström <eri...@student.chalmers.sewrote:
On 7 Maj, 08:55, Jess <w...@hotmail.comwrote:
On May 6, 9:38 pm, Rolf Magnus <ramag...@t-online.dewrote:
If you already have it as a vector of char, there is no need to copy it.
Since c_str() returns an array of chars, do you mean I need to convert
the vector into an array?

Yes, a special property of std::vector is that all the elements are
guaranteed to be stored in contiguous memory (this means that all(?)
implementations of vector uses an array to store the elements. This
means that if you have a vector vec, then &vec[0] will give you a
pointer to the first element of the array used to store the elements.

So all you have to do is to make sure that you always keep an 0-char
at the end of the vector used to store your string in and then c_str()
will be as simple as 'return &vec[0];'. However remember that you have
the 0-char when you perform certain operations such as append so that
you don't add the new text after it.
Now, the problem is the size of the array has to be known in advance, but
obviously, the array's size changes when the corresponding
vector<charchanges. Any suggestions on how I should implement the
c_str() function?
You could use a dynamically allocated array.
Is there a way to keep a dynamically allocated array as a member of a
class? I guess it's impossible. Then I think I can keep a char* as a
member and let it point to a dynamically allocated array (after I do
"new char[...]"). Is this the correct approach?

Yes, you can have a char* as a member and each time the string changes
you delete[] the old one and new[] a new char array. However as
described above this is not necessary, nor is it a good solution since
it would mean that you kept two instances of the string, on in the
vector and one in the char*.

--
Erik Wikström

May 7 '07 #8
Jess wrote:
Thanks!
Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.html>
May 7 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Leif K-Brooks | last post by:
Has anyone ever tried implementing a simple unstructured BASIC dialect in Python? I'm getting interested in language implementation, and looking at a reasonably simple example like that could be...
6
by: Chris Dunaway | last post by:
I have a simple interface with two methods, MethodA, and MethodB. I also have a class with implements the interface. The class provides a full implementation for MethodA but for special reasons,...
3
by: lodge.stuart | last post by:
Hi I've written a small AJAX-style UI - although it's not strictly AJAX as it uses HTML fragments rather than XML This UI works fine for me and the majority of users. However, a few users...
6
by: Boaz Ben-Porat | last post by:
I heard somewhere that the DataGrid class is implemented as a XML graph in memory. Is this true ? TIA Boaz Ben-Porat DataPharm a/s Denmark
6
by: Mountain Bikn' Guy | last post by:
When one gets a row from a database (ie, a DataTable), the row contains a typed value in each column. How is this typically implemented behind scenes. I want to build this functionality myself. The...
4
by: m96 | last post by:
hi, i'm trying to make a query to a ldap server (version v2 or v3 doen't matter) with c#. the query works just fine but the problem is that i can't read the custom attributes/fields, since .net...
12
by: cody | last post by:
Why can I overload operator== and operator!= separately having different implementations and additionally I can override equals() also having a different implementation. Why not forbid...
22
by: sujilc | last post by:
This question seems to be silly. Can ony one figure out the default functions implemented by compiler when we decalre a class like class A { } According to me this declaration will define...
4
by: dba123 | last post by:
I took the code from here and tweaked it to our needs. One of the methods is erroring out throwing an exception and I'm not sure why: object IConfigurationSectionHandler.Create(object parent,...
12
by: Author | last post by:
I know the basic differences between a struct and a class, but apparently not good enough to know why some types/concepts are implemented as struct whereas most types are implemented as class. For...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.