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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:41:41+00:00 2026-06-08T14:41:41+00:00

Just for fun (and for C programming practice) I wrote the following piece of

  • 0

Just for fun (and for C programming practice) I wrote the following piece of code that does the following:

  • Acts as a tracking system for memory allocations
  • Frees all dynamically allocated memory with a function call

Here is the code:

typedef enum _OpMode {
    OM_APPEND,
    OM_DESTROY
} OP_MODE;

void refOp(void *ptr, OP_MODE mode) {
    /* contains static array of pointers and provides an interface to that
       array */

    static void **references = NULL;
    static int size = 0;
    static int reset = 0;

    if (reset) {
        reset = 0;
        references = NULL;
        size = 0;
    }

    switch (mode) {
        case OM_APPEND:
            //add a pointer to reference array
            references = (void**) realloc(references, sizeof(void*) * (size + 1));
            references[size++] = ptr;
            break;
        case OM_DESTROY:
            //free memory at all pointers kept in reference array
            for (int i = 0; i < size; i++) {
                free(references[i]);
                references[i] = NULL;
            }
            free(references);
            reset = 1;
            break;
        default:
            printf("Invalid enum value '%d' passed as mode.\n", mode);
            break;
    }
}

void refDestroyAll() {
    //Wrapper function
    refOp(NULL, OM_DESTROY);
}

void *myAlloc(void* ptr, size_t size) {
    /* Allocates memory and stores pointer copy in reference array */
    void *tmp_ptr;
    tmp_ptr = realloc(ptr, size);
    refOp(tmp_ptr, OM_APPEND);
    return tmp_ptr;
}

The idea is that one would use myAlloc() instead of malloc or realloc to dynamically allocate memory. And one would use refDestroyAll() to free all memory that was created with myAlloc().

I’ve done some testing, and it seems to be working, but I can’t help feeling that I’m missing something important. Does this code actually work as intended, or am I leaking memory when I call refDestroyAll()?

  • 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-08T14:41:45+00:00Added an answer on June 8, 2026 at 2:41 pm

    You have a bug, that could cause a segmentation fault. realloc() could return the same pointer as it is given, in which case you would have added it twice to the array. When you call your free function, it would try and free the same pointer twice, resulting in a segmentation fault error.

    Additionally, I don’t understand why you have the reset parameter. Why not simply set references and size to 0 in the OM_DESTROY case? It is good practice to always set a pointer to NULL immediately after freeing it.

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

Sidebar

Related Questions

Just for fun I created a project that created about 5 GB of memory
I'm writing some code (just for fun so far) in Python that will store
I am writing my own programming language just for the fun of it. Currently
Just for fun, I wrote this simple function to reverse a string in Python:
I'm learning about AI and (just for fun and practice, not profit or anything
I'm programming a little Twitter Client just for fun. I have the tweet's text
I want to write a computer algebra system for iOS (just for fun), and
I am building a website just for fun of learning programming more fully, but
I'm developing an application just for fun that consists of capturing images from a
I am new to haskell and just learning the fun of functional programming. but

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.