address of constant

This is a discussion on address of constant within the C Programming forums, part of the General Programming Boards category; Hello everyone, I have tried that even if I have multiple character pointer variables, if the content they pointed to ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    address of constant

    Hello everyone,


    I have tried that even if I have multiple character pointer variables, if the content they pointed to are the same, they are allocated with the same value. So, I think from compiler point of view, the constant with the same content shares the single copy in memory?

    Here is my test program in Visual Studio 2005.

    Code:
    #include <windows.h>
    
    int main (int argc, char** argv)
    {
    	char* abc = "Hello";
    
    	char* abc2 = "Hello";
    
    	// the value (address) of abc is the same as abc2
    
    	return 0;
    }

    thanks in advance,
    George

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    Yes, that is correct.

    It is allowed to merge both copies of the same string into one, and make both pointers point at the same thing.
    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.

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    Well George, abc points to read-only memory (string literals are read only, but they can be stored in writeable memory, like an array of characters). Therefore, one storage space for "Hello" should be sufficient since it is never moved or edited. Memory that you are allowed to write to is different, and may require several copies of the same object, like two integers that happen to be 4.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Also beware that the compiler may well produce multiple strings if you use multiple source files in the project. So, you can't RELY on it being the same address [in fact, some compilers won't even merge the strings within a single source file].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What does this do (Windows API)?
    By EVOEx in forum Windows Programming
    Replies: 4
    Last Post: 12-19-2008, 09:48 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. DX - CreateDevice - D3DERR_INVALIDCALL
    By Tonto in forum Game Programming
    Replies: 3
    Last Post: 12-01-2006, 06:17 PM
  5. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 09:43 AM

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