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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:40:42+00:00 2026-05-16T20:40:42+00:00

I’m struggling to find why I can’t free a memory block. Something must be

  • 0

I’m struggling to find why I can’t free a memory block. Something must be wrong with the pointer. A memory block for a structure is made in a function and the pointer used is stored in an array. Later the pointer is taken from the array to be used to free the memory.

I’ve figured out which free it is. I’ve placed “//This one” next to it.

#include <stdlib.h>

typedef enum {
    INT = 0,
    FLOAT = 1,
    STRING = 2,
    NONE = 3,
    BOOL = 4
} TYPE;

typedef struct {
    TYPE type;
    void * data;
} Variable;

typedef Variable ** var; //Convenience typedef for pointer to variable pointer

typedef struct {
    size_t size;
    Variable *** pool; //Pointer to the array of pointers to the Variable structure pointers. Pointer required for memory allocation and array consists of pointers to pointers so the pointers to the Variable structures can be changed throughtout different functions.
} VarPool; //Variable pools will just be used to deallocate all variables at the end of a scope

VarPool * global_pool; //Pool for global scope
VarPool ** pool_stack; //Keeps track of pools in stack
unsigned int pool_stack_size;

void init_pool_stack(){
    pool_stack = malloc(sizeof(VarPool *)); //Start with global_pool
    pool_stack_size = 1;
    global_pool = malloc(sizeof(VarPool));
    pool_stack[0] = global_pool;
    global_pool->pool = NULL;
    global_pool->size = 0;
}

Variable ** new_var(){ //Makes new variable
    Variable ** return_variable;
    Variable * new_variable = malloc(sizeof(Variable));
    VarPool * var_pool = pool_stack[pool_stack_size-1]; //Current variable pool on top of stack
    var_pool->size++;
    var_pool->pool = realloc(var_pool->pool,var_pool->size*sizeof(Variable **));
    return_variable = &new_variable;
    var_pool->pool[var_pool->size - 1] = return_variable;
    return return_variable; //Return pointer to new pointer so pointer can be changed to NULL when deleted
}
void empty_pool(){ //Frees all data from variable pool
    VarPool * var_pool = pool_stack[pool_stack_size-1]; //Current pool on top of stack
    for (int x = 0; x < var_pool->size; x++) {
        free(*var_pool->pool[x]); //Free variable data
    }
    free(var_pool->pool); //Free pool variable array
    free(var_pool); //This one
    pool_stack_size--;
    pool_stack = realloc(pool_stack, pool_stack_size*sizeof(VarPool *));
}

int main (int argc, const char * argv[]) {
    init_pool_stack();
    new_var();
    empty_pool(); //Finally empty globals pool which will deallocate pool_stack
    return 0;
}

Thank you for any help.

  • 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-16T20:40:43+00:00Added an answer on May 16, 2026 at 8:40 pm

    in new_var() you have (simplified)

    Variable ** new_var(){ //Makes new variable
      Variable ** return_variable;
      Variable * new_variable = malloc(sizeof(Variable));
    
      return_variable = &new_variable;
      return return_variable;
    }
    

    The value returned from this function becomes invalid once the function ends.
    The address of a local variable is only meaningful while that variable exists.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.