Author | Message | Time |
---|---|---|
Dyndrilliac | My program keeps crashing and I can't figure out for the life of me why.[code]#include <vector.h> #include <string.h> #include <stdio.h> typedef char* LPTSTR; typedef const char* LPCTSTR; vector<LPTSTR> SplitString(LPTSTR szTextBuffer, LPCTSTR Delimiters); int main() { vector<LPTSTR> StringSet; int i = 0; StringSet = SplitString("Hi! My name is Matt!", " "); for (i = 0; i < StringSet.size(); i++) { printf("%s\n", StringSet[i]); } return 0; } vector<LPTSTR> SplitString(LPTSTR szTextBuffer, LPCTSTR Delimiters) { vector<LPTSTR> Results; LPTSTR szTempBuffer = strtok(szTextBuffer, Delimiters); while (szTempBuffer != NULL) { Results.push_back(szTempBuffer); szTempBuffer = strtok(NULL, Delimiters); } return (Results); }[/code]It crashes on the line where 'szTempBuffer' is initialized at the top of the function definition of 'SplitString()'. All help on why this is happening and how to fix it is greatly appreciated. | November 15, 2005, 8:40 PM |
Dyndrilliac | Well, I got it to *WORK* but I still don't know what's wrong with my original code...[code]#include <vector.h> #include <string.h> #include <stdio.h> typedef char* LPTSTR; typedef const char* LPCTSTR; // Changed param 1 from char* to char[] vector<LPTSTR> SplitString(char szTextBuffer[], LPCTSTR Delimiters); int main() { vector<LPTSTR> StringSet; int i = 0; char String[] = "Hi! My name is Matt!"; StringSet = SplitString(String, " "); for (i = 0; i < StringSet.size(); i++) { printf("%s\n", StringSet[i]); } return 0; } vector<LPTSTR> SplitString(char szTextBuffer[], LPCTSTR Delimiters) { vector<LPTSTR> Results; LPTSTR szTempBuffer = strtok(szTextBuffer, Delimiters); while (szTempBuffer != NULL) { Results.push_back(szTempBuffer); szTempBuffer = strtok(NULL, Delimiters); } return (Results); }[/code] | November 15, 2005, 10:15 PM |
Kp | The bigger problem is what's wrong with your compiler. It should never have permitted you to build that first example! strtok writes into its first argument, but you were passing it a read-only string. Of course it's going to crash when strtok tries to write into read-only memory. The compiler should've warned you that you were passing a read-only argument to a function which (correctly!) warned you that it would be mutated. There's also the more minor issue that your typedefs are wrong. LPTSTR is a sequence of TCHARs, not of chars. | November 16, 2005, 12:39 AM |
Dyndrilliac | I'm using Microsoft Visual C++ 6. Is there an option I should have enabled somewhere to raise the awareness of warnings/errors? Anyway, I found a similar explanation at another resource. So does this mean I can't pass a char* or I just can't pass a char* when I don't assign it a variable name? Resource: http://gcc.gnu.org/ml/gcc-bugs/2001-04/msg00587.html | November 16, 2005, 2:59 AM |
Kp | [quote author=Dyndrilliac link=topic=13251.msg134176#msg134176 date=1132109945]I'm using Microsoft Visual C++ 6. Is there an option I should have enabled somewhere to raise the awareness of warnings/errors? Anyway, I found a similar explanation at another resource. So does this mean I can't pass a char* or I just can't pass a char* when I don't assign it a variable name? Resource: http://gcc.gnu.org/ml/gcc-bugs/2001-04/msg00587.html[/quote] Passing a char* is fine. You were passing a const char*, which is not fine. That your compiler didn't warn you about this mistake speaks of a bug or a misconfiguration in the compiler. In gcc, you'd get the warning if you turn on all the useful warnings. I don't recall how to do it in VC++6 -- it's been a long time since I had to deal with that mess of a tool, and I'm glad to be done with it. :) | November 16, 2005, 5:24 AM |
Dyndrilliac | Well, My warning level is 4 and I have it set to treat warnings as errors. Also, it isn't letting me pass a char* either. I replaced [code]char String[] = "Hi! My name is Matt!";[/code] with this:[code]LPSTR String = "Hi! My name is Matt!";[/code] And it still crashed. I changed my typedefs to:[code]typedef char* LPSTR; typedef const char* LPCSTR;[/code] | November 16, 2005, 5:47 AM |
Adron | The point is that "Hi! My name is Matt!" is a read-only string. You cannot pass in a pointer to a read-only string; you need a pointer to a read-write string. | November 16, 2005, 2:27 PM |
Eibro | Hi, this might be of some interest to you: http://cs.stmarys.ca/~e_brooks/strtok.cpp A robust splitting function, along the same lines as you have used. You could improve on it by templating it to take std::basic_string< char > or std::basic_string< wchar_t >. | November 17, 2005, 3:18 AM |
JoeTheOdd | [quote]The bigger problem is what's wrong with your compiler. It should never have permitted you to build that first example![/quote] [quote]I'm using Microsoft Visual C++ 6.[/quote] Am I not the only one who found that insanely funny? | November 20, 2005, 7:44 PM |
UserLoser. | [quote author=Joe link=topic=13251.msg134823#msg134823 date=1132515890] [quote]The bigger problem is what's wrong with your compiler. It should never have permitted you to build that first example![/quote] [quote]I'm using Microsoft Visual C++ 6.[/quote] Am I not the only one who found that insanely funny? [/quote] What's so funny? | November 20, 2005, 9:36 PM |