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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:09:11+00:00 2026-05-30T04:09:11+00:00

When receiving an incoming msessage I receive a correct result when my packet structure

  • 0

When receiving an incoming msessage I receive a correct result when my packet structure has a fixed size array in it, like this

typedef struct inPacket{
    int cmd;
    int seqNo;
    int numbers[100];
} inPacket;

then I call recv like so

char buf[1024];
recv(m_socket, buf, sizeof(buf));

and cast the char array to the packet type

inPacket* p = (inPacket*)buf;

This works fine, I have a full array of ints in the ‘numbers’ field after casting. I cant think why the below doesnt work, when passing in an address at which I want to store the numbers

void func(int* out)
{
    inPacket inpacket;
    inPacket.numbers = out;
    recv(m_socket, (char*)&inpacket, sizeof(inPacket));
}

the ‘numbers’ array is a bad pointer after the call to recv.

The array passed in is allocated by the calling function

int numbers[10];
func(numbers);
  • 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-30T04:09:12+00:00Added an answer on May 30, 2026 at 4:09 am

    What recv does is, it copies data from an internal buffer into the one you supply. So it overwrites the contents of your structure. You also change the array member to some other pointer, which is a no-no. And not only that, you try to set it to an array that is smaller than the existing array, which can lead to recv possibly overwrite memory outside your new array.

    You need to do something like this:

    void func(int* out, int maxnum)
    {
        inPacket inpacket;
        recv(m_socket, (char*)&inpacket, sizeof(inPacket));
    
        memcpy(out, inpacket.numbers, sizeof(int) * maxnum);
    }
    

    Then you call the function like this:

    int numbers[10];
    func(numbers, 10);
    

    Edit: If a dynamic array is wanted, then the call to recv have to be modified, along with the structure:

    typedef struct inPacket{
        int cmd;
        int seqNo;
        int numbers[0];
    } inPacket;
    
    void func(int* out, int maxnum)
    {
        size_t packetSize = sizeof(inPacket) + sizeof(int) * maxnum;
        inPacket *inpacket = malloc(packetSize);
    
        recv(m_socket, inpacket, packetSize);
    
        memcpy(out, inpacket->numbers, sizeof(int) * maxnum);
    
        free(inpacket);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently receiving this error: Validation of viewstate MAC failed. If this application
I'm receiving suddenly this error on a Win2003 Server Web Application: Microsoft VBScript runtime
I have setup a postfix mail receiving server. On this I am using DKIM
I have implemented a postfix mail receiving server. On this server I am receiving
I know Biztalk has a POP3 adapter for receiving/processing email, but our network group
I need to receive incoming emails as multipart-formdata via a POST request from Cloudmailin.
When receiving a bug report or an it-doesnt-work message one of my initials questions
I'm receiving Package Load Failure error when I open VS 2005 after I installed
I'm receiving feedback from a developer that The only way visual basic (6) can
I am receiving a 3rd party feed of which I cannot be certain of

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.