What happens if you write following code?, C/C++ Programming

What happens if you write following code?

string& foo()                                                                                                        


return "Hello World";


cout << foo() << endl;

A: 1. Will give an error as Hello World is created as unnamed character pointer to const. this is being assigned to non-const reference that is not allowed. Could not alter '"Hello World"' to 'std::string&'

2. const string& foo1()


return "Hello World";


It will give a warning .As you are returning a reference to temporary, which will die instantly when the expression is completed?

classsize.C:7: warning: returning reference into temporary output : Aborted. Segment fault.

3. Char *foo1()


return "Hello World";


Returning the address of character literal that is created on the static memory.

In C++, the compiler let the use of string literals to initialize character arrays. A string literal contain zero or more characters surrounded through double quotation marks ("). A string represents a sequence of characters that, taken mutually, form a null-terminated string. The compiler developed static storage space for the string, null-terminates it, & puts the address of this space into the char* variable. The sort of a literal string is an array of const chars.

char* szMyString = "Hello world.";

szMyString[3] = 'q'; // undefined, altering static buffer!!!


In the following instance, the compiler puts a null-character automatically at the end of the literal string of characters "Hello world". Then it creates a storage space for the resulting string - it is an array of const chars. Then it puts the initial address of this array into the szMyString variable. We will attempt to change this string (wherever it is stored) by accessing it through an index into szMyString. It is a Bad Thing; the standard does not say where the compiler puts literal strings. They can go anywhere, perhaps in some place in memory that you shouldn't be altering.


Posted Date: 3/20/2013 3:37:18 AM | Location : United States

Related Discussions:- What happens if you write following code?, Assignment Help, Ask Question on What happens if you write following code?, Get Answer, Expert's Help, What happens if you write following code? Discussions

Write discussion on What happens if you write following code?
Your posts are moderated
Related Questions
Can I drop the [] while deleteing array of some built-in type (char, int, etc)? A: No. you can't Sometimes programmers think that the [] in the delete[] p only present so the

Write a program that writes your name on the monitor ten times. Write this program three times, once with each looping method.

Variables Information stored in a variable can alter in the course of the program. The type used in the definition explains the kind of information the symbol can store. Variab

In the final project assignment you are asked to develop an OOP C++ class hierarchy for derivative pricing, using the binomial tree and Black-Scholes option pricing methods. You wi

solve for radius(R) of a circle of its area is to be inputted by a user. also display compute the circle diameter hint: area= pir21 pi=2.1416

It is standard on functions to provide some indication that the function has succeeded in its operation. Consider the Scanf function:     scanf("%d",&number1);   If the functio

Write a program that predicts users’ age (0-128 years old) with at most 7 questions. The game starts with asking the user whether he/she is younger or older than G (an initial gues

Classify computer system according to capacity. How they are different from computers according to the classification of technology. Provide comparative study also.

Problem #1: plugging the memory leak. When you employ the "normal" new operator, for example Foo* p = new Foo(), the compiler generates some special code to manage the case while t

A skilled programmer is required to build a standalone module to extract, parse and store in database tables, word count data from web pages and RSS feeds. The module will take