Wednesday, 11 March 2015

Strings in C++, what is the difference between C++ Strings and the C character array

Strings in C++, What is the difference between the C++ Strings and the C character array:

The biggest drawback in   C is character array. The difference between the strings and array is one created in stack and another is created in heap.  In addition, the known thing that in some situations we have to pass the char* and some other situations we must copy the whole array. The memory allocated for the Strings in the heap location.
So the memory is allocated dynamically.

As a programmer, we have keep concentrating on the string manipulations, because these are the sources of the bugs.
Coming to the class, which contains character pointer may leads to crash the application by the shallow copying and the deep copying of the member data.

To overcome these problems string library is introduced to C++ programmers. The String library class can resolve the problems occurred while using the char* and character arrays. The string function keeps the track of the memory while using copy constructors.

In this post we first learn the introduction to the strings, and coming posts we will learn the operations on the strings.


What is in a string?

C String is the binary array that is terminated with the zero, which is also called as null terminated.
The difference between the C char pointer and the C++ strings are shown below:

Difference between the C++ Strings and the C character array:

Diff1: First, C++ string objects associate the array of characters, which constitute the string with methods useful for managing and operating on it.

A string also contains certain “maintenance” information about the size and storage location of its data.
These C++ string objects knows its starting location in memory and length and also the length in characters to which it can grow before the string object must resize its internal data buffer.

Diff2: The C++ strings do not include a null terminator, nor do the C++ string handling member functions rely on the existence of a null terminator to perform their jobs.

Advantages of the Strings over C characters:

Strings generally reduces the errors that are caused by C programming. There are 3 major problems gets reduced using C++ Strings.

1. overwriting array bounds.
2. Trying to access arrays through uninitialized or incorrectly valued pointers.
3. and leaving pointers “dangling” after an array ceases to occupy the storage that was once allocated to it.

The memory layout architecture is intended to be flexible enough to allow differing implementations by compiler vendors, yet guarantee predictable behavior for users.

In particular, the exact conditions under which storage is allocated to hold data for a string object are not defined. String allocation rules were formulated to allow but not require a reference counted implementation, but whether or not the implementation uses reference counting, the semantics must be the same.

But to differentiate the C char array and C++ Strings, Strings may not occupy the unique physical region of the memory but C char array occupies the unique physical region of the memory.

The below example shows the how the reference counting is used to avoid storing duplicate copies of data.

#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
int _tmain (int argc, _TCHAR* argv[])
{
string Str1 ("test strings");

string::iterator MyIterator = Str1.begin();  // This is the iterator indicate the first element
string Str2 = Str1; 
*MyIterator = '0';
cout << "Str1 = " << Str1 << endl;
cout << "Str2 = " << Str2 << endl;
getchar ();
return 0;
}


No comments: