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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:32:44+00:00 2026-05-27T15:32:44+00:00

I have a structure , which present my element of data struct myElement {

  • 0

I have a structure , which present my element of data

struct myElement
{
    int field1;
    int field2;
    int field3;    
};

another structure, which contain array of this elements and some another data

struct myArray 
{
    struct myElement *elements;
    int someData;
};

and I need to have array of this arrays like that

struct myArray *myMatrix;

But I have a problem with memory allocation. Count of elements in myArray’s can be different, in myMatrix too, so I need to allocate memory dynamicaly. What is the corret way to allocate and deallocate memory in this situation?

  • 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-27T15:32:45+00:00Added an answer on May 27, 2026 at 3:32 pm

    Here’s a small example of how you would allocate (malloc) and deallocate (free) a dynamic struct myElement array in a struct myArray. Note also that you will need to keep track of the size of the array, so I added size_t elements_len; to struct myArray (excuse the combination of camelCase and underscores – I use underscores in C, but didn’t want to modify your identifiers):

    #include <stdlib.h>
    
    struct myElement
    {
        int field1;
        int field2;
        int field3;    
    };
    
    struct myArray 
    {
        struct myElement *elements;
        size_t elements_len;
        int someData;
    };
    
    void allocate_elements(struct myArray *m, size_t length)
    {
       m->elements = malloc(length * sizeof( *m->elements) );
       m->elements_len = length;
    }
    
    void free_elements(struct myArray *m)
    {
       free(m->elements);
       m->elements = NULL;  /* point to NULL to signify no array allocated */
       m->elements_len = 0; /* length of 0 also signifies no elements/no array */
    }
    
    int main(void)
    {
       struct myArray ma;
    
       allocate_elements(&ma, 5);
       free_elements(&ma);
    
       return 0;
    }
    

    Apply similar logic in order to have a dynamic array of struct myArray. You would malloc enough memory for X amount of struct myArrays, then for each struct myArray element in that array, you would call allocate_elements. Then iterate through each element in the array once you’re done with it and call free_elements.

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

Sidebar

Related Questions

I have a data structure which uses composite ids (Which I dont wish to
I have a data structure which is as given below: class File { public
Guys, I have a data structure which has 25 distinct keys (integer) and a
I have a table in database which has following structure(data) : +--------------------------+ Organization +--------------------------+
I have a data structure, for which I am currently using an ArrayList .
I have a structure which I need to populate and write to disk (several
I have a structure which I create a custom constructor to initialize the members
I have a structure which has 3 identifier fields and one value field. I
Iam struggling with NHibernate and its lazyload. I have a structure which I simplified
I have a C code in which I have a structure declaration which has

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.