Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. bad pointer

bad pointer

Scheduled Pinned Locked Moved C / C++ / MFC
algorithmsjsonquestion
12 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jon 80
    wrote on last edited by
    #1

    strToken is retaining an invalid value. + strToken 0xcccccccc char * unsigned int CSentenceAr::search(const char* strSearch) { // initialize local variables unsigned long iWordCount = 0; char buffer[MAX_SENTENCE_LENGTH] = {0}; /* Algorithm: strToken shall initially contain the sentence string. When strWord is found from the string, this is removed from the string so that this function checks the rest of the string for other instances of the word. */ char* strToken = {0}; for (int iCurrent = 0; iCurrent < Size; iCurrent++) // copy to buffer, used as parameter for strstr. { memcpy(buffer, Sentences.strSentence[iCurrent], MAX_SENTENCE_LENGTH); strToken = strstr(buffer,strSearch); while (strToken !=0 && *strToken != 0) { if (strlen(strToken) > 0) { iWordCount++; strToken += strlen(strSearch); } if (strToken >= buffer + MAX_SENTENCE_LENGTH) {break;} strToken = strstr(strToken, strSearch); } } return (int) iWordCount; } Is it declared incorrectly? Jon

    D Z S 3 Replies Last reply
    0
    • J jon 80

      strToken is retaining an invalid value. + strToken 0xcccccccc char * unsigned int CSentenceAr::search(const char* strSearch) { // initialize local variables unsigned long iWordCount = 0; char buffer[MAX_SENTENCE_LENGTH] = {0}; /* Algorithm: strToken shall initially contain the sentence string. When strWord is found from the string, this is removed from the string so that this function checks the rest of the string for other instances of the word. */ char* strToken = {0}; for (int iCurrent = 0; iCurrent < Size; iCurrent++) // copy to buffer, used as parameter for strstr. { memcpy(buffer, Sentences.strSentence[iCurrent], MAX_SENTENCE_LENGTH); strToken = strstr(buffer,strSearch); while (strToken !=0 && *strToken != 0) { if (strlen(strToken) > 0) { iWordCount++; strToken += strlen(strSearch); } if (strToken >= buffer + MAX_SENTENCE_LENGTH) {break;} strToken = strstr(strToken, strSearch); } } return (int) iWordCount; } Is it declared incorrectly? Jon

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      jon_80 wrote:

      strToken is retaining an invalid value.

      At what point?


      "Money talks. When my money starts to talk, I get a bill to shut it up." - Frank

      "Judge not by the eye but by the heart." - Native American Proverb

      J 1 Reply Last reply
      0
      • D David Crow

        jon_80 wrote:

        strToken is retaining an invalid value.

        At what point?


        "Money talks. When my money starts to talk, I get a bill to shut it up." - Frank

        "Judge not by the eye but by the heart." - Native American Proverb

        J Offline
        J Offline
        jon 80
        wrote on last edited by
        #3

        Since it is declared (see the code), that's why I said retained. The problem is that since it has an invalid value the program is not going through the while loop.

        Jon

        D 1 Reply Last reply
        0
        • J jon 80

          strToken is retaining an invalid value. + strToken 0xcccccccc char * unsigned int CSentenceAr::search(const char* strSearch) { // initialize local variables unsigned long iWordCount = 0; char buffer[MAX_SENTENCE_LENGTH] = {0}; /* Algorithm: strToken shall initially contain the sentence string. When strWord is found from the string, this is removed from the string so that this function checks the rest of the string for other instances of the word. */ char* strToken = {0}; for (int iCurrent = 0; iCurrent < Size; iCurrent++) // copy to buffer, used as parameter for strstr. { memcpy(buffer, Sentences.strSentence[iCurrent], MAX_SENTENCE_LENGTH); strToken = strstr(buffer,strSearch); while (strToken !=0 && *strToken != 0) { if (strlen(strToken) > 0) { iWordCount++; strToken += strlen(strSearch); } if (strToken >= buffer + MAX_SENTENCE_LENGTH) {break;} strToken = strstr(strToken, strSearch); } } return (int) iWordCount; } Is it declared incorrectly? Jon

          Z Offline
          Z Offline
          Zac Howland
          wrote on last edited by
          #4

          Break up the scope of your function: Function 1 will take a single sentence and count how many times a substring appears in it. Function 2 will take a collection of sentences and call Function 1 on each of them and sum the results.

          const unsigned long MAX_SENTENCE_LENGTH = 100;
          
          unsigned long HowManyInSentence(const char* sentence, const char* substring)
          {
                  char buffer[MAX_SENTENCE_LENGTH + 1] = {0};
                  memcpy(buffer, sentence, MAX_WORD_LENGTH);
                  char* token = 0;
                  unsigned long wordCount = 0;
                  token = strstr(buffer, substring);
                  while (token != 0 && *token != 0)
                  {
                          if (strlen(token) > 0)
                          {
                                  ++wordCount;
                                  token += strlen(substring);
                                  if (token >= buffer + MAX_WORD_LENGTH)
                                  {
                                          break;
                                  }
                          }
                          token = strstr(token, substring);
                  }
                  return wordCount;
          }
          
          unsigned long HowMany(const char* substring)
          {
          	// assume that g_sentences is declared somewhere and has MAX_SENTENCES entries
          	unsigned long count = 0;
          	for (int i = 0; i < MAX_SENTENCES; ++i)
          	{
          		count += HowManInSentence(g_sentences[i], substring);
          	}
          
          	return count;
          }
          

          Make sure that your buffer is null-terminated otherwise token will end up off in the weeds. Oh, and token doesn't need to be assigned with scope (that is, you don't need char* token = {0};). char* token = 0; is fine and more readable. -- modified at 12:58 Friday 4th August, 2006

          If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

          J 1 Reply Last reply
          0
          • J jon 80

            Since it is declared (see the code), that's why I said retained. The problem is that since it has an invalid value the program is not going through the while loop.

            Jon

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            strstr() will return NULL if strSearch is not found in buffer. That would be why the while loop is not entered. You should be able to set a breakpoint on the strstr() statement to confirm this.


            "Money talks. When my money starts to talk, I get a bill to shut it up." - Frank

            "Judge not by the eye but by the heart." - Native American Proverb

            J 1 Reply Last reply
            0
            • J jon 80

              strToken is retaining an invalid value. + strToken 0xcccccccc char * unsigned int CSentenceAr::search(const char* strSearch) { // initialize local variables unsigned long iWordCount = 0; char buffer[MAX_SENTENCE_LENGTH] = {0}; /* Algorithm: strToken shall initially contain the sentence string. When strWord is found from the string, this is removed from the string so that this function checks the rest of the string for other instances of the word. */ char* strToken = {0}; for (int iCurrent = 0; iCurrent < Size; iCurrent++) // copy to buffer, used as parameter for strstr. { memcpy(buffer, Sentences.strSentence[iCurrent], MAX_SENTENCE_LENGTH); strToken = strstr(buffer,strSearch); while (strToken !=0 && *strToken != 0) { if (strlen(strToken) > 0) { iWordCount++; strToken += strlen(strSearch); } if (strToken >= buffer + MAX_SENTENCE_LENGTH) {break;} strToken = strstr(strToken, strSearch); } } return (int) iWordCount; } Is it declared incorrectly? Jon

              S Offline
              S Offline
              Stober
              wrote on last edited by
              #6

              >>memcpy(buffer, Sentences.strSentence[iCurrent], MAX_SENTENCE_LENGTH); how is Sentences.strSentence declared? is it a char**? has memory been allocated or is your program just scribbling all over memory with that statement?

              J 1 Reply Last reply
              0
              • S Stober

                >>memcpy(buffer, Sentences.strSentence[iCurrent], MAX_SENTENCE_LENGTH); how is Sentences.strSentence declared? is it a char**? has memory been allocated or is your program just scribbling all over memory with that statement?

                J Offline
                J Offline
                jon 80
                wrote on last edited by
                #7

                :) it's a char... struct Sentence { char strSentence [MAX_LINES] [MAX_SENTENCE_LENGTH]; }; Jon

                1 Reply Last reply
                0
                • D David Crow

                  strstr() will return NULL if strSearch is not found in buffer. That would be why the while loop is not entered. You should be able to set a breakpoint on the strstr() statement to confirm this.


                  "Money talks. When my money starts to talk, I get a bill to shut it up." - Frank

                  "Judge not by the eye but by the heart." - Native American Proverb

                  J Offline
                  J Offline
                  jon 80
                  wrote on last edited by
                  #8

                  I meant to copy Sentences.strSentences to the buffer, look for the occurrence of strSearch within buffer and the pointer returned by strstr written to strToken. memcpy(buffer, Sentences.strSentence[iCurrent], MAX_SENTENCE_LENGTH); strToken = strstr(buffer,strSearch); while (strToken !=0 && *strToken != 0) { if (strlen(strToken) > 0) { iWordCount++; // The program looks for strSearch from the position where it was found onwards. strToken += strlen(strSearch); } if (strToken >= buffer + MAX_SENTENCE_LENGTH) {break;} strToken = strstr(strToken, strSearch); } I hope it is clear now. What I don't get is what is wrong with strToken? Jon

                  D 1 Reply Last reply
                  0
                  • J jon 80

                    I meant to copy Sentences.strSentences to the buffer, look for the occurrence of strSearch within buffer and the pointer returned by strstr written to strToken. memcpy(buffer, Sentences.strSentence[iCurrent], MAX_SENTENCE_LENGTH); strToken = strstr(buffer,strSearch); while (strToken !=0 && *strToken != 0) { if (strlen(strToken) > 0) { iWordCount++; // The program looks for strSearch from the position where it was found onwards. strToken += strlen(strSearch); } if (strToken >= buffer + MAX_SENTENCE_LENGTH) {break;} strToken = strstr(strToken, strSearch); } I hope it is clear now. What I don't get is what is wrong with strToken? Jon

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #9

                    jon_80 wrote:

                    strToken = strstr(buffer,strSearch);

                    What is the value of buffer and strSearch at this point?


                    "Money talks. When my money starts to talk, I get a bill to shut it up." - Frank

                    "Judge not by the eye but by the heart." - Native American Proverb

                    1 Reply Last reply
                    0
                    • Z Zac Howland

                      Break up the scope of your function: Function 1 will take a single sentence and count how many times a substring appears in it. Function 2 will take a collection of sentences and call Function 1 on each of them and sum the results.

                      const unsigned long MAX_SENTENCE_LENGTH = 100;
                      
                      unsigned long HowManyInSentence(const char* sentence, const char* substring)
                      {
                              char buffer[MAX_SENTENCE_LENGTH + 1] = {0};
                              memcpy(buffer, sentence, MAX_WORD_LENGTH);
                              char* token = 0;
                              unsigned long wordCount = 0;
                              token = strstr(buffer, substring);
                              while (token != 0 && *token != 0)
                              {
                                      if (strlen(token) > 0)
                                      {
                                              ++wordCount;
                                              token += strlen(substring);
                                              if (token >= buffer + MAX_WORD_LENGTH)
                                              {
                                                      break;
                                              }
                                      }
                                      token = strstr(token, substring);
                              }
                              return wordCount;
                      }
                      
                      unsigned long HowMany(const char* substring)
                      {
                      	// assume that g_sentences is declared somewhere and has MAX_SENTENCES entries
                      	unsigned long count = 0;
                      	for (int i = 0; i < MAX_SENTENCES; ++i)
                      	{
                      		count += HowManInSentence(g_sentences[i], substring);
                      	}
                      
                      	return count;
                      }
                      

                      Make sure that your buffer is null-terminated otherwise token will end up off in the weeds. Oh, and token doesn't need to be assigned with scope (that is, you don't need char* token = {0};). char* token = 0; is fine and more readable. -- modified at 12:58 Friday 4th August, 2006

                      If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                      J Offline
                      J Offline
                      jon 80
                      wrote on last edited by
                      #10

                      Thanks, but what do you mean by null-terminated? Also why this line of code? if (token >= buffer + MAX_WORD_LENGTH) { break; } Jon

                      D Z 2 Replies Last reply
                      0
                      • J jon 80

                        Thanks, but what do you mean by null-terminated? Also why this line of code? if (token >= buffer + MAX_WORD_LENGTH) { break; } Jon

                        D Offline
                        D Offline
                        David Crow
                        wrote on last edited by
                        #11

                        jon_80 wrote:

                        ...what do you mean by null-terminated?

                        Strings (not to be confused with an array of characters) in C must be nul-terminated in order to use string-related functions (e.g., strcpy(), strlen(), strstr(), strchr()).


                        "Money talks. When my money starts to talk, I get a bill to shut it up." - Frank

                        "Judge not by the eye but by the heart." - Native American Proverb

                        1 Reply Last reply
                        0
                        • J jon 80

                          Thanks, but what do you mean by null-terminated? Also why this line of code? if (token >= buffer + MAX_WORD_LENGTH) { break; } Jon

                          Z Offline
                          Z Offline
                          Zac Howland
                          wrote on last edited by
                          #12

                          jon_80 wrote:

                          but what do you mean by null-terminated?

                          Lets say I write the following: char* myString = "Hello, World!"; There are 13 characters in that string, but in memory, the size allocated for it would be 14. The reason is that the last character will be a 0, so the string actually looks like this in memory: Hello, World!\0. C-style strings require this, as do many C++ string wrappers.

                          if (token >= buffer + MAX_WORD_LENGTH)                        {                                
                          	break;
                          }
                          

                          The above section of code checks to make sure that the token (which is nothing more than a character pointer) is not pointing to something that is outside the buffer. buffer is an array of characters (with a size of MAX_WORD_LENGTH -- which actually should have been renamed to MAX_SENTENCE_LENGTH). Basically, if token has moved passed the end of the string, we stop.

                          If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups