Click here to Skip to main content
15,884,472 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: How to parse HL7 message in VC++ Pin
Abhijit D. Babar18-Apr-09 1:05
Abhijit D. Babar18-Apr-09 1:05 
GeneralRe: How to parse HL7 message in VC++ Pin
Stuart Dootson18-Apr-09 1:07
professionalStuart Dootson18-Apr-09 1:07 
GeneralRe: How to parse HL7 message in VC++ Pin
Abhijit D. Babar18-Apr-09 1:40
Abhijit D. Babar18-Apr-09 1:40 
GeneralRe: How to parse HL7 message in VC++ Pin
Stuart Dootson18-Apr-09 1:50
professionalStuart Dootson18-Apr-09 1:50 
GeneralRe: How to parse HL7 message in VC++ [modified] Pin
Abhijit D. Babar18-Apr-09 3:05
Abhijit D. Babar18-Apr-09 3:05 
GeneralRe: How to parse HL7 message in VC++ Pin
Stuart Dootson18-Apr-09 5:08
professionalStuart Dootson18-Apr-09 5:08 
QuestionSplit string into substrings Pin
Manfr3d17-Apr-09 21:12
Manfr3d17-Apr-09 21:12 
AnswerRe: Split string into substrings [modified] Pin
Stuart Dootson17-Apr-09 21:56
professionalStuart Dootson17-Apr-09 21:56 
Austrian_Programmer wrote:
When I try to execute the program a runtime error occurs saying that there is a null-reference exception in the line list[c] = strtok(str, delimiters);. To ensure that there is enough free memory I even used the new operator.


That's because (although exmp is a char*), it's pointing at a string stored in read-only memory (string literals are read-only). strtok needs to write to it. It can't, so you get a runtime exception. Change

char* exmp = "I have a problem!";


to

char exmp[] = "I have a problem!";


and the crash goes away, because now you've allocated a read/write character array and initialised it from the (read-only) string literal.

There are many other issues as well, however:

  1. Look at the strtok documentation[^] - you only pass in str on the first call to strtok for that string - the rest of the time, you pass in NULL.
  2. The loop condition in str2list is all wrong. list is a char**, so sizeof(list)/sizeof(char*) will be equal to 1. If you want to be safe with respect to the size of list, you need to explicitly pass it in - C/C++ doesn't pass around array sizes with arrays.
  3. Similarly, the loop condition in _tmain is wrong (although this problem goes away if you take note of the next point).
  4. There's no point using new when you have a fixed array size - declaring arglist with char* arglist[16]; will work just as well.
  5. I'm...interested that you use char as the type of the loop variables
  6. Although the for loop in str2list may compile with the compiler you're using, it won't with VS2008 (and possibly won't with earlier compilers), and isn't compliant with the C++ standard - going by the standard, your loop variable isn't accessible outside the loop.
  7. You return the number of tokens in the list from str2list...and then don't use it. Mmmmm.


Anyway - here's my fixed (I think) version of your code.

#include <iostream>
#include <string.h>

using namespace std;

int str2list(char* str, char** list, int maxTokenCount, const char* delimiters)
{
   for (int c = 0; c<maxtokencount;>   {
      list[c] = strtok(str, delimiters);
      if (list[c] == NULL) return c;
      str = 0;
   }
}

int main()
{
   char* arglist[16] = {0};
   char exmp[] = "I have a problem!";
   const int numToks = str2list(exmp, arglist, 16, " ");
   for (int c = 0; c < numToks; c++)
   {
      cout << arglist[c] << endl;
   }
   return 0;
}


[edit]Just for laughs, I did a version using STL and C++ classes, for those of us who like type-safety and nice things like that. It is special cased for the situation where the separator is a space (you can alter that by playing with locales and facets, but that's an exercise for the reader), but anyway.

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>

using namespace std;

vector<string> split(string const& exmp)
{
   istringstream s(exmp);
   return vector<string>(istream_iterator<string>(s), istream_iterator<string>());
}

int main()
{
   string exmp = "I have a problem!";
   const vector<string> toks = split(exmp);
   
   copy(toks.begin(), toks.end(), ostream_iterator<string>(cout, "\n"));

   return 0;
}
[/edit]

Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

modified on Saturday, April 18, 2009 11:11 AM

GeneralRe: Split string into substrings Pin
Manfr3d18-Apr-09 5:04
Manfr3d18-Apr-09 5:04 
GeneralRe: Split string into substrings Pin
Stuart Dootson18-Apr-09 5:14
professionalStuart Dootson18-Apr-09 5:14 
QuestionHow do I develop an internet video conferencing system like skype? Pin
Hiigara17-Apr-09 11:16
Hiigara17-Apr-09 11:16 
AnswerRe: How do I develop an internet video conferencing system like skype? Pin
Divyang Mithaiwala17-Apr-09 19:40
Divyang Mithaiwala17-Apr-09 19:40 
AnswerRe: How do I develop an internet video conferencing system like skype? Pin
zhanzongru19-Apr-09 1:20
zhanzongru19-Apr-09 1:20 
QuestionDLL Processing Speed, Pin
ScotDolan17-Apr-09 8:55
ScotDolan17-Apr-09 8:55 
AnswerRe: DLL Processing Speed, Pin
Stuart Dootson17-Apr-09 9:04
professionalStuart Dootson17-Apr-09 9:04 
AnswerRe: DLL Processing Speed, Pin
Luc Pattyn17-Apr-09 11:26
sitebuilderLuc Pattyn17-Apr-09 11:26 
QuestionDo loop while waiting for a component event Pin
digitalni17-Apr-09 8:22
digitalni17-Apr-09 8:22 
AnswerRe: Do loop while waiting for a component event Pin
Stuart Dootson17-Apr-09 8:48
professionalStuart Dootson17-Apr-09 8:48 
GeneralRe: Do loop while waiting for a component event Pin
digitalni17-Apr-09 9:20
digitalni17-Apr-09 9:20 
GeneralRe: Do loop while waiting for a component event Pin
Stuart Dootson17-Apr-09 9:35
professionalStuart Dootson17-Apr-09 9:35 
QuestionBaes/Derived Class Question Pin
ForNow17-Apr-09 7:04
ForNow17-Apr-09 7:04 
AnswerRe: Baes/Derived Class Question Pin
CPallini17-Apr-09 7:11
mveCPallini17-Apr-09 7:11 
AnswerRe: Baes/Derived Class Question Pin
led mike17-Apr-09 7:21
led mike17-Apr-09 7:21 
GeneralRe: Baes/Derived Class Question Pin
ForNow17-Apr-09 12:32
ForNow17-Apr-09 12:32 
GeneralRe: Baes/Derived Class Question Pin
ForNow18-Apr-09 15:38
ForNow18-Apr-09 15:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.