Link to home
Start Free TrialLog in
Avatar of srik27
srik27

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of Axter
Axter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Please disregard above post.
It's suppose to be for another question
srik27,
> >char* teststr = "something";
> >and sortstr(teststr);   // it is crashing.

You should not try to modify the contents of a string literal.
Avatar of srik27
srik27

ASKER

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.
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";
Avatar of srik27

ASKER

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.
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.
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.
Avatar of srik27

ASKER

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.
srik27,

FYI:
You are able to split points
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
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 ;)