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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:36:45+00:00 2026-06-05T14:36:45+00:00

I am writing a C based library and I’m a little confused when it

  • 0

I am writing a C based library and I’m a little confused when it comes to using free() to deallocate blocks of memory…
Essentially I have several similar structs defined this way:

typedef struct
{
    pthread_t thread_id;
    pthread_attr_t attr;
    void     *data;
    size_t stacksize = NULL;
} thread_info;

Where I essentially use realloc() to allocate memory for this structure and have an array point to it.

My question is, if I use:

free(my_array[thread_index]); 

Would the free() call deallocate memory used not just by the structure but all its data types within it, ie. the *thread_id*, attr, data, and stacksize will be deallocated as well or do I have to deallocate them individually then deallocate the structure from the array.

To me it would make sense that if I am using free() on such a structure all data contained within it will be deallocated and I wouldn’t have to explicitly deallocate each struct attribute but I just want to make sure if that is the case.

  • 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-05T14:36:47+00:00Added an answer on June 5, 2026 at 2:36 pm

    For dynamically allocated “structures” (i.e. heirarchies of array’s, struct’s, etc), you need to call free on each pointer from “the bottom up”. You can think of this operation as performing a depth-first traversal of a tree.

    The usual restrictions apply to variables that are pointers to memory on the stack, rather than the heap. For example, variables defined with type[n], char[n] = 'abc', etc vs. allocations from the heap using the *alloc family of functions. Also beware of cases where there are multiple pointers to the same address, calling free twice on these is a programming error (though not necessarily a segmentation fault).

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

Sidebar

Related Questions

I'm writing a Qt based (QtCore) C++ library and would like to have access
I'm writing a .NET wrapper around an old MFC-based library we have. It's based
I have been designing a component-based game library, with the overall intention of writing
Several frameworks for writing web-based desktop-like applications have recently appeared. E.g. SproutCore and Cappuccino
I'm writing a library search engine where a user can search based on various
I'm writing a Navigation-Based iPhone app, and I'd like to have a UIToolBar docked
I'm writing a web based application that will have its own authorization/authentication mechanism (traditional
I am writing an application based on wxWidgets library. I had some classes which
I am writing a library for efficient number processing. I have to support different
So I'm writing yet another Twisted based daemon. It'll have an xmlrpc interface as

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.