Where does the string gets stored?

Discussion in 'C' started by Iqbal_h_a, Sep 10, 2006.

  1. Iqbal_h_a

    Iqbal_h_a New Member

    Joined:
    Sep 10, 2006
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Please look at the following code snippets:

    char str[]="abcd"; // Gets stored in stack frame.

    char *str="abcd"; // Where does this gets stored?


    In case of first declaration ie char str[]="abcd"; the string "abcd" gets stored in the stack frame of the invoked funciton. I wonder where does the string gets stored when we declare char *str="abcd"; (Is it in the heap?). Please reply.


    Thanks
    Iqbal
     
  2. Aztec

    Aztec New Member

    Joined:
    May 9, 2006
    Messages:
    90
    Likes Received:
    0
    Trophy Points:
    0
    It's stored in a non-modifiable memory probably ROM.
     
  3. Iqbal_h_a

    Iqbal_h_a New Member

    Joined:
    Sep 10, 2006
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    I could not get you. :confused:
     
  4. Aztec

    Aztec New Member

    Joined:
    May 9, 2006
    Messages:
    90
    Likes Received:
    0
    Trophy Points:
    0
    It simply means that you cannot modify the string since it's stored in some non-modifiable momory whereas your other one can by modified.
     
  5. Iqbal_h_a

    Iqbal_h_a New Member

    Joined:
    Sep 10, 2006
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    But I can modify it.
     
  6. Aztec

    Aztec New Member

    Joined:
    May 9, 2006
    Messages:
    90
    Likes Received:
    0
    Trophy Points:
    0
    You are not allowed to modify that. If you do that, results are unpredicatable i.e it may work or it may not work. You should not do that.
     
  7. heavensgate15

    heavensgate15 New Member

    Joined:
    Mar 18, 2008
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Hmm.. I'm not sure but I think it depends. If *str is holding an address which is located in the heap, then, it will be stored in the heap. But if *str is holding an address which is located in stack, then, the value will be stored in stack...
     
  8. imported_xpi0t0s

    imported_xpi0t0s New Member

    Joined:
    Jul 18, 2008
    Messages:
    101
    Likes Received:
    0
    Trophy Points:
    0
    It's stored in the program static data area, sometimes known as BSS. This is static data that you MUST NOT modify (all being well you will be prevented anyway). It's not stored in ROM, it's stored in RAM, but it shouldn't be changed because you will get undefined behaviour.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice