Thread: regarding variables.

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    9

    regarding variables.

    please help me regarding the below issues.

    1)
    where do gloabl variables are stored...i mean like local variables int a;
    are stored in stack.
    2)
    int a, *b;
    in the above case the variables are stored in stack
    now
    char *a = "hello world"
    in the above case pointer "a" would be stored in stack as its a local variable now where is the value string is stored.."hello world"
    also there should have been some kind of memory allocation should have been done ... but automatically its happening ..so is the complier internally calling the malloc function to allocate for the string " hello world"..in that case the string would be stored in heap...plz help me

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by kiranck007
    please help me regarding the below issues.

    1)
    where do gloabl variables are stored...i mean like local variables int a;
    are stored in stack.
    Globals are stored in the program's data segment.


    Quote Originally Posted by kiranck007
    2)
    int a, *b;
    in the above case the variables are stored in stack
    now
    char *a = "hello world"
    in the above case pointer "a" would be stored in stack as its a local variable now where is the value string is stored.."hello world"
    also there should have been some kind of memory allocation should have been done ... but automatically its happening ..so is the complier internally calling the malloc function to allocate for the string " hello world"..in that case the string would be stored in heap...plz help me
    The string "hello world" is a string literal that is stored in a segment of the programs memory like the data segment (but is marked usually as read-only) but may be called something else (anybody?). It is NOT allocated via a malloc call, the pointer "a" simply is set to the address of this memory segment where the text resides.
    "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
    Registered User
    Join Date
    Jan 2006
    Posts
    9

    thanks

    Quote Originally Posted by hk_mp5kpdw
    Globals are stored in the program's data segment.




    The string "hello world" is a string literal that is stored in a segment of the programs memory like the data segment (but is marked usually as read-only) but may be called something else (anybody?). It is NOT allocated via a malloc call, the pointer "a" simply is set to the address of this memory segment where the text resides.
    thanks a lot that was an instant reply...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. Best way to avoid using global variables
    By Canadian0469 in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2008, 12:02 PM
  3. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  4. esbo's data sharing example
    By esbo in forum C Programming
    Replies: 49
    Last Post: 01-08-2008, 11:07 PM
  5. Replies: 6
    Last Post: 01-02-2004, 01:01 PM