CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 27
  1. #1
    Join Date
    Aug 2006
    Location
    Timisoara, Romania
    Posts
    433

    [RESOLVED] is there any problem if I set a char to '\0' within a string?

    Just to make sure: I have a string like this:
    Code:
    WCHAR* wsThing = new WCHAR[nLen]; //assume nLen = 20;
    wsThing[8] = 0;
    delete[] wsThing;
    I just want to make sure: it releases all memory, right?

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: is there any problem if I set a char to '\0' within a string?

    Yes, but consider:
    Code:
    std::vector<WCHAR> wsThing(nLen); //assume nLen = 20;
    wsThing[8] = 0;
    // Look ma, no need to remember to delete[]!
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: is there any problem if I set a char to '\0' within a string?

    You can do it, but you are undermining the system. Passing the string as an array to a C-Style API will break the API. You may have weird behavior when putting the string in a stream.

    Strings hold char, but they are really supposed to represent text. If you just want a container of char, consider using a vector<char> instead. The interface will be better adapted, and your code less confusing.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  4. #4
    Join Date
    Aug 2006
    Location
    Timisoara, Romania
    Posts
    433

    Re: is there any problem if I set a char to '\0' within a string?

    Quote Originally Posted by monarch_dodra View Post
    You can do it, but you are undermining the system. Passing the string as an array to a C-Style API will break the API. You may have weird behavior when putting the string in a stream.

    Strings hold char, but they are really supposed to represent text. If you just want a container of char, consider using a vector<char> instead. The interface will be better adapted, and your code less confusing.
    sorry for asking, but for whom is this message, for me or for laserlight?

    I only need to make sure that delete[] deletes the characters after '\0' as well. I'm using the string with windows API functions. the display is correct (I've had no problems with it).

  5. #5
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: is there any problem if I set a char to '\0' within a string?

    Quote Originally Posted by Feoggou View Post
    sorry for asking, but for whom is this message, for me or for laserlight?

    I only need to make sure that delete[] deletes the characters after '\0' as well. I'm using the string with windows API functions. the display is correct (I've had no problems with it).
    It was for you, but it was irrelevant. I hadn't taken the time to read your full message, sorry. I thought you were talking about std::string, when in fact you were talking about plain old char arrays.

    The delete will be correct, as delete does not look at the object contents contents to know the bounds. However, for mostly every other function of the type strlen, or strcopy, you have to make sure your string is null terminated.

    Also, consider using std::string, arrays are evil.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  6. #6
    Join Date
    Jan 2009
    Posts
    1,689

    Re: is there any problem if I set a char to '\0' within a string?

    delete doesn't care what's in the array. Null terminators are placed in the middle of arrays all the time, thats how you change the length of a string

    However, seeing that you're using C++, might I recommend std::wstring instead? That does all the memory management for you.

  7. #7
    Join Date
    Oct 2010
    Posts
    8

    Re: is there any problem if I set a char to '\0' within a string?

    Try this
    wsThing[19]='\0';
    //Well, that is your ma!

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: is there any problem if I set a char to '\0' within a string?

    Quote Originally Posted by Dateol View Post
    Try this
    wsThing[19]='\0';
    //Well, that is your ma!
    Why?

  9. #9
    Join Date
    Jul 2010
    Posts
    94

    Re: is there any problem if I set a char to '\0' within a string?

    This is an Obama application, then they are the peasants, farmers or idiotic laymen,
    and we are the government sometimes the general, sometimes the CIAs etc, All that high! ****ing

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: is there any problem if I set a char to '\0' within a string?

    Quote Originally Posted by Sharpie View Post
    This is an Obama application, then they are the peasants, farmers or idiotic laymen,
    and we are the government sometimes the general, sometimes the CIAs etc, All that high! ****ing
    This forum attracts more than its share of nuts. What is the purpose of that post, and did you forget your meds?

  11. #11
    Join Date
    Jan 2009
    Posts
    1,689

    Re: is there any problem if I set a char to '\0' within a string?

    Quote Originally Posted by laserlight View Post
    Yes, but consider:
    Code:
    std::vector<WCHAR> wsThing(nLen); //assume nLen = 20;
    wsThing[8] = 0;
    // Look ma, no need to remember to delete[]!
    You can even do this
    Code:
    WCHAR wsThing[nLen];
    wsThing[8] = 0;
    //I don't need delete[] either :)
    It's not standard, but most compilers allow you to do this. GNU certainly does, I would be very surprised if VC++ doesn't. I'm guessing that behind the scenes, it uses an autoptr.

  12. #12
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: is there any problem if I set a char to '\0' within a string?

    That does not work in VS because it's only standard in C99, and VS does not support C99. Since gcc supports C99 anyway, allowing c++ to do it too is an easy extension, but you should absolutely not write C++ that way if you have any expectation of portability.

  13. #13
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: is there any problem if I set a char to '\0' within a string?

    Quote Originally Posted by ninja9578
    I'm guessing that behind the scenes, it uses an autoptr.
    I have the impression that gcc's implementation of variable length arrays uses the stack, so the g++ extension probably does so too rather than make use of a smart pointer behind the scenes.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  14. #14
    Join Date
    Jan 2009
    Posts
    1,689

    Re: is there any problem if I set a char to '\0' within a string?

    Oh, I guess so. I actually don't see a reason for this to not be added to the standard. It is really easy to implement:

    Code:
    int x[y];
    Code:
    mov ax, [y]
    muli ax,  4  ;4 bytes to an int
    add sp, ax

  15. #15
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: is there any problem if I set a char to '\0' within a string?

    Well, it does slightly complicate the computation of the stack position of other locals.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured