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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:43:25+00:00 2026-05-23T13:43:25+00:00

I have a complex C code with me and while executing it, I chanced

  • 0

I have a complex C code with me and while executing it, I chanced upon the following errors:

  1. glibc: corrupted double-linked list
  2. glibc: malloc() memory corruption
  3. munmap_chunk() invalid pointer

I realized 1) is associated with freeing already freed memory. I am still trying to figure out the reasons for 2) and 3).

Well, the thing is then I did some searches and got the general opinion that I must debug with “valgrind” to detect memory corruption related problems.

Ok, coming back to the point,when I searched this forum, I have just dug up some code posted at: What is the best way to free memory after returning from an error?

And this piece of code had solved my problems:

int func(void **mem1, void **mem2)
{
    *mem1 = NULL;
    *mem2 = NULL;

    *mem1 = malloc(SIZE);
    if(!*mem1)
        goto err;

    *mem2 = malloc(SIZE);
    if(!*mem2)
        goto err;

    return 0;
err:
    if(*mem1)
        free(*mem1);
    if(*mem2)
        free(*mem2);

    *mem1 = *mem2 = NULL;

    return 1;
}

Well what really solved my issue is the line:

eg:

char *ptr = NULL;

ptr = (char *)malloc(SIZE);

assign and use ptr

free(ptr);

How is char *ptr = NULL helping???? Infact when I assigned to NULL in the beginning, I didn’t even use free(ptr). It still worked liked a charm(I tried executing several times)

When I remove the NULL assignment in the beginning I get error 1) 🙁 🙁

I am going to install Valgrind but before that I would like some insights on this.

Thanks

  • 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-23T13:43:26+00:00Added an answer on May 23, 2026 at 1:43 pm

    I’ll take a shot in the dark and guess that you attempt to free() the ptr pointer before allocating with malloc().

    If it has been initialized to NULL, most free() implementations do nothing. From the free() manual page:

    free() frees the memory space pointed
    to by ptr, which must have been
    returned by a previous call to
    malloc(), calloc() or realloc().
    Otherwise, or if free(ptr) has already
    been called before, undefined
    behaviour occurs. If ptr is NULL, no
    operation is performed.

    If it has not been set to NULL, you are trying to free either a random pointer, or something that has been already been freed.

    That said, Valgrind is the best tool to properly detect such errors on POSIX systems.

    EDIT:

    What needs to be understood it that C is not Java and it does not have the luxuries of a VM. Everything exists within the same address space, with minimal protections – and that includes the structures of the memory allocator. Once a memory-related error occurs, there is no way to predict how it will make itself known.

    Of the other two errors, I’d guess at first glance that (3) is once again related to freeing an address that has not been allocated. There is no way, however, to be sure that this is actually the issue. Once the memory of a process is corrupted, you cannot trust anything it tells you about itself – which is what’s happening in this case.

    Just use proper debugging tools like GDB or Valgrind and save yourself (and us) the pain of guessing blindly…

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

Sidebar

Related Questions

I have two complex (i.e. objects with string, int, double, List and other home
I have this long and complex source code that uses a RNG with a
I have this piece of code: var myObj = function () { this.complex =
I have to buil a rather complex WPF UI (a hughe Grid) in code
I have some debugging code that if executed while running with GBD attached should
I often see code like: Iterator i = list.iterator(); while(i.hasNext()) { ... } but
I have a linked list of structures. Lets say I insert x million nodes
I have this code: while($row = mysql_fetch_array($res)) { $name = $row['advertiser']; $id = $row['id'];
Let's say you have the following code: string encoded=9,8,5,4,9; // Parse the encoded string
I have a complex query with group by and order by clause and I

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.