A question about string

This is a discussion on A question about string within the C Programming forums, part of the General Programming Boards category; char *p = "i come from china"; does it allocate in the stack,or static area of the memory? does the ...

  1. #1
    Registered User zouyu1983's Avatar
    Join Date
    Nov 2006
    Location
    Fuzhou University, Fujian, China
    Posts
    35

    Wink A question about string

    char *p = "i come from china";

    does it allocate in the stack,or static area of the memory?

    does the standard define the behavior that modifys the value of the string like this?

    thanks

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    A string literal like that will be stored in the program's data segment. As far as addresses go, I'd say that it's likely to be near where your static data is stored. As a string literal, it may be placed in a "read-only" area of memory and attempts to alter the data may result in a program crash or other wonderful things.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by zouyu1983
    char *p = "i come from china";

    does it allocate in the stack,or static area of the memory?

    does the standard define the behavior that modifys the value of the string like this?
    It is undefined.

    You can go here and grab n1124 and look in section 6.4.5 or Annex J.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Reusing a string pointer question
    By chiefmonkey in forum C++ Programming
    Replies: 3
    Last Post: 05-06-2009, 04:53 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. String array question
    By gogo in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2001, 05:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21