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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:40:34+00:00 2026-05-18T01:40:34+00:00

In C can you have realloc inside realloc? For example, a struct inside a

  • 0

In C can you have realloc inside realloc? For example, a struct inside a struct when you need to malloc both of them and realloc both of them. If yes can someone please provide a simple example?
Thank you in advance.

  • 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-18T01:40:35+00:00Added an answer on May 18, 2026 at 1:40 am

    Your question is not dreadfully clear, but…

    Yes, a given dynamically allocated structure (for example, an array of structures) can itself contain pointers to allocated data (such as various other arrays of allocated structures), and you can reallocate the various parts independently.

    However, the system will not call realloc() for you while you are reallocating one of the structures; you would have to separately program the various resizing operations.

    Example nested data structures:

    struct line { char *info; size_t length; };
    
    struct section { size_t num_lines; struct line *lines; };
    

    You could allocate an array of sections, and reallocate that array when needed. Each section contains an array of lines, and each of those arrays of lines can be independently reallocated too.


    Hence:

    size_t num_sections = 0;
    size_t max_sections = 0;
    struct section *sections = 0;
    
    if (num_sections == max_sections)
    {
         size_t new_max = (max_sections + 1) * 2;
         struct section *new_sections;
         if (sections == 0)
             new_sections = malloc(new_max * sizeof(*new_sections));
         else
             new_sections = realloc(sections, new_max * sizeof(*new_sections));
         if (new_sections == 0)
             ...out of memory error...
         sections = new_sections;
         max_sections = new_max;
     }
     struct section *section = &sections[num_sections++];  // Newly available section
     section->num_lines = 0;
     section->lines = 0;
     return section;
    

    (I’m assuming C99 – with variable declarations where I want them.)

    A similar process applies for the array of lines within a section, except the section structure doesn’t have separate values for the number of allocated lines and the number of lines actually in use. Each line also has its own allocated memory for the string of characters, of course…

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

Sidebar

Related Questions

You can have different naming convention for class members, static objects, global objects, and
Routines can have parameters, that's no news. You can define as many parameters as
I have a program that can have a lot of parameters (we have over
I have a class Person which can have several Homes, each one with one
A single Biztalk Server can have multiple Host processes. Is it possible to create
Each of my clients can have many todo items and every todo item has
I have a Windows Forms (.NET) application that can have multiple documents open simultaneously.
A PHP array can have arrays for its elements. And those arrays can have
Contributing to open source can have many forms: working with issue trackers, patches, further
I have a web application with users and their documents. Each user can have

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.