Why doesnt the following program work....


Hi Guys,

I have the following question....
when i call the below function by saying(from void main)

char* teststr = "something";
and sortstr(teststr);   // it is crashing.

However when i do....
char* strstr = new char[20]; // or strlen of teststr
strcpy(strstr,teststr);
and then do sortstr(strstr); // this seems to work fine.

Dont worry about the fact that i have used bubble sort etc..when other faster algorithms are available...i just want to know y the first case is not working.





void sortstr(char* str)
{
int i=0;
int j=0;
   if(str==0)
         return;

for(i=0;i<len1;i++)
       {
        for(j=i+1;j<len1-1;j++)
        {
             if( *(str+i) > *(str+j) )
             {
                   char tmp;
                   tmp = *(str+i);
                   *(str+i) = *(str+j);  // it is crashing here...when not allocated memory thru new
                   *(str+j) = tmp;

             }
        }
       }
   }



thanks a lot,
S.
srik27Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AxterCommented:
The above also will not compile on VC++ 6.0.

If portability is not an issue, and you never plan on using the code no an older version of the compiler, then this shouldn't be important.

However, if it's a possibility, then you should avoid this type of code.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
synqueCommented:
You cannot modify a string literal. In the first case you're assing the pointer of a string literal to the variable teststr. This string literal is read only.
0
AxterCommented:
Please disregard above post.
It's suppose to be for another question
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

AxterCommented:
srik27,
> >char* teststr = "something";
> >and sortstr(teststr);   // it is crashing.

You should not try to modify the contents of a string literal.
0
srik27Author Commented:
Hi Synque,

Thanks a lot.....i got confused...i am clear now...thanks for your answer...so in other words...if i ever want to change something...then i should not have it allocated from the literal pool which is read only ....right?

And thank you for the lightning fast replies.

Thanks,
S.
0
AxterCommented:
synque,
> >You cannot modify a string literal. In the first case you're assing
> >the pointer of a string literal to the variable teststr. This string
> >literal is read only.

Actually, IAW C++ standard that is not completely correct.

The C++ standard specifies the out come of modifying a string literal and undefined.

That means it's up to the compiler as to what happens when you modify a string literal.

For example, in VC++ 5.0, you can modify a string literal, and it will not crash.

In VC++ 6.0, you can not modify a string literal, and if you try it will crash or raise an exception.

You should avoid undefined behavior in your code.

So you should not try to modify the contents of a string literal.

Here's an easy fix.
char teststr[] = "something";
0
srik27Author Commented:
Hi Axter,

Got your answer....however i am sorry as your answer is the same as synque's and he came first...i am not sure if i shud split the points or not...
Will give the points soon after i make a decision.

Thanks,
S.
0
AxterCommented:
srik27,
> >allocated from the literal pool which is read only

The C++ standard doesn't say whether it's read-only.  That depends on your compiler.

You can do the following:
char teststr[99] = "something";

Now you're variable is not pointing to a string literal, but it contains the desired values.
0
AxterCommented:
srik27,
> >Got your answer....however i am sorry as your answer is the same as
> >synque's and he came first...i am not sure if i shud split the points or not...

No, my answer is not the same.

Please read it again.

He's telling you that you can not modify a string literal.

Which is not correct IAW with the C++ standard.

What I'm saying is that you should not modify a string literal, because it results in undefined behavior.
0
srik27Author Commented:
Hi Guys,
Based on the effort....i might as well split the points...but i have one doubt.....i dont understand this....you say that a string literal should not be modified....
i assumed that a string literal is "something"....

so how would char test[]="something";
solve the problem....as we are still modifying it...arent we?
ooooo.....i think i get it....since test is not a pointer....in this case we will not modify "something"

Is this correct..

Thanks axter...you helped me clear something.

Thanks,
S.
0
AxterCommented:
srik27,

FYI:
You are able to split points
0
AxterCommented:
srik27,
> >so how would char test[]="something";
> >solve the problem....as we are still modifying it...arent we?

Because it's allocating member for test, and then initailizing the value to the string literal.

char *test="something"; //This points to a string literal
char test[]="something"; //This makes a copy of the string literal to memory that is OK to modify
0
synqueCommented:
Axter,
>Actually, IAW C++ standard that is not completely correct.
>The C++ standard specifies the out come of modifying a string literal and undefined.
*snip*
Alright, forgive me the inaccuracy. Personally I like to think of string literals as "array of const char". But I can also see that this wont "work" for historical reasons.

Sorry again... and thanks for the points ;)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.