Thread: Allocation problems

  1. #1
    Registered User
    Join Date
    Oct 2005

    Allocation problems

    INFO : char* msg;
                    SocketClient c(hostname, port);
    		string ret = c.ReceiveLine(); //Receives a string where its length is 22
    		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());
    			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';
    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
    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > 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.

  4. #4
    Registered User
    Join Date
    Oct 2005
    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, 06: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, 05:10 PM
  5. problems with memory allocation
    By Waldo2k2 in forum C++ Programming
    Replies: 12
    Last Post: 07-16-2002, 01:08 PM