Thread: Pointers misbehaving

  1. #1
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212

    Unhappy Pointers misbehaving

    this function in my TCP/IP wrapper class reads from the socket into the char pointer bufm byte at a time until it reads the string "end". I can verify that it actually does this using printf right before the return 0...
    (continued below)
    Code:
    int TcpIp::readUntil(char *buf, int bufsiz, char *end)
    {
      char tbuf[1];
      int br = 0;
      
      tbuf[1] = '\0';
      buf[0] = '\0';
      
      while (recv(connection, tbuf, 1, 0) != SOCKET_ERROR)
      {
        strcat(buf, tbuf);
        
        if (strstr(buf,end))
        {
          return 0;
        }
        
        br ++;
        if (br == bufsiz)
        {
          return 1;
        }
      }
      return 2;
    }
    but when i call this functon from main:
    Code:
      TcpIp connection;
      char buffer[256];
      
      if (!connection.conn("127.0.0.1",555))
      {
        if (!connection.readUntil(buffer, 256, "\n\n"))
        {
          cout << buffer;
        }
      }
      delete &connection;
    the buffer is filled with garbage, even if the function itself proves the pointer has the received string in it.

    Bloody hell.

  2. #2
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Originally posted by Salem
    > char tbuf[1];
    Should be
    char tbuf[2];
    to hold a char, and a \0

    > if (!connection.readUntil(buffer, 256, "\n\n"))
    This is a poor way of testing the return result of a non-bool function returning 3 possible values
    ok thanks for that, all fixed now. two dumbass mistakes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM