whether this is valid

M

madhur

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
 
J

Joona I Palaste

madhur said:
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.
 
C

Christian Bau

"madhur" <[email protected]> said:
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?
 
M

Malcolm

Christian Bau said:
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 */
 
C

CBFalconer

madhur said:
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";
 
T

tweak

CBFalconer said:
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.
 
M

madhur

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.
 
C

CBFalconer

madhur said:
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)
 
B

Barry Schwarz

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.
It is true that if there were a constant of the form "a" in your
program, it would be a string literal. However, there is not.

There is a pointer a which is initialized to point to a string
literal. And no, it is not legal to modify a string literal in any
way.

Once you change the value in a so that it points to a modifiable area
of memory, such as an array of at least 4 char or an allocated area of
at least 4 bytes, your strcpy will work just fine.

If a will always point to unmodifiable areas of memory, you might want
to declare it const char *a so the compiler can detect if you try to
change the data it points to.


<<Remove the del for email>>
 
E

Emmanuel Delahaye

madhur said:
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.

It's not. This is why it is highly recommended that you write it

char const *a = "str";

strcpy (a, "abc");

will now produce some warning with a decent and well tuned compiler or lint.
Of course 'const' could be abused by some malicious typecast, but no code
writer fitted with a working brain would do that... I guess...
 
E

Emmanuel Delahaye

madhur said:
char *a="str";
a=malloc(100); /*str is lost*/

What is the point in defining a string literal if you don't use it at all?

What's wrong with :

char *a = malloc(100);

also, the value of a should be tested against NULL before a is used.

if (a != NULL)
{

Also, don't forget to free the allocated bloc with free() once you don't need
it any more.

free (a), a = NULL;
}
strcpy(a,"abc");
strcpy(a,"def");

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

Yes. The bloc of memory allocated by malloc() is guaranteed to belong to a
read/write memory zone.
 
D

Dan Pop

In said:
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.

Haven't you also learnt that you MUST check the FAQ before posting?

Dan
 
A

Alex Monjushko

madhur said:
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.

You answered your own question. You can't legally modify a string
literal. However, you can make your pointer point to something else.

a = "abc";

or

a = malloc(4);

if(a != NULL)
strcpy(a, "abc");

Alternatively, you can declare 'a' to be an array to start with.

char a[] = "str";

strcpy(a, "abc");
 
P

Prawit Chaivong

madhur said:
Hello

char *a="str";
strcpy(a,"abc");
"str" actually doesn't exist in your (function) stack. BUT It gonna be
located in data segment which is read only. (not 'a' itself, as you
said)
And your *a is a pointer (located in your stack that means it be able
to modified). This variable pointed to address that "str" located.

And then you tried 'strcpy(a,"abc");'. That means you tried to modify
memory that a pointed to. Do you remember I just mentioned that it is
read only data segment.

This code is not gonna work. Let change to char a[]= "str"; like the
other people in group said.

In this case compiler will allocate 4 bytes(Ignor alignment here) for
you in stack and copy "str" from data segment to that memory. And It
can be modify because it's located in stack segment.

Regards,
Prawit Chaivong.
 
T

Tuxpal

madhur said:
char *a = malloc(100);
also, the value of a should be tested against NULL before a is used.
if (a != NULL)
{
Also, don't forget to free the allocated bloc with free() once you don't need
it any more.

free (a), a = NULL;
}


Yes. The bloc of memory allocated by malloc() is guaranteed to belong to a
read/write memory zone.

a = NULL;

Is this to avoid dangling pointers ?
Is it needed always ?
 
E

Emmanuel Delahaye

a = NULL;

Is this to avoid dangling pointers ?

Yes, once a bloc has been freed, the pointers holds an invalid address. I
decide to give him a standard invalid value.
Is it needed always ?

No. But I consider it good practice (D.P. may think that's is a religious
issue, but I don't care). In my personnal rule of programming, a pointer
should have 2 kind of possible values :

NULL : meaning that the pointer is invalid.
not NULL : meaning that the pointer points to a valid memory.
 
D

Dan Pop

In said:
Yes, once a bloc has been freed, the pointers holds an invalid address. I
decide to give him a standard invalid value.
^^^^^^^^^^^^^
If it were an invalid value, it would have been as good as any other
invalid pointer value.

A null pointer value is not an invalid value (except for a few, well
defined operations: indirection and arithmetic). Most importantly,
it can be used in pointer comparisons (for equality), while a genuine
invalid pointer value cannot.

As usual, there are cases where it makes sense to do what Emmanuel does
and there are cases where it doesn't. But he is not expected to be able
to understand this.

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top