Header Files

In C++, strings are not a built-in data type, but rather a Standard Library facility.

#include <string>
//#include <string.h>   //(.h is used for C not C++)
#include <cstring>
using namespace std;    //Or use std::string;
#include <sstream>    //Lets you create strings using << for instance

Memory Allocation For Strings

Its handled automatically – you don’t have to specify a size when creating a string.  The string class functions simply compute the size every time they need to allocate memory, at a runtime cost (by, for example, using strlen when needed).  This is roughly what it does:

Compute the size of the input
Compute the new size the string should have after reading
Allocate memory (depending on new size), if required
Copy data in new memory
Return old memory to the heap and now use the new memory instead

So very handy, BUT, at the expense of speed of course.  If you wanted to work on a string as fast as possible creating a char array with as big a size as you will ever need and the working on the individual char’s would be fastest.

Here’s a few useful examples of how things work:

Constant strings are always there, no need to worry about them being destroyed or destroying them:
const char* func1() {
   const char* s = "Hello World";
   return s;
}
const char*  s1 = func1();
delete s1; //?

You can’t delete s1, as the string it points to does not live on the heap.  

You can pass a ram memory string back from a function call:
string func2() {
   string s = "Hello world";
   return s;
}
string s2 = func2();

This is fine. func2’s s goes out of scope and cleans up. s2 will duplicate the string from s, and also clean itself up at the end of function it is used in.

You can’t return a pointer to a string that will get destroyed:
const char* func3() {
   string s = "this is a literal string";
   return s.c_str();
}
const char*  s3 = func3();
delete s3;

func3 returns a pointer to a string that will be freed when the called function exits. You will get an exception upon execution of delete s3.

Deleting Strings

You don’t have to. When the string goes out of scope, it’s destructor will be called automatically and the memory will be freed.

If you want to clear the string right now (without waiting until it goes out of scope) just use:

mystringname.clear().

string or wstring?

std::string is a basic_string templated on a char, and std::wstring on a wchar_t.

char holds a character, usually a 1-byte character.

wchar_t holds a wide character, but its different across systems:

On Linux, a wchar_t is 4-bytes.  Holds all the ASCII character set including special characters.

On Windows it’s 2-bytes.  Only special characters are available for the current locale of the Windows user.

Unicode

Sort of – the problem is that wchar_t is not directly tied to unicode. There’s a good explanation in an answer here.

 

USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

Your email address will not be published. Required fields are marked *