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

  • Home
  • SEARCH
  • 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 6818245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:09:17+00:00 2026-05-26T21:09:17+00:00

I have a struct PACKET which contains two int fields. How would I declare

  • 0

I have a struct PACKET which contains two int fields. How would I declare a dynamic array of PACKETs? I will be adding to the array an unknown number of times in the main…that is why I need it to be dynamically sized.

  • 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-26T21:09:18+00:00Added an answer on May 26, 2026 at 9:09 pm

    Encapsulate your dynamic array implementation with malloc/realloc in a .h/.c file pair for easier use.

    static int length = 0;
    
    PACKET *dynamic_array_put(PACKET *list, const PACKET new) {
      PACKET *ret_val = NULL;
    
      ret_val = realloc(list, sizeof(PACKET) * (length + 1));
    
      if (ret_val == NULL) {
        /* Not enough memory */
        return NULL;
      }
    
      memmove(list + length, &new, sizeof(PACKET));
      length++;
    
      return ret_val;
    }
    
    PACKET *dynamic_array_get(PACKET *list, int index) {
      PACKET *p = NULL;
    
      p = malloc(sizeof(PACKET));
      if (p == NULL) {
        /* Not enough memory */
        return NULL;
      }
    
      memmove(p, list + index, sizeof(PACKET));
    
      return p;
    }
    
    PACKET *dynamic_array_remove(PACKET *list, int index) {
      PACKET *ret_val = NULL;
      PACKET tmp;
    
      memmove(&tmp, list + index, sizeof(PACKET));
      memmove(list + index, list + index + 1, 
              sizeof(PACKET) * (list - index - 1));
    
      ret_val = realloc(list, sizeof(PACKET) * (list - 1));
      if (ret_val == NULL) {
        /* Not enough memory, restore! */
        memmove(list + index + 1, list + index, 
                sizeof(PACKET) * (list - index - 1));
        memmove(list + index, &tmp, sizeof(PACKET));
        return NULL;
      }
    
      length--;
    
      return ret_val;
    }
    

    If memory space is no issue, you can do the memory allocation in blocks, instead of at every addition of PACKETs and speed up the average addition.

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

Sidebar

Related Questions

I have a struct struct Packet { int senderId; int sequenceNumber; char data[MaxDataSize]; char*
I have the following code int ParseData(unsigned char *packet, int len) { struct ethhdr
I'm using LLVM-clang on Linux. Suppose in foo.cpp I have: struct Foo { int
I have this struct: struct Map { public int Size; public Map ( int
i have made a module which aims to send the ping packet using ioctl
I have a program which transmits(broadcasts) and receives UDP packets simultaneously using pthreads. What
I have struct that store info about ARP packet and I need to save
I have a struct that's used in my internet application, which looks like: struct
I have a code which receives packets from the Ethernet and sends it via
I have a packet from a server which is parsed in an embedded system.

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.