473,410 Members | 1,946 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,410 software developers and data experts.

string advice

I have a situation in which a function expects a char const* and uses
the supplied address directly. It's a GUI element in a particular
platform but the question is generic.

I could give it the c_str() return of the string and expect that the
string isn't going to change, if it does send the c_str() again.

I could give it &str[0].

I could use a vector and do the same as the second string method.

I could also just use a raw array but I see no real advantage in that.

From a portability or design standpoint is there any /real/ difference
in the first three choices?
Feb 6 '07 #1
1 1363
* Noah Roberts:
I have a situation in which a function expects a char const* and uses
the supplied address directly. It's a GUI element in a particular
platform but the question is generic.

I could give it the c_str() return of the string and expect that the
string isn't going to change, if it does send the c_str() again.

I could give it &str[0].

I could use a vector and do the same as the second string method.

I could also just use a raw array but I see no real advantage in that.

From a portability or design standpoint is there any /real/ difference
in the first three choices?
The c_str() result is only guaranteed to be valid as long as the
std::string instance exists and no mutating operations are performed.
This may matter if the GUI element doesn't copy the string.

&str[0] is not guaranteed to be zero-terminated. Pedantically, under
the current standard it's not guranteed to be a pointer to a contiguous
buffer, either, but for all practical purposes you can ignore that.

A vector has the advantage that folks not familiar with the standard's
evolution and existing compiler practice, may not think you're doing a
dangerous thing (they'll think, rightly, that the buffer is contiguous).
It has the disadvantage that if the string comes from elsewhere in the
form of a std::string, you'll have to copy the data.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 6 '07 #2

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

Similar topics

6
by: Paul Robinson | last post by:
I am developing a website in ASP that connects to a Sybase database. However, when I try to open a connection to the database the page will not load. The script does not timeout, nor the...
37
by: Kevin C | last post by:
Quick Question: StringBuilder is obviously more efficient dealing with string concatenations than the old '+=' method... however, in dealing with relatively large string concatenations (ie,...
3
by: Ilja Booij | last post by:
Hi all, I have some trouble with the following: I'm getting a time string, in YYYY-MM-DD HH:mm:ss format, which I need to translate into a string with DD-Mon-YYYY HH:mm:ss +HHMM, where the...
7
by: zjut | last post by:
I need to implement the method : round(String name, int index) The given string maybe the every type of float type, ( the msdn given the regax is that : integral-digits]exponential-digits]) ...
12
by: arnuld | last post by:
in C++ Primer 4/3 Lippman says in chapter 3, section 3.3.1: vector<stringsvec(10); // 10 elements, each an empty string here is the the code output & output from my Debian box running "gcc...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
18
by: william | last post by:
below is a short piece of code I wrote to testify my understanding of char *, and array. #include <stdio.h> int main() { char *str=NULL; char x="today is good!"; printf("%s", str);
2
by: Carl R. Davies | last post by:
I want to find a character within a string then copy from that character to the end into another string and erase it from the original. But I'm not sure which of the many algorithms I should use,...
9
by: thijs.braem | last post by:
Hi everyone, I'm having quite some troubles trying to convert Unicode to String (for use in psycopg, which apparently doesn't know how to cope with unicode strings). The error I keep having...
0
by: Hendrik van Rooyen | last post by:
"Simon Brunning": Thanks for the link. Any advice on what to do or use as an I/O structure for dissemination? Ken Seehart:
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.