Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3438398
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:10:59+00:00 2026-05-18T08:10:59+00:00

My application is crashing when I try printing the buffer. Otherwise, it works fine.

  • 0

My application is crashing when I try printing the buffer. Otherwise, it works fine.
This is the code:

irc.h

  class IRC
            {
              public:
                      void sockconnect(char * hName, int portNum);
                          void sockwrite(char* sendbuf);
                          char sockread(void);
                          bool connected;
                  private:
                          WSADATA wsaData;
                          SOCKET m_socket;
                          sockaddr_in clientService;
                          LPHOSTENT hostEntry;

};

irc.cc

char IRC::sockread(void)

  {
    int result;
    char buffer[DEFAULT_BUFLEN];
        result = recv(m_socket, buffer, DEFAULT_BUFLEN, 0);

        if (result > 0) {
             return *buffer;
              }
          else if (result == 0)
              {
             connected = false;
                 return *buffer;
              }
          else {
         printf("recv failed with error: %d\n", WSAGetLastError());
         return *buffer;
        }

   }

main.cc

 IRC client;

 while (client.connected == true) {
     char buffer = client.sockread();
         if (buffer == NULL)
           break;


        printf ("Buffer: %s\n",buffer);
       }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T08:11:00+00:00Added an answer on May 18, 2026 at 8:11 am

    If you want to print the first character use

    printf ("Buffer: %c\n",buffer);
    

    If you want to print the whole then sockread should return the whole buffer, not the first character. For that you will need to return the address of the first element of the buffer which in this case should already be dynamically allocated.

    printf ("Buffer: %s\n",buffer);
    

    Edit After thinking I think you want the latter for that change the sockread() function in the following way:

    • change the return type from char to char* or better const char*
    • char buffer[DEFAULT_BUFLEN]; to char* buffer = new char[DEFAULT_BUFLEN];
    • return *buffer to return buffer

    Also, in this case don’t forget to delete the buffer

    const char* buffer = client.sockread(); //not char buffer as in your code
    printf ("Buffer: %s\n",buffer);
    delete [] buffer;
    

    hth

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The title explains all... I have this snippet of code in my application: String
I've got this trivial class hierarchy: class Base { public: virtual int x( )
My application is crashing when it exits the thread function. This is how my
I just came across with a problem using XmlDocument.LoadXml . The application was crashing,
I have a WPF application that's crashing once I get it onto machines that
My application was rejected because it is crashing on iPhone 3G running iPhone OS
I've got (the currently latest) jdk 1.6.0.18 crashing while running a web application on
Application has an auxiliary thread. This thread is not meant to run all the
The setup I have is I'm using a Java application to call native C-code
my application is running fine in simulator...but not on real device....and i have jailbroken

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.