?
Solved

Why doesnt the following program work....

Posted on 2005-02-25
13
Medium Priority
?
381 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

777 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