2
#include<stdio.h>

int main(void)
{
    /* in this amessage is an array big enough to
     * hold  the sequence of character and '\0' */
    char amessage[] = "now is the time";

    /* in this case a character pointer is
     * pointing to a string constant */
    char *pmessage = "now is the time";

    return 0;
}

I am confused how is the memory allocated in the second case?? Is that copy of memory private....I mean is it safe to write something as 2nd stmt and be sure that anything else will not overwrite the same memory??

4
  • No, you can't be sure. You should take care for that yourself through your programming Commented Oct 7, 2011 at 17:32
  • 1
    @Mr.DDD: Are you sure? I think most compilers will load that data into a different segment of the process' memory, not the heap. Commented Oct 7, 2011 at 17:35
  • @Mr.DDD - there won't be any loading (copying). it most likely will point to some place in data section of executable Commented Oct 7, 2011 at 17:38
  • using microsoft c with default optimization options, the string data itself is allocated in the static data segment. in both cases there is nothing allocated on the stack. the pointers to the data segment are allocated in registers. either string is mutable. Commented Oct 7, 2011 at 17:46

3 Answers 3

6
char *pmessage = "now is the time";

Creates a string literal, the memory for the string is allocated somewhere in a read only location, it is implementation dependent detail of compilers to be specific.

Modifying it will lead to an Undefined Behavior. So If you are using string literal it is your responsibility to ensure that you do not try to write to this read only memory.

Sign up to request clarification or add additional context in comments.

3 Comments

The space for string literals need not come from read only locations: there are implementations of C on systems for which it makes no sense speaking of read only locations.
@pmg: The answer says: it is implementation dependent detail of compilers to be specific.
Right ... my point is that it need not be a read only location. The implementation can 'choose' good old read/write memory for string literals.
2

The string literals can be distinct or not: it's the compiler choice (the C99 Standard says it is unspecified).

Suppose, for a minute, that changing the string literals themselves wasn't UB.

int main(void) {
    char *p = "now is the time";
    /* compiler makes sure there is a place with "now is the time"
    ** and makes p point there */
    char *q = "time";
    /* possibly, the compiler reuses the time from
    ** the previous string literal */

    q[0] = 'd'; /* technically UB: don't do this */
    puts(p);    /* possibly writes "now is the dime" */
}

As tou your specific question: as long as you don't invoke UB, it's safe. The memory allocated by the compiler takes into account all other uses of all other objects.

Comments

-4

It's safe. They'll be allocated on the stack. The only thing different between amessage and pmessage is that pmessage is read only.

1 Comment

Compilers should actually warn or reject this. String literals are read-only, so you shouldn't assign them to a non-const pointer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.