// Create the 2 pointers
char* string1 = NULL;
// Make string1 point to a umteen-char array
// Note that I do not include the '\0' in the string
// This _should_ make room for the string _and_ the
// null terminator, correct?
string1 = new char[strlen("My Test String")];
// Make the first char in string1 be a null-terminator,
// so that we can concatenate starting at the beginning
string1[0] = 0;
// Concatenate the test string onto string1.
strcat(string1,"My Test String");
// ----POINT OF INTEREST-----
// Can I do the following without writing blindly to some other
// variable or memory on the computer?
string1[strlen(string1)] = 0;
// Or, should I do this in order to put the null terminator
// at the last possible space in the array?
// string1[strlen(string1)+1] = 0
A little bit on char*'s
Alright, I have seen two ways of going about char*'s, and I am just a bit curious as to where to put the null terminator.
Basically what I am asking is...
When you make a char array [or a char* that points to a char array] and you set the length of it, lets say 'char a[255]', can I use a[255] to store the null terminator, or MUST I use a[254]. Does declaring it as a[255] leave enough space for 255 characters and the null terminator?
[edited by - zackriggle on March 21, 2003 11:20:13 PM]
quote: 1.) Will the below code leave enough space for the null terminator? Or, do I need to use (strlen(string1)+1)You need to use strlen(string1)+1.
quote: // Make the first char in string2 nullThese two lines can be replaced with one call to strcpy. Also, both strcpy and strcat leave a null terminator at the end of the string being modified.
string2[0] = 0;
// Concatenate string1 onto string2
strcat(string2,string1);
quote: 2.) Will the below code make the last char in the string equal to 0, and have the desired effect of making the string null-terminated, or will it write blindly to some other variable?It''ll look for string2''s null terminator (that''s how strlen works), and write a new null terminator over the old one. However, if string2 wasn''t null-terminated to begin with, the code would have a chance of crashing.
string2[strlen(string2)] = 0
quote: char a[255];No; a[254] is the last element.
Can I use a[255] to be my null terminator?
Hope that helps.
Just so no-one looking at this gets confused, I changed the code before I noticed that anyone had posted.
Also, would this be a way of reducing memory leaks when re-assigning char*'s that you cannot suppose have been set to null, initialized, and basically have no idea what they point to?
[edited by - zackriggle on March 21, 2003 12:37:37 AM]
Also, would this be a way of reducing memory leaks when re-assigning char*'s that you cannot suppose have been set to null, initialized, and basically have no idea what they point to?
if(backup_path != NULL){ // It is pointing at _something_. If it is a array, delete the array if(strlen(backup_path) > 0) { delete [] backup_path; backup_path = NULL; } // If it is not an array, just delete the pointer. else { delete backup_path; backup_path = NULL; }}
[edited by - zackriggle on March 21, 2003 12:37:37 AM]
No, thats bad. new char is not the same as new char[1]. Even if the array only has one element, you still use delete[] on it.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
There is no way to tell from a pointer whether it''s pointing to an array or a single object - but you''ll always allocate char*s as arrays anyway. Since delete[] NULL; is harmless (it does nothing), the code can be reduced to:
delete[] backup_path;
backup_path = NULL;
You might want to consider the string class sometime. As a quick example:
delete[] backup_path;
backup_path = NULL;
You might want to consider the string class sometime. As a quick example:
#include <iostream>#include <string>using namespace std;int main(){ string a = "Type", b = "something: "; string c = a + '' '' + b; cout << c << flush; // prints "Type something: " string line; getline(cin, line); cout << "You typed: " << line << endl;}
I've considered the string class, as well as the CString class, however I found that I cannot do this:
Some_String = Some_Char_Pointer
I'm sure there is a function that does that, I'm pretty sure that it is string::assign(). Hmm... ever since I got this Visual Assist thing some overloaded functions are showing up that didn't with plain MS IntelliSense. What I mean is, before, the little yellow box popped up with this:
_Myt& assign(const _Myt& _X)
I had no idea what in the burning hells that meant. Now, it shows several overloaded versions, including this:
_Myt& assign(const char*)
and
_Myt& assign(const char*,size_type)
I still don't know what a _Myt is, and I'm not looking through string.h to find out because I'll come out more confused than when I went in. Anyone got a clue what a _Myt is?
--Conclusion--
Going with std::string
== One Last Question ==
Are there any ways to perform file I\O other than fstream, ifstream, and ofstream (yes, I know ifstream is just input fstream and ofstream is output fstream)
[edited by - zackriggle on March 22, 2003 4:47:55 PM]
Some_String = Some_Char_Pointer
I'm sure there is a function that does that, I'm pretty sure that it is string::assign(). Hmm... ever since I got this Visual Assist thing some overloaded functions are showing up that didn't with plain MS IntelliSense. What I mean is, before, the little yellow box popped up with this:
_Myt& assign(const _Myt& _X)
I had no idea what in the burning hells that meant. Now, it shows several overloaded versions, including this:
_Myt& assign(const char*)
and
_Myt& assign(const char*,size_type)
I still don't know what a _Myt is, and I'm not looking through string.h to find out because I'll come out more confused than when I went in. Anyone got a clue what a _Myt is?
--Conclusion--
Going with std::string
== One Last Question ==
Are there any ways to perform file I\O other than fstream, ifstream, and ofstream (yes, I know ifstream is just input fstream and ofstream is output fstream)
[edited by - zackriggle on March 22, 2003 4:47:55 PM]
quote: Original post by zackriggle
Anyone got a clue what a _Myt is?
You''re not supposed to know. Thats why it starts with an underscore
quote:
== One Last Question ==
Are there any ways to perform file I\O other[\i] than fstream, ifstream, and ofstream (yes, I know ifstream is just input fstream and ofstream is output fstream)
You can use C-style IO(fprintf etc...) or use your OS'' file API calls(CreateFile, WriteFile etc... on Windows).
"If there is a God, he is a malign thug."
-- Mark Twain
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement