Char * sharing same memory

This is a discussion on Char * sharing same memory within the C Programming forums, part of the General Programming Boards category; Here is my code and I need a clarification in the output of this code: Here is my code and ...

  1. #1
    Registered User Raj 89's Avatar
    Join Date
    Nov 2012
    Location
    Chennai, TamilNadu
    Posts
    16

    Char * sharing same memory

    Here is my code and I need a clarification in the output of this code:

    Here is my code and I need a clarification in the output of this code:



    #include <stdio.h>

    int main()
    {
    char name1[10] = "Rajan" , name2[10] = "Rajan" ;
    char *name3 = "Chennai" , *name4 = "Chennai" ;
    printf("\nAddress for name1 and name2 : %p and %p",name1,name2) ;
    printf("\nAddress for name3 and name4 : %p and %p",name3,name4) ;
    return 0 ;
    }


    The output of this code is


    Address for name1 and name2 : 0x7fff9e6cbe10 and 0x7fff9e6cbe20
    Address for name3 and name4 : 0x400760 and 0x400760
    Here the address of the values name1 and name2 are different since i allocated two different arrays.
    But in the case of name3 and name4, the address is same, why is it not different?
    It won't create a different memory and allocate name for the value Chennai? Why is it pointing to the same memory?
    Last edited by Raj 89; 11-30-2012 at 06:55 AM.

  2. #2
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    You have assigned a static string, and then taken a pointer to the string.

    The string "Chennai" is statically allocated by the compiler. It knows that it cannot change. It also knows the second string is static and the same too. Thus the pointers to their location are likely to be the same if the compiler optimises them into a single instance (which is likely). You can't change either of them because of the static initialisation, so the compiler is completely correct in optimising this instance.

    Because the string is static, pointers to it work the same. You can change where the pointer points to but as long as it started pointing at the static string "Chennai", it will be the same as any other pointing to another static string "Chennai". If you wish to allocate a string for use elsewhere that you need to take a pointer to, or may need to change the string later, do so properly (dynamic allocation, not getting a pointer of a static string like the syntax you have).

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    > But in the case of name3 and name4, the address is same, why is it not different?
    > It won't create a different memory and allocate name for the value Chennai? Why is it pointing to the same memory?
    It's an allowed optimisation (it saves memory), because the strings are constants.
    There is nothing to be gained by duplicating the same information over and over.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    I'd like to add that you really, really shouldn't use:
    Code:
    char *name3 = "Chennai" , *name4 = "Chennai" ;
    As the strings are constants. Changing them will result in undefined behaviour (usually a crash). Instead use:
    Code:
    const char *name3 = "Chennai" , *name4 = "Chennai" ;
    It also makes it more obvious why the compiler can do that: you can't change either, at any time, so why have them stored twice?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sharing a Memory Space between two Processes
    By akm3 in forum C Programming
    Replies: 1
    Last Post: 04-02-2009, 02:06 AM
  2. Sharing Memory among Processes
    By doom in forum C# Programming
    Replies: 4
    Last Post: 10-04-2005, 06:51 PM
  3. allocating memory for char* array
    By creeping_death in forum C++ Programming
    Replies: 2
    Last Post: 06-17-2003, 04:49 AM
  4. Sharing memory Thread<->Process
    By marsface in forum Windows Programming
    Replies: 6
    Last Post: 08-14-2002, 11:52 PM
  5. Sharing memory
    By YALINI in forum C Programming
    Replies: 1
    Last Post: 08-28-2001, 12:47 PM

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