String overwriting memory?

This is a discussion on String overwriting memory? within the C++ Programming forums, part of the General Programming Boards category; This is the function causing trouble, it seems to overwrite some part of the memory... Code: #include <iostream> #include <string> ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    12

    Question String overwriting memory?

    This is the function causing trouble,
    it seems to overwrite some part of the memory...


    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    int _replace (string to_place, string be_placed);
    int main()
    {
    string s1= "thisisastring";
    string s2= "thisisanother";
    _replace(s1,s2);
    //s2 is always longer than s1
    return 0;
    }
    int _replace(string to_place,string be_placed)
    {
        const unsigned int i=be_placed.size();
        const unsigned int j=to_place.size();
    	const unsigned int diff= i-j-2;
    	
    	
    	for(unsigned int n=diff;n-diff<to_place.size();n++)   
    	{
    		be_placed[n]=to_place[n-diff];
    		
    	}
    
        /*for(i-=1,j-=1;i>0&&j>0;i--,j--)
        {
             be_placed[i]=to_place[i];
             if(be_placed[i]=' ') break;
    		 cout<<be_placed[i]<<to_place[i];
         }*/  
         cout<<be_placed<<endl;
         return 0;  
     }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >//s2 is always longer than s1

    >string s1= "thisisastring";
    >string s2= "thisisanother";

    Both strings are the same length. Perhaps you meant to say:
    //s2 is at least as long as s1

    > const unsigned int diff= i-j-2;
    Using your example, both strings are the same length, so diff would start out -2, which is not a valid index.

    You didn't explain the purpose of your function, so I'm guessing this should be:
    const unsigned int diff= i-j;

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >_replace
    This invades the implementation's namespace. Leading underscores are reserved for internal implementation names. You also need to be more specific about what you're trying to do with this function. The name suggests that you want to replace the contents of s1 with the contents of s2, an operation that is supported in a number of ways by the string class to begin with. Here is one of them:
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    void replace_string ( string& to_place, const string& be_placed );
    
    int main()
    {
      string s1 = "thisisastring";
      string s2 = "thisisanother";
    
      cout<< s1 <<" -- "<< s2 <<endl;
      replace_string ( s1, s2 );
      cout<< s1 <<" -- "<< s2 <<endl;
    
      return 0;
    }
    
    void replace_string ( string& to_place, const string& be_placed )
    {
      to_place.assign ( be_placed );
    }
    On a side note, the whole diff thing is just going to confuse the matter. If you're worried about walking outside of s1's boundaries (a valid concern!), why not just do something like this if you feel the need to perform the replacement manually:
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    void replace_string ( string& to_place, const string& be_placed );
    
    int main()
    {
      string s1 = "thisisastring";
      string s2 = "thisisanothertest";
    
      cout<< s1 <<" -- "<< s2 <<endl;
      replace_string ( s1, s2 );
      cout<< s1 <<" -- "<< s2 <<endl;
    
      return 0;
    }
    
    void replace_string ( string& to_place, const string& be_placed )
    {
      const string::size_type end = to_place.size();
    
      for ( string::size_type i = 0; i != end; ++i )
        to_place[i] = be_placed[i];
    }
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 08:54 AM
  2. Replies: 8
    Last Post: 04-25-2008, 03:45 PM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 10:11 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21