LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 05-18-2010, 06:31 AM   #1
915086731
Member
 
Registered: Apr 2010
Posts: 144
Blog Entries: 6

Rep: Reputation: 2
Smile What is the existing period of character string in c++/c


Like the following c++ program, I define a pointer of char type in the class named "test". In the function init1(), str="hello1" , what is the existing period of "hello1". Whether the string "hello1" will be invalid if the function init1() exits ? then "str" point to the invalid field in memory. When will "hello1" and "hello2" be released by OS ?

Where is "hello1" stored in memory?
Code:
#include <iostream>
using namespace std;

class test{
    char *str;

public:
    void init1(){
        str = "hello1";               //Is "hello1" stored in stack?
    }
    void init2(){
        str = "hello2";
    }
    void display(){
        cout<< str << endl;
    }
};

int main(){
    test t1;
    t1.init1();
    t1.display();
    t1.init2();
    t1.display();
    return 0;
}
Code:
cout<< "hello everyone"
When will "hello everyone" be released?

Thanks !!

Last edited by 915086731; 05-18-2010 at 06:40 AM.
 
Old 05-18-2010, 07:45 AM   #2
posixculprit
Member
 
Registered: May 2010
Posts: 136

Rep: Reputation: 42
According to the ISO/IEC 9899:1999 C standard, second edition, section 6.4.5 "String literals", subsection "Semantics", second paragraph:

Quote:
In translation phase 7, a byte or code of value zero is appended to each multibyte character sequence that results from a string literal or literals. The multibyte character sequence is then used to initialize an array of static storage duration and length just sufficient to contain the sequence. <...>

Last edited by posixculprit; 05-18-2010 at 07:46 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script: concatenate variables with period character the182guy Linux - Software 4 12-28-2009 01:41 PM
Handle $ character in a string MaryG Linux - Newbie 4 09-11-2009 06:13 AM
Test for last character in string cgraham1 Programming 14 06-11-2009 01:25 PM
Remove last character from file/string linuxchump Programming 34 06-08-2009 04:01 AM
Picking a character from a string randomly swatward Programming 2 08-14-2005 01:21 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration