delete [] problem, and extra char(s) showing up in substring?
-
int main() {
char * lpszData = "file=abcdefg&info=&x=3&4=5";
int count = 0;
char * pt = strchr(lpszData, '&');
while ( pt != NULL ) {
count++;
pt = strchr(++pt, '&');
};
cout << "Data: " << lpszData << "\n";
cout << "Count: " << count << "\n\n";char \*\* split = new char \* \[count + 1\]; char \* start = lpszData; for (int i = 0; i <= count; i++) { pt = strchr(start, '&'); if ( pt == NULL ) pt = lpszData + strlen(lpszData); split\[i\] = new char\[pt - start + 1\]; strset(split\[i\], 0); strncpy(split\[i\], start, pt - start); start = pt + 1; }; for (int i = 0; i <= count; i++) { cout << "split\[" << i << "\] = " << split\[i\] << "\\n"; }; for (int i = 0; i <= count; i++) delete \[\] split\[i\]; delete \[\] split;
}
First, the
delte [] split[i];
in the for loop at the end gives me an error, but when I remove this line the code leaks. Second, the output is:Data: file=abcdefg&info=&x=3&4=5
Count: 3split[0] = file=abcdefg
split[1] = info=_[extended_char]_3
split[2] = x=3
split[3] = 4=5Try as I might I can't figure out where the extra characters are coming from in
split[1]
. Can anyone help with either of these problems? Thanks for any assistance you can give, MZR -
int main() {
char * lpszData = "file=abcdefg&info=&x=3&4=5";
int count = 0;
char * pt = strchr(lpszData, '&');
while ( pt != NULL ) {
count++;
pt = strchr(++pt, '&');
};
cout << "Data: " << lpszData << "\n";
cout << "Count: " << count << "\n\n";char \*\* split = new char \* \[count + 1\]; char \* start = lpszData; for (int i = 0; i <= count; i++) { pt = strchr(start, '&'); if ( pt == NULL ) pt = lpszData + strlen(lpszData); split\[i\] = new char\[pt - start + 1\]; strset(split\[i\], 0); strncpy(split\[i\], start, pt - start); start = pt + 1; }; for (int i = 0; i <= count; i++) { cout << "split\[" << i << "\] = " << split\[i\] << "\\n"; }; for (int i = 0; i <= count; i++) delete \[\] split\[i\]; delete \[\] split;
}
First, the
delte [] split[i];
in the for loop at the end gives me an error, but when I remove this line the code leaks. Second, the output is:Data: file=abcdefg&info=&x=3&4=5
Count: 3split[0] = file=abcdefg
split[1] = info=_[extended_char]_3
split[2] = x=3
split[3] = 4=5Try as I might I can't figure out where the extra characters are coming from in
split[1]
. Can anyone help with either of these problems? Thanks for any assistance you can give, MZRMike the Red wrote:
strset(split[i], 0);
Replace with
split[i][pt - start]='\0';
:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Mike the Red wrote:
strset(split[i], 0);
Replace with
split[i][pt - start]='\0';
:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]-nt-
-
-nt-
:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
int main() {
char * lpszData = "file=abcdefg&info=&x=3&4=5";
int count = 0;
char * pt = strchr(lpszData, '&');
while ( pt != NULL ) {
count++;
pt = strchr(++pt, '&');
};
cout << "Data: " << lpszData << "\n";
cout << "Count: " << count << "\n\n";char \*\* split = new char \* \[count + 1\]; char \* start = lpszData; for (int i = 0; i <= count; i++) { pt = strchr(start, '&'); if ( pt == NULL ) pt = lpszData + strlen(lpszData); split\[i\] = new char\[pt - start + 1\]; strset(split\[i\], 0); strncpy(split\[i\], start, pt - start); start = pt + 1; }; for (int i = 0; i <= count; i++) { cout << "split\[" << i << "\] = " << split\[i\] << "\\n"; }; for (int i = 0; i <= count; i++) delete \[\] split\[i\]; delete \[\] split;
}
First, the
delte [] split[i];
in the for loop at the end gives me an error, but when I remove this line the code leaks. Second, the output is:Data: file=abcdefg&info=&x=3&4=5
Count: 3split[0] = file=abcdefg
split[1] = info=_[extended_char]_3
split[2] = x=3
split[3] = 4=5Try as I might I can't figure out where the extra characters are coming from in
split[1]
. Can anyone help with either of these problems? Thanks for any assistance you can give, MZRMike the Red wrote:
split[i] = new char[pt - start + 1]; strset(split[i], 0);
The second statement is effectively canceling out the first.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
int main() {
char * lpszData = "file=abcdefg&info=&x=3&4=5";
int count = 0;
char * pt = strchr(lpszData, '&');
while ( pt != NULL ) {
count++;
pt = strchr(++pt, '&');
};
cout << "Data: " << lpszData << "\n";
cout << "Count: " << count << "\n\n";char \*\* split = new char \* \[count + 1\]; char \* start = lpszData; for (int i = 0; i <= count; i++) { pt = strchr(start, '&'); if ( pt == NULL ) pt = lpszData + strlen(lpszData); split\[i\] = new char\[pt - start + 1\]; strset(split\[i\], 0); strncpy(split\[i\], start, pt - start); start = pt + 1; }; for (int i = 0; i <= count; i++) { cout << "split\[" << i << "\] = " << split\[i\] << "\\n"; }; for (int i = 0; i <= count; i++) delete \[\] split\[i\]; delete \[\] split;
}
First, the
delte [] split[i];
in the for loop at the end gives me an error, but when I remove this line the code leaks. Second, the output is:Data: file=abcdefg&info=&x=3&4=5
Count: 3split[0] = file=abcdefg
split[1] = info=_[extended_char]_3
split[2] = x=3
split[3] = 4=5Try as I might I can't figure out where the extra characters are coming from in
split[1]
. Can anyone help with either of these problems? Thanks for any assistance you can give, MZRJust for your information: you might be interested in the strtok[^] function. It could ease your life a lot in this case ;)
Cédric Moonen Software developer
Charting control [v2.0] OpenGL game tutorial in C++ -
Just for your information: you might be interested in the strtok[^] function. It could ease your life a lot in this case ;)
Cédric Moonen Software developer
Charting control [v2.0] OpenGL game tutorial in C++I can't tell you how many times I've tried to find this function - I KNEW there had to be one... I always looked at the list of "String Manipulation Routines", saw
strtok
and its description, and said to myself "What the hell is a token?" :doh: Thankfully, I followed your link and looked at the example. You're right - this could ease my life in a LOT of cases! :thumbsup: Thank you, sir! MZR