Thread: empty strings

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    330

    empty strings

    Can you depend on compilers that if you use the empty string literal "" all over the place that it only allocates space once for that string?

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    I don't think so, no.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Once... Or never... Or any time... It's upto compiler.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I would imagine that it would normally be once or never in most cases.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    VC++ 2005 has an option for "String Pooling", which sounds like it does what you're asking, but I think it's disabled by default.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The short answer is "Probably".

    The compiler can "merge" (or "fold") multiple constant strings into a single string - but usually only within that particular source file. Sometimes the linker is able to take it the full way to the whole executable, but not always.

    I wouldn't worry too much about empty strings - each instance only takes up a byte. Long strings (a common case is format strings for printf - obviously not in a C++ program, but I'm sure people duplicate strings all over the program in "cout" or similar) that may be repeated many times.

    --
    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.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> VC++ 2005 has an option for "String Pooling", which sounds like it does what you're asking, but I think it's disabled by default.

    That's a little odd. The compiler should already be allowed to do that for string literals, so I expected that option to be for non string literals. But the example they have uses string literals.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I think the string pooling is slightly different, the example they give is:
    Code:
    char *s = "This is a character buffer";
    char *t = "This is a character buffer";
    With this option t and s would point to the same memory.

    This is slightly different from string literals not assigned to pointers, e.g. as arguments to functions in various ways, where the address of the string isn't assigned to any variable.

    --
    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.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I guess, but it shouldn't be. The standard already deprecates the assignment above to non-const char *, and if you modify the string pointed to by s or t that is undefined behavior, so there's no reason not to maintain only one copy of that string. If the example used this:
    Code:
    char s[] = "This is a character buffer";
    char t[] = "This is a character buffer";
    then I might understand the point.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The reason string pooling is not enabled by default is backward compatibility with programs foolish enough to modify string literals. If the compiler merged strings, the modifications would show up in all strings.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    I think that would make more sense if it was const char [].

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with binary file c++
    By lucky_mutani in forum C++ Programming
    Replies: 4
    Last Post: 06-05-2009, 09:24 AM
  2. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  3. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM