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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:15:20+00:00 2026-06-12T21:15:20+00:00

I have a structure typedef struct foo { int lengthOfArray1; int lengthOfArray2; int* array1;

  • 0

I have a structure

typedef struct foo {
    int lengthOfArray1;
    int lengthOfArray2;
    int* array1;
    int* array2;
} foo;

I need to allocate enough memory for the entire structure and its array’s contents. So assuming each array had a length of 5…

foo* bar = (foo*)malloc(sizeof(foo) + (sizeof(int) * 5) + (sizeof(int) * 5));

I now have to point array1 and array2 to the correct location in that allocated buffer:

bar->array1 = (int*)(&bar->lengthOfArray2 + sizeof(int));
bar->array2 = (int*)(bar->array1 + lengthOfArray2);

Is this correct?

Edit #1

Just to clear up any confusion: I am trying to keep the memory in one block, and not three.

Edit #2

I cannot use C99 as the MSVC 2010 compiler does not support it (http://stackoverflow.com/questions/6688895/does-microsoft-visual-studio-2010-supports-c99).

  • 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-12T21:15:22+00:00Added an answer on June 12, 2026 at 9:15 pm

    Following the OP’s approach this should do the job:

    /* Defining these types allows to change the types without having the need to modify the code. */
    typedef int Foo_ArrayElement1_t;
    typedef int Foo_ArrayElement2_t;
    
    typedef struct Foo_s {
        size_t lengthOfArray1; /* 'size_t' is the type of choice for array/memory dimensions. */
        size_t lengthOfArray2;
        Foo_ArrayElement1_t * array1;
        Foo_ArrayElement2_t * array2;
    } Foo_t;
    
    /*
     * Allocates memory to hold a structure of type Foo_t including size for 
     * 's1' elements referenced by 'array1' and 's2' elements by 'array2'.
     * The arrays' elements are set to 0.
     *
     * Returns a pointer to the freshly allocated memory or NULL if the memory could not 
     * be allocated.
     */
    Foo_t * Foo_CreateAndInit(size_t s1, size_t s2)
    {
      /* At once allocate all 'Foo_t' (including the memory Foo_t's array pointers shall point to). */
      Foo_t * pfoo = calloc(1,
          sizeof(*pfoo) +
          s1 * sizeof(*(pfoo->array1) + 
          s2 * sizeof(*(pfoo->array2)));
      if (pfoo)
      {
        pfoo->lengthOfArray1 = s1;
        pfoo->lengthOfArray2 = s2;
    
        /* The first array starts right after foo. */
        pfoo->array1 = (Foo_ArrayElement1_t *) (pfoo + 1); 
    
        /* The second array starts right after s1 elements of where the first array starts. */
        pfoo->array2 = (Foo_ArrayElement2_t *) (pfoo->array1 + s1); /* That casting here is not 
            necessaryas long as 'Foo_t.array1' and 'Foo_t.array2' point to the same type but makes 
            the code work even if those types were changed to be different. */
      }
    
      return pfoo;
    }
    
    ...
    
    Foo_t * foo = Foo_CreateAndInit(5, 5);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a structure typedef struct store { char name[11]; int age; } store;
Let's say I have a first structure like this: typedef struct { int ivalue;
I have a structure typedef struct myStruct_st { int a; }myStruct; It can be
I have a structure such as typedef struct FT_Bitmap_ { int rows; int width;
I have a structure to represent strings in memory looking like this: typedef struct
I have this C code : #include<stdio.h> typedef struct { int foo; } MyStruct;
I have the structure typedef struct EData { int a; char c; } Edata
So I have a structure of unknown size as follows: typedef struct a{ int
I have this structure: typedef struct { int data[10]; } small_structure; and this code:
I have this C code for a Point structure: typedef struct Point{ int x,

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.