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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:46:45+00:00 2026-05-14T19:46:45+00:00

I’ve been using C on some projects for a master’s degree but have never

  • 0

I’ve been using C on some projects for a master’s degree but have never built production software with it. (.NET & Javascript are my bread and butter.) Obviously, the need to free() memory that you malloc() is critical in C. This is fine, well and good if you can do both in one routine. But as programs grow, and structs deepen, keeping track of what’s been malloc‘d where and what’s appropriate to free gets harder and harder.

I’ve looked around on the interwebs and only found a few generic recommendations for this. What I suspect is that some of you long-time C coders have come up with your own patterns and practices to simplify this process and keep the evil in front of you.

So: how do you recommend structuring your C programs to keep dynamic allocations from becoming memory leaks?

  • 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-14T19:46:45+00:00Added an answer on May 14, 2026 at 7:46 pm

    Design by contract. Make sure every function comment is explicit about its memory hygiene – that is to say, whether it mallocs and whose responsibility it is to free what was allocated, and whether it takes ownership of anything passed in. And BE CONSISTENT with your functions.

    For example, your header file might contain something like:

    /* Sets up a new FooBar context with the given frobnication level.
     * The new context will be allocated and stored in *rv; 
     * call destroy_foobar to clean it up. 
     * Returns 0 for success, or a negative errno value if something went wrong. */
    int create_foobar(struct foobar** rv, int frobnication_level);
    
    /* Tidies up and tears down a FooBar context. ctx will be zeroed and freed. */
    void destroy_foobar(struct foobar* ctx);
    

    I heartily endorse the advice to use Valgrind, it’s a truly fantastic tool for tracking down memory leaks and invalid memory accesses. If you’re not running on Linux, then Electric Fence is a similar tool, though less functional.

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

Sidebar

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.