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 8826525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:09:30+00:00 2026-06-14T07:09:30+00:00

EDIT : I changed the title because it suited not nearly the problem, as

  • 0

EDIT: I changed the title because it suited not nearly the problem, as the new operator was not the problem. The formerly title was “Can operator new fail?”

In the code below, before a client connects to the server I create a new Socket object pointer. When a client connects I create the object with new. But somehow when I step through the code with the debugger (Eclipse CDT, g++ Ubuntu/Linaro 4.6.3-1ubuntu5) I see that after the call of the new operator the pointer ist still NULL.

class Socket
{
public:
    Socket( int domain, int type, int protocol = 0 );
    ~Socket();
    [...]
    int accept( Socket * socket );
    [...]
private:
    Socket();
    int mSocketDescriptor;
    int mNetworkProtocol;
    int mTransportProtocol;
};

[...]

int Socket::accept( Socket * socket )
{
    // Accept one connection (blocking)
    struct sockaddr_in cli_addr;
    socklen_t clilen = sizeof(cli_addr);
    int ret = ::accept(mSocketDescriptor, (struct sockaddr *) &cli_addr, &clilen);
    if ( ret >= 0 ){
        socket = new Socket();  // <- Here's the problem, socket remains NULL
        socket->mSocketDescriptor  = ret;
        socket->mNetworkProtocol   = this->mNetworkProtocol;
        socket->mTransportProtocol = this->mTransportProtocol;
    }
    return ret;
}

Mainloop:

// Accept all incoming connections in a loop
while(true){

    // Accept one connection (blocking)
    net::Socket * newConn = NULL;
    if (socket.accept(newConn) < 0){
        perror("accept()");
        exit(EXIT_FAILURE);}

    // Create a new thread that is talking to the client
    pthread_t nThreadID;
    pthread_create(&nThreadID, NULL, ClientMainThreadProc, newConn);
}

I read through the C++ reference. It tells me that a bad_alloc exception should araise if the new fails. But that is not the fact so I have no idea whats going wrong. Any suggestions?

  • 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-06-14T07:09:32+00:00Added an answer on June 14, 2026 at 7:09 am

    Okay the solution to the problem is, as hmjd stated:

    I think you mean newConn is still NULL in the caller? If so, pass the pointer by reference as a copy of the pointer is being passed to accept() so any change is local to the accept() function.

    Unfortunately my debugger steered me in the wrong direction, showing the socket pointer note even change locally. But when pass the pointer by reference everthing works fine.

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

Sidebar

Related Questions

EDIT: changed the title to fit the code below. I'm trying to retrieve a
Edit : I changed the title because the issue was not what I initially
EDIT#2 I changed the Title name because it no longer represented the question I
(Someone edit the title if you can understand and define my problem better.) The
EDIT:I've changed the title, because the issue had nothing to do with IE image.load()
I had trouble coming up with a title for this question because I'm not
EDIT : Changed title from inheritance to composition. Left body of question unchanged. I'm
(edit) current behavior is no tooltip showing on hover. not the default html title
Edit: Changed the title. I'm less interested in the two segments being the same,
[Edit] changed return 0 to return . Side effects of beinga Python n00b. :)

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.