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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T15:04:40+00:00 2026-05-11T15:04:40+00:00

What’s the best-practice in handling memory in C? There are no classes with constructors/destructors

  • 0

What’s the best-practice in handling memory in C?

There are no classes with constructors/destructors to handle this for me.

  • Is it good to allocate memory in the beginning of a function or use a function that creates it for me? And how should I free them again?
  • These are broad questions and they differ from situation to situation but how do you prefer to handle it?
  • What tips and lessons can you give?

I think I’ve got a good grasp on how to handle memory in C++ but doing it in C is different I’m a bit off.

In C++ I’ve got constructors and destructors, I’ve got the pretty straightforward new and delete and I know how to encapsulate it using RAII, using with smart pointers and within classes.

However in C I can’t handle malloc and free the same way. I don’t know how to hide them and how to automate things. All I can figure is using functions for initiating and destroying my pointers. But how should I structure my memory handling?

While writing this I’ve realized this is more a question about me understanding the flow of C than anything else, but one question at a time.

Edit: Thanks for the answers but I need to rephrase myself.

When I say that I use RAII and smart pointers for C++ I don’t want the same for C, I know it’s not the same. But how I handle memory allocation in C++ is connected to these techniques.

For example in my classes I dynamically add and destroy the memory my class uses. This way I can achieve a sort of encapsulation, I don’t need to know when/how/why the class handles it’s memory, it just does. This means I can "hide" the lower memory handling and just focus on a few "bigger" classes.

  • 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. 2026-05-11T15:04:40+00:00Added an answer on May 11, 2026 at 3:04 pm

    Part of the confusion is that it is inherently more difficult in C. malloc and free are similar to new and delete: malloc allocates new memory, and returns a pointer to that memory. free makes that memory available again, so long as it’s memory that was allocated using malloc. Otherwise, it just makes hash of some chunk of memory. It doesn’t care.

    The important thing with malloc/free is to decide on and consistently maintain a disciplined use. Here are some hints:

    ALWAYS check the returned pointer from malloc for NULL

    if((p = (char *) malloc(BUFSIZ)) == NULL ) {    /* then malloc failed do some error processing. */ } 

    For belt and suspenders safety, set a pointer to NULL after freeing it.

    free(p); p = NULL ; 

    try to malloc and free a chunk of memory within the same scope if possible:

     {  char * p ;    if((p = malloc(BUFSIZ)) == NULL) {        /* then malloc failed do some error processing. */    }   /* do your work. */     /* now you're done, free the memory */     free(p);    p = NULL ;  /* belt-and suspenders */  }     

    When you can’t, make it clear that what you’re returning is malloc‘ed memory, so the caller can free it.

     /* foo: do something good, returning ptr to malloc memory */  char * foo(int bar) {      return (char *) malloc(bar);  }     
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.