Thread: C/C++ string allocation question

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    19

    C/C++ string allocation question

    Hi guys,

    I was wondering, if I do something like this,

    Code:
    LPWSTR func()
    {
        LPWSTR pszString = L"Some string";
    
        return pszString;
    }
    
    int main(...)
    {
        ...
        LPWSTR pszString = func();
        ...
    }
    Where does the string get saved? Is it saved in stack?
    I am asking this because I wanted to know if using returned pszString
    in the main function is legal or not. Thanks!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by donglee
    Where does the string get saved? Is it saved in stack?
    I am asking this because I wanted to know if using returned pszString
    in the main function is legal or not. Thanks!
    It would be saved in the data segment, and have static storage duration. Consequently, you can use the pointer returned in the global main function. However, any attempt to change what the pointer points to will result in undefined behaviour.

    Disclaimer: this is something at the edge of my knowledge, so you are better off having someone more sure of the facts to confirm or deny this (as in the storage). Of course, note that the details are implementation dependent, and this is the general C++ programming forum after all, although the only thing Windows specific about your question is LPWSTR.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I can confirm that the posted code is legal. Any string constant (when not used to initialize a char array) in the code will be stored in the "read only data" section [or the code section, or the data section - it is up to the compiler to decide THAT part - wherever it is stored will be "permanent"]. The only "local" storage in your code is the actual pointer, which is initialized to the permanent location of the string.

    Edit: to avoid accidentally writing to the string, I suggest you add const in front of all the LPWSTR, e.g. const LPWSTR func(), const LPWSTR pszString, etc.

    That way, thecompiler will tell you when you try to pass the string to a function that may modify it, or if you write code yourself that modify the string [assuming of course you are not using functions that aren't const-correct].

    --
    Mats
    Last edited by matsp; 04-28-2009 at 11:35 AM.
    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.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by matsp
    to avoid accidentally writing to the string, I suggest you add const in front of all the LPWSTR, e.g. const LPWSTR func(), const LPWSTR pszString, etc.
    That seems insufficient to me since it would only avoid accidentally changing the pointer without avoiding changes to what the pointer points to.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by laserlight View Post
    That seems insufficient to me since it would only avoid accidentally changing the pointer without avoiding changes to what the pointer points to.
    No, that would be const wchar_t *xxx, which means that the content of the string is constand, but the pointer can be changed. wchar_t * const xxx is what you need to say to make sure that the pointer can not be changed.

    However, the point is moot, as the typedef for LPCWSTR is const already (C stands for CONST in the MS naming convention, apparently).

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

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    34
    You guys are awesome, thank you for your answers. I tried to search on the web for .text and .data memory segments used in C++, but I could only get some short descriptions. Which book would contain low level C/C++ details like this? I've been looking for such book for a long time. I recently bought the book "The C++ Programming," but it doesn't seem to contain such details.

    Also, where do "heap" and "stack" memories fit into the picture?
    I suppose that the text and data segments you mentioned are the ones located at the bottom of the diagram?
    http://www.cs.rit.edu/~hpb/Lectures/..._and_stack.gif

    Thanks!

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by chiefmonkey View Post
    You guys are awesome, thank you for your answers. I tried to search on the web for .text and .data memory segments used in C++, but I could only get some short descriptions. Which book would contain low level C/C++ details like this? I've been looking for such book for a long time. I recently bought the book "The C++ Programming," but it doesn't seem to contain such details.
    The various segments that compose a running program are at a level below the C++ language. Books which cover computer architecture or assembly language will have much more detail than any C++ book.

    Also, where do "heap" and "stack" memories fit into the picture?
    Heap, stack, code and data are architecture/platform details. Any picture is going to be highly generalized and may not reflect the actual situation on a given platform. What is important from a C++ standpoint is that these regions are distinct from each other, and quite possibly have different access rules.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by matsp
    No, that would be const wchar_t *xxx, which means that the content of the string is constand, but the pointer can be changed. wchar_t * const xxx is what you need to say to make sure that the pointer can not be changed.
    Precisely: since LPWSTR is a typedef of wchar_t*, const LPWSTR is an alias for wchar_t* const, not const wchar_t*.

    Quote Originally Posted by matsp
    However, the point is moot, as the typedef for LPCWSTR is const already (C stands for CONST in the MS naming convention, apparently).
    Yes, then the correct change would be to use LPCWSTR instead of LPWSTR.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of strings and memory allocation
    By maluyk in forum C Programming
    Replies: 7
    Last Post: 01-26-2009, 11:52 AM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  3. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  4. Next Question...
    By Azmeos in forum C++ Programming
    Replies: 3
    Last Post: 06-06-2003, 02:40 PM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM