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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:28:39+00:00 2026-05-23T05:28:39+00:00

I am working on network programming using epoll and I have this code. int

  • 0

I am working on network programming using epoll and I have this code.

            int str_len = read(m_events[i].data.fd, buf, BUF_SIZE);
            printf("read %d \n", str_len);

            if(str_len == 0){
                if(!removeClient(m_events[i].data.fd))
                    break;
                close(m_events[i].data.fd);

            }
            else {
                printf("ServerManager::eventAcceptLoop, A message has been received \n");
                pushWork(buf);
            }

the buf is declared like this

buf[BUF_SIZE];

and I want to pass the data in the buf to the functiion “pushWork”

 pushWork(char * pdata){
      hand this pdata to the thread pool to parse and handle it
 }

I think I need to copy the data in the buf instead of pointing to the buf because it will be overriden whenever a new data comes in. Right now I do something like

 char *pdata = buf;
 pushWork(pdata)

Should I do memcopy or something else? and flush the buf?
Please let me know how I can handle this. Thanks in advance…

  • 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-23T05:28:40+00:00Added an answer on May 23, 2026 at 5:28 am

    Yes, you can copy the data:

    char* pdata = new char[str_len];
    memcpy(pdata, buf, str_len);
    pushWork(pdata);
    

    At the pushWork end, it has to take care of freeing the data when it’s done:

    delete[] pdata;
    

    You will also want to consider encapsulating this all into a std::vector so you don’t have to worry about exception safety:

    std::vector<char> data(buf, buf + str_len);
    pushWork(data);
    

    In this case, pushWork needs to be modified to take a std::vector by value, but the upside is that it won’t have to worry about deleting the data when done, and will be fully exception-safe.

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

Sidebar

Related Questions

I am working on network programming using epoll and I have this code... int
I am working on a network programming and I have this code void WorkHandler::workLoop(){
I am working on a network programming using epoll. I was wondering how I
I am working on a network programming and I have seen people using vector
I'm working on a loosely coupled cluster for some data processing. The network code
I am working on a network programming on linux machine using C++ and I
I am working on a network programming and I need a data structure to
I am working on a network programming and I have created a thread pool.
I am working a network programming using eclipse CDT. It worked fine before if
So, I'm working on some network programming in C, and it would seem that

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.