Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Why doesnt the following program work....

Posted on 2005-02-25
13
Medium Priority
?
386 Views
Last Modified: 2010-04-01

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.
0
Comment
Question by:srik27
  • 8
  • 3
  • 2
13 Comments
 
LVL 30

Accepted Solution

by:
Axter earned 100 total points
ID: 13407142
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
 
LVL 5

Assisted Solution

by:synque
synque earned 100 total points
ID: 13407147
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
 
LVL 30

Expert Comment

by:Axter
ID: 13407148
Please disregard above post.
It's suppose to be for another question
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

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

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

Author Comment

by:srik27
ID: 13407173
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
 
LVL 30

Expert Comment

by:Axter
ID: 13407185
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
 

Author Comment

by:srik27
ID: 13407200
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
 
LVL 30

Expert Comment

by:Axter
ID: 13407209
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
 
LVL 30

Expert Comment

by:Axter
ID: 13407236
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
 

Author Comment

by:srik27
ID: 13407249
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
 
LVL 30

Expert Comment

by:Axter
ID: 13407250
srik27,

FYI:
You are able to split points
0
 
LVL 30

Expert Comment

by:Axter
ID: 13407264
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
 
LVL 5

Expert Comment

by:synque
ID: 13407471
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question