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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:49:51+00:00 2026-06-12T23:49:51+00:00

the problem is I can’t free it,the console output pointer are the same inside

  • 0

the problem is I can’t free it,the console output pointer are the same inside free function, the xcode detected at the line from function StringList_add when realloc called.

typedef struct stringlist_s {
        int max_str;
        char **str;
}stringlist_t;

//functions
stringlist_t *StringList_new()
{
    stringlist_t *lst = (stringlist_t *)malloc(sizeof(stringlist_t));

    return lst;
}

void StringList_add(stringlist_t *str_list,char *str)
{
    if(!str)
        return;
    if(!str_list)
        return;

    str_list->str = (char **)realloc(str_list->str, sizeof(char *)  * (str_list->max_str+1));

    str_list->str[str_list->max_str] = (char *)malloc(strlen(str) + 1);

    memcpy(str_list->str[str_list->max_str], str, strlen(str) + 1);

    str_list->max_str++;
}

void StringList_release(stringlist_t *strList)
{
    if(!strList) {
        printf("Releasing empty pointer\n");
         return;
    }

    for(int i = 0 ; i < strList->max_str; ++i )
    {
        free(strList->str[i]);
        printf("pointer inside is %p\n",strList->str[i]);
    }

    printf("list before is  %p\n",strList);
    free(strList);
    printf("list  now is %p\n",strList);  //value is the same as previous printf

}

I just use this to test the code above:

stringList_t *a = StringList_new();
StringList_add(a,"abc");
StringList_add(a,"edf");
StringList_release(a);
  • 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-12T23:49:53+00:00Added an answer on June 12, 2026 at 11:49 pm

    A problem is that StringList_new() allocates a new stringList_t but never initialises it members. At the call to realloc():

    str_list->str = (char **)realloc(str_list->str, sizeof(char *)  * 
        (str_list->max_str+1));
    

    neither str_list->str or str_list->max_str have been initialised. From the reference page for realloc():

    It must be previously allocated by malloc(), calloc() or realloc() and not yet freed with free(), otherwise, the results are undefined.

    which will be the case when used with an unitialised pointer.

    Change to:

    stringlist_t *StringList_new()
    {
        stringlist_t *lst = malloc(sizeof(*lst));
        lst->max_str = 0;
        lst->str     = NULL;
    
        return lst;
    }
    

    Don’t cast the return value of malloc() or realloc(). Passing a NULL pointer to realloc() is fine, it behaves like malloc() in that case. When using realloc() store the return value in a temporary pointer variable to avoid a memory leak in the event that realloc() fails:

    char** tmp = realloc(str_list->str, sizeof(*tmp)  * (str_list->max_str+1));
    if (tmp)
    {
        str_list->str = tmp;
    }
    

    Note I did not use calloc() in StringList_new() as according the C standard all bits zero need not represent a null pointer.

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

Sidebar

Related Questions

What problem can happen if the goto-line function is used in a non-interactive elisp
So basically my problem can be written in pseudo-code as follows: split the line
What potential problem can arise if I am keeping data inside a global var
problem can be seen here: http://www.studioimbrue.com/beta The code: $(document).ready(function(){ $('div.caption').hide(); $('.captions ul li img').hover(function(){
I wonder if the objective function of a general dynamic programming problem can always
From what I've been seeing around the internet, this problem can't really be solved
The problem can be described as follow. Input __m256d a, b, c, d Output
My problem can be reduced to basically the following set of entities : I
My problem can be summed up by making this simple command works : nice
I am facing a problem - can not update the price of the variant

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.