Generic char* question
if i have a char* such as str2 and str3 is it neccassary to create a memory with the new operator or can i do it just by assigning string constants to them?
int l;
char* str2;
char* str3;
str3="test this one";
l=strlen(str3);
str2=new char[l+1];
strcpy(str2,str3);
or
str3=str2;
is there an advantage or disadvantage to doing it either way?
I am using MSVC++ 6.0 if that makes a difference.
August 23, 2000 06:40 PM
The difference between strcpy(str2, str3) and str2=str3 is that in the former case str3 and str2 point to *different* strings that just happen to have the same contents. In the latter case str3 and str2 point to the *same* string.
Consider what happens in each case if you then do:
str2[0] = ''x'';
In the strcpy case str2 is now "xest this one" and str3 is unchanged. In the str2=str3 case you''ll either crash because str3 (and str2) is read only or both str2 and str3 will see the change.
-Mike
Consider what happens in each case if you then do:
str2[0] = ''x'';
In the strcpy case str2 is now "xest this one" and str3 is unchanged. In the str2=str3 case you''ll either crash because str3 (and str2) is read only or both str2 and str3 will see the change.
-Mike
Actually i tested it out on my compiler for the str2=str3 case and both strings kept different addresses...so i could still modify them independently...my real question was about using the new operator to create a memory space for a char* as opposed to simply assigning str2="stuff";
i know that str2=str3 keeping seperate addresses doesnt make since due to the fact they are both pointers but...the only thing i can think of is its something specific to this compiler.
i know that str2=str3 keeping seperate addresses doesnt make since due to the fact they are both pointers but...the only thing i can think of is its something specific to this compiler.
Weirdly, with some implementations (and I think MSVC++6) when you declare the same text withing a char*, you are using the same address. For instance:
char* str1 = "Hello, its a nice day";
char* str2 = "its a nice day";
"its a nice day" is the same address in str1 and str2, but "Hello," stays individual. Now say you changed str1 to: "Hi, what a great day!" then you can see how this would affect str2. Don''t ever change a char*! If you wanted to change a char*, create some memory in the free store with the new keyword.
char* str1 = "Hello, its a nice day";
char* str2 = "its a nice day";
"its a nice day" is the same address in str1 and str2, but "Hello," stays individual. Now say you changed str1 to: "Hi, what a great day!" then you can see how this would affect str2. Don''t ever change a char*! If you wanted to change a char*, create some memory in the free store with the new keyword.
-=[ Lucas ]=-
The ''merging'' of two identical strings is not implementation dependent, it''s a compiler option. I''m sure you can change it under Borland C++ (''Merge duplicate strings''), but I haven''t seen it with VC++. I think it''s done when using the "optimize for minimal size" option.
Dormeur
Dormeur
Wout "Dormeur" NeirynckThe Delta Quadrant Development Page
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement