Allocation problems

This is a discussion on Allocation problems within the C++ Programming forums, part of the General Programming Boards category; Code: /* INFO : char* msg; */ SocketClient c(hostname, port); string ret = c.ReceiveLine(); //Receives a string where its length ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220

    Allocation problems

    Code:
    /*
    INFO : char* msg;
    */
                    SocketClient c(hostname, port);
    		string ret = c.ReceiveLine(); //Receives a string where its length is 22
    		c.SendLine("EHLO");
    		c.SendLine("AUTH PLAIN");
    
    		char s = '\0';
    		ret = username;
    		ret += s;
    		ret += username;
    		ret += s;
    		ret += password;
    
    		msg = new char(ret.length()+1);
    
    		Crypt::Base64Encode((unsigned char *)msg, 
    				(const unsigned char*)ret.c_str(), ret.length());
    		try
    		{
    			c.SendLine(msg); //First memory error
    		}
    		catch (std::bad_alloc ex)
    		{
    			cout<<"error\n"<<ex.what(); //yes, we got some bad allocation error
    		}
    		ret = c.ReceiveLine(); //Second memory error
    
    ....
    
    std::string Socket::ReceiveLine() {
      std::string ret;
       while (1) {
         char r;
    
         switch(recv(s_, &r, 1, 0)) {
           case 0: // not connected anymore;
             return "";
           case -1:
              /*if (errno == EAGAIN) {
                 return ret;
              } else {
                // not connected anymore*/
               return "";
             //}
         }
    
         ret += r;
         if (r == '\n')  return ret;
       }
    }
    
    ...
    
    void Socket::SendLine(std::string s) {
      s += '\n';
      send(s_,s.c_str(),s.length(),0);
    }
    First memory error: HEAP[xchar.exe]: HEAP: Free Heap block 8a6800 modified at 8a6890 after it was freed
    Second memory error: Unhandled exception at 0x7c812a5b in xchar.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0012eee0

    OS = Windows XP Professional SP2
    Compiler = Visual Studio 2005

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    msg is a character array. Apparently you use Crypt::Base64Encode to convert the ret string and store it in msg. At that point ret has embedded nulls, so presumably afterwards msg has embedded nulls. Then you send msg to SendLine, which accepts a string. The character array is converted to a string by searching for the first null and using it as the terminating character, so everything after the first embedded null is lost.

    I don't see why this would cause the errors you mention, but maybe re-thinking that will help.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,338
    > msg = new char(ret.length()+1);
    Try using [ ] rather than ( )
    This just allocates a single char, and initialises it to whatever that value is.

    You wanted an array of chars.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Location
    Brasil
    Posts
    220
    problem solved, salem was right, thank you all!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. redundant allocation
    By George2 in forum C++ Programming
    Replies: 22
    Last Post: 03-06-2008, 05:43 PM
  3. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 05:04 PM
  4. Dynamic allocation (I thought it would crash)
    By Baaaah! in forum C Programming
    Replies: 16
    Last Post: 11-30-2005, 04:10 PM
  5. problems with memory allocation
    By Waldo2k2 in forum C++ Programming
    Replies: 12
    Last Post: 07-16-2002, 01:08 PM

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