C++

Objectives : Passing string data by value and by reference to a pointer.

  • File Name : string_new_delete1.cpp
  • Passing reference of a string to a pointer: .  
    syntax:  strPtr1 = &str1;//allowed, passing reference of a string
  • Passing string's value to a pointer :
    • We can't use assignment operator(=), between a pointer and a string-object, and we have to pass the address of the string-object (passing reference of the object). As an alternative, we may use new operator, as indicated at the next step.

       
    • It appears that code like this, "strPtr1=new string(str1); " would simply pass the value of "str1" as a soft-copy, and any change at the pointer's value would not affect the object "str1".
    • It works well, when you don't want to mutate/edit the input string, and edit a deep copy with a pointer to pointer.
    • The operators new and delete work in pair, you can't use delete if the object is not allocated with new.
  • Code: string_new_delete1.txt
 

Step: 1 Create a source file.

Step: 2 Edit and save Source file:

Step: 3 Runtime Views:

a) Imposing size constraints: Testing an input which was more than 15 characters long. As defined in the “if statement block”, the application was terminated early because the string was longer than 15 characters.

b) Passing string value to a pointer: Note that original string “Hello World” did not mutate, when passed to a pointer with new operator. It is apparent that one pointer passes the data to another pointer, as a deep copy, and change in one pointer would be reflected on the other.

The illustration below, shows that the runtime reference of "strPter1" allocated a new, location on heap, stored the value a string object, and did not hold the reference of the string-object "str1". Therefore, the value of the string-object "str1" remained unaltered.

However, the data exchange between the two pointers occurred as a deep-copy, and the reference of the object (pointer strPtr1) was passed to the other (pointer p1). As a result, any change in one of the two objects was reflected on the other.

Step: Brief discussion:

Passing value of a string : When you pass a string's reference (&str1 ), any change in the pointer, would reflect on the string. In this example we passed the value of a string to a pointer using new operator, rather than initializing a pointer with the address-of a string (&str1). This strep, disconnected the address of the two objects, and thereby avoided any mutation to the original string. To remind you the fact again that you can't use "=" operator between a pointer and a string object.

Passing value by reference: The data passed from one pointer to another by reference only. Here, a pointer dynamically allocated value of a string in the memory, from heap, and later allowed alterations with another pointer.

In C++ language, new and delete work in pairs, you can only delete or de-allocate the object which was allocated or created with new operator only. If you use delete against any other object, it will throw a run-time error.

Special notes : Please also refer to this document, where I am showed that how new operator allocates a new memory location on demand( string_new_delete2.htm)