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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:27:41+00:00 2026-06-13T20:27:41+00:00

I need to create binary data packets in C code. However, I am missing

  • 0

I need to create binary data packets in C code. However, I am missing some fundamental point. Currently, there are already structs defined and I will need to use them (at least for reading packet data). The same structure will be used both for received packets and for sent packets. Here are packet structs:

typedef struct {

  uint8_t packetType;
  uint16_t packetBody;

} MyStruct;

and another struct:

typedef union {

  const uint8_t *bytes; 
  MyStruct *packet;

} MyPacket;

Here’s the function that should take a pointer to the struct, fill it with data and return back:

void packetWithBytes(MyPacket *packet)
{
  packet->packet->packetType = 1; // crashed with EXC_BAD_ACCESS
  packet->packet->packetBody = 3;
}

Here’s a call to my function that should return a pointer to binary data:

MyPacket *packetRef;
packetWithBytes(packetRef); 

NSData *data = [NSData dataWithBytes:packetRef->bytes length:sizeof(packetRef->bytes)];

I feel I should allocate some space somewhere (with malloc) but not sure where and how to do that. The calling part code should not know any details about the size of the packet. Tried to do allocation but getting the same error:

void packetWithBytes(MyPacket *packet)
{
  packet->bytes = malloc(sizeof(uint8_t) + sizeof(uint16_t)); //crash
  packet->packet->packetType = 1;
  packet->packet->packetBody = 3;
}

Tried to remove const from MyStruct member bytes but getting the same error. I’m not not very familiar with C, so help would be very appreciated.

  • 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-13T20:27:42+00:00Added an answer on June 13, 2026 at 8:27 pm
    MyPacket *packetRef;
    packetWithBytes(packetRef);
    

    is the problem. packetRef is an uninitialized pointer and such it may point to an invalid memory location – dereferencing it is undefined behavior. You can solve this in two way:

    MyPacket *packetRef = malloc(sizeof(*packetRef));
    packetWithBytes(packetRef);
    

    or

    MyPacket packetRef;
    packetWithBytes(&packetRef);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a SAS dataset from a binary file. My code is:
I need to create a binary blob of empty data to PInvoke a native
I need to create a hash map which will hold 528 bit binary data.
I need to create a binary bitmap from a closed 2D polygon represented as
I've already created the Binary Tree and Linked list classes, I'm just in need
Related Question: vector <unsigned char> vs string for binary data . My code uses
Concrete use case: There is an abstraction for binary data, which is widely used
I want to Use Some Java Code From the WCF Service. Basically I need
I have a simulation that reads large binary data files that we create (10s
I wan't to create DataSet from code and set it as data source for

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.