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. delete [] problem, and extra char(s) showing up in substring?

delete [] problem, and extra char(s) showing up in substring?

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
7 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.
  • M Offline
    M Offline
    Mike the Red
    wrote on last edited by
    #1

    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: 3

    split[0] = file=abcdefg
    split[1] = info=_[extended_char]_3
    split[2] = x=3
    split[3] = 4=5

    Try 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

    CPalliniC D C 3 Replies Last reply
    0
    • M Mike the Red

      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: 3

      split[0] = file=abcdefg
      split[1] = info=_[extended_char]_3
      split[2] = x=3
      split[3] = 4=5

      Try 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

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      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]

      In testa che avete, signor di Ceprano?

      M 1 Reply Last reply
      0
      • CPalliniC CPallini

        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]

        M Offline
        M Offline
        Mike the Red
        wrote on last edited by
        #3

        -nt-

        CPalliniC 1 Reply Last reply
        0
        • M Mike the Red

          -nt-

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          :)

          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]

          In testa che avete, signor di Ceprano?

          1 Reply Last reply
          0
          • M Mike the Red

            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: 3

            split[0] = file=abcdefg
            split[1] = info=_[extended_char]_3
            split[2] = x=3
            split[3] = 4=5

            Try 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

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

            Mike 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

            1 Reply Last reply
            0
            • M Mike the Red

              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: 3

              split[0] = file=abcdefg
              split[1] = info=_[extended_char]_3
              split[2] = x=3
              split[3] = 4=5

              Try 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

              C Offline
              C Offline
              Cedric Moonen
              wrote on last edited by
              #6

              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++

              M 1 Reply Last reply
              0
              • C Cedric Moonen

                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++

                M Offline
                M Offline
                Mike the Red
                wrote on last edited by
                #7

                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

                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