string literals in overlapping areas of memory


Quote: draft explicitly allows compilers to store string literals in
overlapping areas of memory as a space optimization.
Ref: http://www.gotw.ca/gotw/011.htm


Can some one please help me to understand how compiler would store
string literals in overlapping area of memory
Has it something to do with substrings
Is it same as 14882-2003 1.13.4.2 (or draft refers to C++ 0x\t)


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

, > Can some one please help me to understand how compiler would store
> string literals in overlapping area of memory

If you had

#include

int main
{
std::cout << " World" << std::endl;
std::cout << "ello World" << std::endl;
std::cout << "llo World" << std::endl;
std::cout << "o World" << std::endl;
std::cout << "World" << std::endl;

return 0;
}

for all those double quoted strings, the compiler could just store 1 copy of
" World" in the executable and just pass the offset of where each right
subset portion starts. It saves executable space.

Stephen Howe



--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Posted On: Monday 5th of November 2012 12:56:48 AM Total Views:  151
View Complete with Replies

RELATED TOPICS OF C Language PROGRAMMING LANGUAGE




Declaring const char strings in a class?

I'm having trouble with the proper declaration of character arrays in a class and was hoping someone here could help me. I have tried various confuguratinos of the following: class.h class SomeClass { public: SomeClass(); // constructor private: static const char *charArray[]; }; class.cpp SomeClass::SomeClass() { const char *charArray[4] = { "string1", "string2", "string3", "string4"}; } I've tried individual assignments also, but this also gave me compiler errors - would someone tell me how to make this sort of assignment Much
VIEWS ON THIS POST

95

Posted on:

Monday 5th November 2012
View Replies!

How do you compare char strings in an elegant way?

, I have this very simple task, but I cant get it to work: there is this std::map my_map; and I would like to compare the requested strings using strcmp. It works like this : bool comp(const char* s1, const char* s2) { return _stricmp(s1, s2) < 0; } my_map(std:tr_fun(comp)); A neat solution would be to model the function with STL functor objects in a combination similiar to bind2nd(less(strcmp), 0); which is total garbage, but maybe you get the idea. thank you -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
VIEWS ON THIS POST

105

Posted on:

Monday 5th November 2012
View Replies!

standards question about multi line string constants

I've seen a few obscure syntaxes for specifying string constants, and I want to ask which ones are in the c++ standard and which are not. 1. Concatenating string literas like so: char* str = "first" "second" "third" "forth" "etc"; 2. Multiline strings like so char* str = "a multi line string without a back slash before the newline"; which on the compilers I've seen has the behavior of inserting a newline between "without" and "a" in str as if \n had been typed there. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
VIEWS ON THIS POST

159

Posted on:

Monday 5th November 2012
View Replies!

How can I write a class with "string or char*" member variable to a file...?

Hi to every body... I'm a novice C++ programmer & I've a question, How can I write a class as follow to a file... Class A { public: // Accessors Methods...
VIEWS ON THIS POST

120

Posted on:

Monday 5th November 2012
View Replies!

"ispunct()" not working on std::string

/* C++ Primer 4/e * section 3.2 - String Standard Library * exercise 3.10 * STATEMENT * write a programme to strip the punctation from the string. */ #include #include int main() { std::cout > a_word; for(std::string::size_type ix = 0; ix != a_word.size(); ++ix) { if(ispunct(a_word[ix]) == 0) stripped_word[ix] = a_word[ix]; } std::cout
VIEWS ON THIS POST

91

Posted on:

Monday 5th November 2012
View Replies!

Exception safety: how to assign std::string when strong guarantee is needed?

* Niels Dekker - no return address: > How would you assign one std::string to another, when you need to have > the "strong guarantee", with respect to exception safety > > It seems to me that the following five attempts only offer the basic > guarantee: > > void Foo( const std::string & string1, std::string & string2 ) { > > // Attempt 1. operator= > string2 = string1; > > // Attempt 2. Copy-and-swap. > std::string(string1).swap(string2); > > // Attempt 3. Copy-and-swap (non-member). > std::string temp(string1); > swap(string2, temp); > > // Attempt 4. assign. > string2.assign(string1); > > // Attempt 5. assign iterator-range. > string2.assign(string1.begin(), string1.end()); > } > > Your help is appreciated! This one's very easy. You install your own new-handler, which calls std::terminate. Then out-of-memory condition won't cause an exception, and so you're guaranteed that there's no exception from that. Otherwise, any attempt to allocate memory may cause an exception. -- 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 [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] ,
VIEWS ON THIS POST

143

Posted on:

Monday 5th November 2012
View Replies!

Converting date string to a number

How can I convert a date string to a number (e.g. a time_t value or a tm struct) I know about the strptime function, but then I have to know the format string. And that is a problem. I'm trying to autoformat the contents of text entries in a GUI. For numbers, I'm converting the text representation to the appropriate type (using atoi, atof, ...) and converting the result back to text with the correct format (using sprintf). But this does not work for date (or time) strings. For instance, if the deed date format is "dd-mm-yy", but the user enters "dd-mm-yyyy", I can't convert the string to a valid date, because I don't know its format.
VIEWS ON THIS POST

103

Posted on:

Monday 5th November 2012
View Replies!

parsing string with std::stringstream

Does *ANYONE* in here know how I may parse the various date/time 'elements' from a string. The input string has the ff format: 'YYYY-MM-DD HH:MM:SS AM'
VIEWS ON THIS POST

100

Posted on:

Monday 5th November 2012
View Replies!

string class problem

can anyone tell me a solution: i cannot use the features in standard c++ string classgh i included the string.h file but still its not working.
VIEWS ON THIS POST

95

Posted on:

Monday 5th November 2012
View Replies!

std::string and case insensitive comparison

what is the most efficient way of doing a case insensitive comparison I am trying to write a universal String class and I am stuck with the case insensitive part : TCHAR is a char in MultiByte String env (MBCS) and wchar_t if UNICODE #if defined(WIN32) || defined(UNDER_CE) typedef std::basic_string tstring; #else #endif #endif class String : public Object { private: tstring m_str; public: String(){} String(LPCTSTR lpsz) { m_str = lpsz; } String(tstring str) { m_str = str; } // Comparison int Compare( LPCTSTR psz ) const { return m_str.compare(psz); } int CompareNoCase( LPCTSTR psz ) const { \t } // Convert the string to lowercase String& MakeLower( LPCTSTR psz ) { std::transform(m_str.begin(), m_str.end(), m_str.begin(), tolower); return *this; } }
VIEWS ON THIS POST

104

Posted on:

Monday 5th November 2012
View Replies!

Pointers + dealing with integer arrays and strings

I am a beginner and have some confusion with respect to pointers and strings. It seems that the pointers with dealing with integer arrays behave differently, as opposed to strings. Can some one explain me the difference Sample Program: int main() { int array[]={1,2,3,4,5}; char array1[]={"Name is Max"}; int *ptr; char *ptr1; ptr=array; ptr1=array1; cout
VIEWS ON THIS POST

153

Posted on:

Monday 5th November 2012
View Replies!

pinter = string.

I have a simple question, how do I move data from a string to a pointer array. I am doing some work in win32 and many of the commands need pointers but poitner are cumbersome and I try to avoid them. Basically I want to do: char * prt; std::string str; str = ""; ptr = str;
VIEWS ON THIS POST

90

Posted on:

Monday 5th November 2012
View Replies!

seeking within ostringstream's

What is a stringsteam supposed to do when you seek past the end of existing buffer. I can seek past the end of a file stream (my implementation fills the space will nulls but I cannot find if this is guaranteed either) Below code fails for a stringstream. Any good ways of dealing with this. Reason for doing this is that I am modify old C code for screen output that positions fields based on a row. I wanted to use a stringstream and just seek to the correct place. I have tried basically filling the stream with X spaces first. But then I am never sure there is enough space. Any help appreciated. Adrian #include #include #include int main(int argc, char *argv[]) { std:fstream out("test.txt"); out.seekp(500); out
VIEWS ON THIS POST

119

Posted on:

Monday 5th November 2012
View Replies!

error C2679: binary '

I have written this code, and at the end, I am trying to write a vector of strings into a text file. However, my program is nor compiling, and it gives me the following error when I try to write to the file: error C2679: binary '
VIEWS ON THIS POST

266

Posted on:

Monday 5th November 2012
View Replies!

ostringstream and sgetn() weirdness

hi, i have come aross a strange problem with a bit of code that uses a ostringstream to build up a string and i extract the string into a user buf via the sgetn() call instead of via the str() method. however, it appears that when the contents extracted via sgetn() are invalid for the first extract. below is a test prog that demonstrates the issue. we are using Forte 7.0 on solaris 5.8; i dont have access to gcc (or any other c++ compiler). can someone tell me where i'm going wrong thanks ray #include #include #include #include #include using namespace std; int main(int argc, char* argv[]) { try { ostringstream dump; size_t sz = 0; if (argc == 1) { dump sgetn(tmp, 4); tmp[4] = NULL; dump.seekp(0); // this comes out as empty string!!! cout
VIEWS ON THIS POST

203

Posted on:

Monday 5th November 2012
View Replies!

Reliably cross-platform determining read-only string

I was wondering, is it possible to determine whether a string can be modified (const char *) by the application or whether it's located in what's commonly .rodata
VIEWS ON THIS POST

163

Posted on:

Monday 5th November 2012
View Replies!

string formatting in cpp

Hi - I have the need to format my input string by putting single quotes around them. Right now I am converting the string object to a c style string so that I can use sprintf. see code snippet below: 1 string insert_str; 2 const char* t = table.c_str(); 3 const char* i = input.c_str(); 4 insert_str = printf("insert into %s VALUES( '%s'", t, i); 5 cout
VIEWS ON THIS POST

220

Posted on:

Monday 5th November 2012
View Replies!

string class problem

can anyone tell me a solution: i cannot use the features in standard c++ string classgh i included the string.h file but still its not working.
VIEWS ON THIS POST

147

Posted on:

Monday 5th November 2012
View Replies!

error: 'string' does not name a type

What would cause this error Here is the code: #include class name { public: // default constructor void NAME() { FirstName = "John"; MiddleName = "H."; LastName = "Doe"; } // non-default constructor string NAME(string FN, string MN, string LN) { FirstName=FN; MiddleName=MN; LastName=LN; } // returns First Name, Middle Name, and Last Name in order void getFirstLast() { } // prints the first, middle, and last name void print() { } private: string FN, MN, LN; }
VIEWS ON THIS POST

199

Posted on:

Monday 5th November 2012
View Replies!

string and class error

i have created a class which contains all the information needed for a program based on accounts, this is shown below. When compiled the string "word" (in function writetofile) which is initialised in the constructor is apparently "undeclared", yet it has been declared in the constructor. i am new to c++ class programming, do i need to also declare it, as well as initialise and if not, what is the problem and how can it be solved thanks wilson #include #include #include #include #include using namespace std; class Account { friend void new_account(); public: Account(){string word = "hello";}//can eventually be user inputted void returnbalance() { std::cout
VIEWS ON THIS POST

142

Posted on:

Monday 5th November 2012
View Replies!