Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > whether this is valid

Reply
Thread Tools

whether this is valid

 
 
madhur
Guest
Posts: n/a
 
      06-26-2004
Hello

char *a="str";
strcpy(a,"abc");

I have learnt that since "a" is a string literal, it might be allocated on
read only memory on some implementations.
Is it correct to modify a string literal using this way.

Madhur Ahuja
India



 
Reply With Quote
 
 
 
 
Joona I Palaste
Guest
Posts: n/a
 
      06-26-2004
madhur <(E-Mail Removed)> scribbled the following:
> Hello


> char *a="str";
> strcpy(a,"abc");


> I have learnt that since "a" is a string literal, it might be allocated on
> read only memory on some implementations.
> Is it correct to modify a string literal using this way.


Not, it's not correct. It might work or "work" on some implementations
but that's due to either special allowances or sheer dumb luck. The
above code causes undefined behaviour and thus should never be relied
on.

--
/-- Joona Palaste ((E-Mail Removed)) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"As we all know, the hardware for the PC is great, but the software sucks."
- Petro Tyschtschenko
 
Reply With Quote
 
 
 
 
Christian Bau
Guest
Posts: n/a
 
      06-26-2004
In article <(E-Mail Removed)>, "madhur" <(E-Mail Removed)>
wrote:

> Hello
>
> char *a="str";
> strcpy(a,"abc");
>
> I have learnt that since "a" is a string literal, it might be allocated on
> read only memory on some implementations.
> Is it correct to modify a string literal using this way.


If anyone tells you that it is, shoot him. If you are ever caught doing
it on purpose in a professional job, you will be instantly fired.

Now please tell us: If it were legal, why on earth would you do it?
 
Reply With Quote
 
Malcolm
Guest
Posts: n/a
 
      06-26-2004

"Christian Bau" <(E-Mail Removed)> wrote in message
> > char *a="str";
> > strcpy(a,"abc");
> >

> Now please tell us: If it were legal, why on earth would you do > it?
>

char *filename = "foo";

/* lots of code using "filename" */

Change to specs

char *filename = "foo";

if( filenotavailable() )
strcpy(filename, "bar");

/* lots of code unmodified from previous version */


 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      06-26-2004
In article <cbku61$3h2$(E-Mail Removed)>,
Malcolm <(E-Mail Removed)> wrote:
>if( filenotavailable() )
> strcpy(filename, "bar");


Why not

filename = "bar";

?

-- Richard
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      06-26-2004
madhur wrote:
>
> char *a="str";
> strcpy(a,"abc");
>
> I have learnt that since "a" is a string literal, it might be
> allocated on read only memory on some implementations.
> Is it correct to modify a string literal using this way.


No. If you get in the habit of making such declarations as below,
the compiler will usually warn you about misuse:

const char *a = "str";

--
Chuck F ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

 
Reply With Quote
 
tweak
Guest
Posts: n/a
 
      06-27-2004
CBFalconer wrote:
> madhur wrote:
>
>>char *a="str";
>>strcpy(a,"abc");
>>
>>I have learnt that since "a" is a string literal, it might be
>>allocated on read only memory on some implementations.
>>Is it correct to modify a string literal using this way.

>
>
> No. If you get in the habit of making such declarations as below,
> the compiler will usually warn you about misuse:
>
> const char *a = "str";
>

And that's because modifying the contents of a string pointed to
results in undefined behavior:

" ...the pointer may subsequently be modified to point elsewhere,
but the result is undefined if you try to modify the string contents."

K&R2 pg. 104

This goes back to the differences between arrays and pointers.

Brian

P.S. I do not think this has changed since K&R2. Please correct me if
I'm wrong.
 
Reply With Quote
 
Stephen Sprunk
Guest
Posts: n/a
 
      06-27-2004
"Malcolm" <(E-Mail Removed)> wrote in message
news:cbku61$3h2$(E-Mail Removed)...
> char *filename = "foo";
>
> if( filenotavailable() )
> strcpy(filename, "bar");


Why not make filename a char[]?

S

--
Stephen Sprunk "Those people who think they know everything
CCIE #3723 are a great annoyance to those of us who do."
K5SSS --Isaac Asimov

 
Reply With Quote
 
madhur
Guest
Posts: n/a
 
      06-27-2004
Hello
Thanks, for all your answers.
Now, If I do the following:

char *a="str";
a=malloc(100); /*str is lost*/
strcpy(a,"abc");
strcpy(a,"def");

Is this OK. Memory allocated using memory should not be Read Only in this
case.

"madhur" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello
>
> char *a="str";
> strcpy(a,"abc");
>
> I have learnt that since "a" is a string literal, it might be allocated on
> read only memory on some implementations.
> Is it correct to modify a string literal using this way.
>
> Madhur Ahuja
> India
>
>
>



 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      06-27-2004
madhur wrote:
>
> Thanks, for all your answers.
> Now, If I do the following:
>
> char *a="str";
> a=malloc(100); /*str is lost*/
> strcpy(a,"abc");
> strcpy(a,"def");
>
> Is this OK. Memory allocated using memory should not be Read Only
> in this case.


Please do not top-post. Your answer belongs after the material
(suitably snipped) to which you are replying.

That is perfectly legitimate. a can hold a pointer to char, and
that is what you have given it via the malloc (but you failed to
check for NULL). There is no memory leak, even though the "str"
may become inaccessible, or may not, depending on the compiler.

char *a = "str";
char *b = "str";

a may well end up the same as b. No guarantees.

--
Some useful references:
<http://www.ungerhu.com/jxh/clc.welcome.txt>
<http://www.eskimo.com/~scs/C-faq/top.html>
<http://benpfaff.org/writings/clc/off-topic.html>
<http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/> (C99)


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to check whether malloc has allocated memory properly in case ifmalloc(0) can return valid pointer Shivanand Kadwadkar C Programming 83 01-08-2011 08:18 AM
whether Vista Exam valid after windows 7 release ??? DineshMoorthy Microsoft Certification 4 01-13-2009 04:29 AM
choose whether .jsf produces valid HTML or XHTML? Joerg Hoehle Java 0 01-24-2007 05:55 PM
How to check whether HttpSession is still valid? juppie Java 5 08-30-2006 11:42 AM
whether MCAD is valid for becoming a MCP or not? priya12 MCAD 1 08-28-2005 12:18 PM



Advertisments