Do I need to resize wstring if it already has a string but I'm assigning a new string with different size?

Posted on 2006-04-27
Last Modified: 2012-06-21
The following variables are all wstring.  However, those in userDataIter(vector Iterator) points to already has a string them.  Would the code below be correct as far as the memory goes for wstring?  Thank you.

            if(!(userData->LoginName.empty())&& (LoginNameChanged))      
                  userDataIter->LoginName = userData->LoginName;

            if(!(userData->shell.empty())&& (shellChanged))
                  userDataIter->shell = userData->shell;
Question by:lapucca
    LVL 86

    Expert Comment

    If you're talking about a 'std::wstring' - no, not at all. The string object itself will take care about that.
    LVL 12

    Accepted Solution

    >>               userDataIter->LoginName.clear();
    >>               userDataIter->LoginName = userData->LoginName;
    I find the first statement useless. Since you are assigning in the next line.
    You can remove that statement.
    LVL 86

    Expert Comment


    You asked whether you need to resiue a string - I answered 'No'. Is that incoorect? No, it isn't... So I cannot quite understand on which basis you are picking your answers.

    Author Comment

    Hi jkr,

    Your answer is correct and was posted an hour earlier.  I should have accept your answer instead.  I picked rajeev's answer becuase he also point out the other question I meant to ask, do I need to clear the string firs (_bstr_t requires detach first).  

    Anyway, you're still correct.  I'll pick my answer more carefully next time based on my question.  Thank you.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
    Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now