-
October 20th, 2010, 11:53 AM
#1
[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?
-
October 20th, 2010, 11:55 AM
#2
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[]!
-
October 20th, 2010, 01:18 PM
#3
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.
-
October 21st, 2010, 07:30 AM
#4
Re: is there any problem if I set a char to '\0' within a string?
Originally Posted by monarch_dodra
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).
-
October 21st, 2010, 07:37 AM
#5
Re: is there any problem if I set a char to '\0' within a string?
Originally Posted by Feoggou
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.
-
October 21st, 2010, 10:46 AM
#6
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.
-
October 21st, 2010, 11:22 AM
#7
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!
-
October 21st, 2010, 11:47 AM
#8
Re: is there any problem if I set a char to '\0' within a string?
Originally Posted by Dateol
Try this
wsThing[19]='\0';
//Well, that is your ma!
Why?
-
October 21st, 2010, 01:17 PM
#9
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
-
October 21st, 2010, 01:42 PM
#10
Re: is there any problem if I set a char to '\0' within a string?
Originally Posted by Sharpie
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?
-
October 21st, 2010, 02:11 PM
#11
Re: is there any problem if I set a char to '\0' within a string?
Originally Posted by laserlight
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.
-
October 21st, 2010, 02:31 PM
#12
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.
-
October 21st, 2010, 02:51 PM
#13
Re: is there any problem if I set a char to '\0' within a string?
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.
-
October 21st, 2010, 05:00 PM
#14
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:
mov ax, [y]
muli ax, 4 ;4 bytes to an int
add sp, ax
-
October 21st, 2010, 05:19 PM
#15
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|