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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:55:57+00:00 2026-06-11T13:55:57+00:00

I am doing a application witch uses sockets so I am holding in an

  • 0

I am doing a application witch uses sockets so I am holding in an array the sockets handles.I have the following code:

while(0 == 0){
    int * tx = (int*)(malloc((nr_con + 2) * sizeof(int)));
    if (conexiuni != NULL)
    {
        syslog(LOG_NOTICE,"Ajung la eliberare %d",nr_con);
        memcpy(&tx[0],&conexiuni[0],(sizeof(int) * (nr_con)));
        syslog(LOG_NOTICE,"Ajung la eliberare %d",nr_con);
        free(conexiuni);
    }
    conexiuni = tx;

    syslog(LOG_NOTICE,"Ajung la mama %d",nr_con);
    //The line bellow causes a segfault at second connection
    if ((conexiuni[nr_con] = accept(hsock,(sockaddr*)(&sadr),&addr_size)) != -1)
    {
        nr_con++;
        syslog(LOG_NOTICE,"Primesc de la %s",inet_ntoa(sadr.sin_addr));
        syslog(LOG_NOTICE,"kkt %d",conexiuni[nr_con - 1]);
        int * sz = (int*)malloc(sizeof(int));
        *sz = conexiuni[nr_con - 1];
        syslog(LOG_NOTICE,"after %d",*sz);
        pthread_create(&tidi,0,&ConexiuniHandler, sz);
    }
}

When I connect the second time when I assign the array the program crashes. What am I doing wrong? I tried the same code on Windows and it works well but on Linux it crashes.

  • 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-11T13:55:58+00:00Added an answer on June 11, 2026 at 1:55 pm

    I assume that what you are wanting to do is to have a server that is accepting connections and then as the connections are accepted, you start a thread to handle that connection request. So each time you do an accept you are wanting to start up a thread and give it the socket handle. You are also keeping up with all of the socket handles in an array which is dynamically increased as you accept new connection requests.

    Following is a suggested method. I have not done any testing nor have I even compiled this code segment however it is a place to start. One thing that I am doing is increasing the array of socket handles by blocks of 16 each time I do a resize of the array. I am doing this because it can make the job of the memory manager a bit easier and reduce the amount of fragmentation by reducing the number of calls to malloc().

    int nr_con = 0;      // we start off with no connections
    int block_con = 16;  // number of blocks to allocate each time we increase the array
    SOCKET  *conexiuni = malloc ((nr_con + block_con) * sizeof(SOCKET));
    while(1) {
        syslog (LOG_NOTICE, "Ajung la mama %d", nr_con);
    
        // wait for a connection request to come in.  if it does, log the request
        // then create a thread to handle the request providing the socket to the thread.
        // we are keeping an array of the sockets that is dynamically increased so
        // we will allocate blocks of 16 at a time as we lengthen the array.
        if ((conexiuni[nr_con] = accept (hsock, (sockaddr*)(&sadr), &addr_size)) != -1)
        {
            block_con--;
            if (block_con < 1) {
            {
                // so lets add another block to our array by allocating the memory
                // then copying the current array to the new memory area then freeing
                // the old memory area which is no longer needed.
                block_con = 16;
                SOCKET *pTemp = malloc(nr_con + block_con) * sizeof(SOCKET));
                syslog (LOG_NOTICE, "Ajung la eliberare %d", nr_con);
                memcpy (pTemp, conexiuni, (sizeof(SOCKET) * (nr_con + 1)));
                syslog (LOG_NOTICE, "Ajung la eliberare %d", nr_con);
                free (conexiuni);
                conexiuni = pTemp;
            }
            syslog (LOG_NOTICE, "Primesc de la %s", inet_ntoa(sadr.sin_addr));
            syslog (LOG_NOTICE, "kkt %d", conexiuni[nr_con]);
    
            SOCKET  *sz = conexiumi + nr_con;
            syslog (LOG_NOTICE, "after %d", *sz);
    
            // start the thread which is to service this connection request.
            pthread_create (&tidi, 0, &ConexiuniHandler, sz);
            nr_con++;
        }
    }
    

    However there are a few issues with something like this. First of all in the example above I am not handling an out of memory error should malloc() return a NULL pointer due to being unable to provide the memory request.

    The second issue is the possibility that the thread will not access the pointer to the socket before the array is dynamically extended rendering the pointer provided invalid since it was freed during the dynamic reallocation. So if you have lots of connections coming in quickly, this may be a problem. At a minimum the first thing the thread should do is make a local copy of the socket handle.

    Another question is how are you going to go back over the array to determine which sockets are still valid and open and which are stale with the connection closed. Do you just keep dynamically allocating space as connection requests come in until you run out of memory after a few days of your server being up and running?

    Rather than using int, you really should be using SOCKET since that it the actual data type. I realize that in most cases, a SOCKET is actually an int, however it is usually better to be precise in these matters.

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

Sidebar

Related Questions

I have a straightforward question. I am doing a web application that uses MySQL,
I'm doing an asp.net application with one page. In this page, I have one
I have a web application that will be doing some processing with submitted data.
I am doing some work on an application that uses an existing schema that
My application uses Django non-rel . I don't have access to model. I have
Application uses Entity Framework 4.1 with database first approach. I have in database a
I have developed an application which uses the UI Automation managed library. The performance
We have a winforms application that is deployed to users through clickonce and uses
I have a CI application that uses .htaccess for URL routing. My basic setup
I have a Unicode Win32 application that uses 3rd party libraries, some of which

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.