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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:41:30+00:00 2026-05-20T17:41:30+00:00

Hello I am trying to free a linked list I have created and I’m

  • 0

Hello I am trying to free a linked list I have created and I’m having some real trouble.

My System is Ubuntu 10.10 and I am using the gcc compiler. I have to use a makefile and separate source files compilation.
This is the head of the makefile

OBJS    = main.o Buffer.o ExtMerge.o MySet.o
SOURCE  = main.c Buffer.c ExtMerge.c MySet.c
HEADER  = MyHeader.h Buffer.h ExtMerge.h MySet.h
OUT     = myjoin
CC  = gcc
FLAGS   = -g -c

The programm is part of an external sorting programm.It reads a bulk of records from a file, inserts them to the List sorted and writes them to part files.So far everything is going as it should. Then another file is opened. At this point, it would be wise to free the memory so that the new List (containing data from say file2) doesn’t cost extra memory.

Let me show you my code:
The nodes of the List:

struct record{

    char    **field;
    struct  record  *next;
    };

This function inserts an array of tokens (read from the file) to the list ordered:
cols_no is the number of strings in the array, column is the one of interest.
The following malloc stuff works just perfect

struct record *OrderedInsertRec(struct record *buf, char **arr, int column, int cols_no)
{   struct  record  *aux=NULL;
    int i;

if ( buf==NULL ) { 
            buf = (struct record *) malloc (sizeof(struct record)+1);
            buf->field=(char **)calloc(cols_no, sizeof(char*));
    for (i = 0; i <= cols_no; ++i){
        buf->field[i] = (char*)malloc(((strlen(arr[i])+1))*sizeof(char*));
        strcpy(buf->field[i],arr[i]);
    }

            buf->next = NULL;
    return(buf);
            }                 
else if ( strverscmp (buf->field[column], arr[column]) < 0 ) {  
    buf->next = OrderedInsertRec (buf->next, arr, column, cols_no);
    return(buf);
    }
else    {
    aux = (struct record *) malloc (sizeof(struct record)+1);
            aux->field=(char **)calloc(cols_no, sizeof(char*));
    for (i = 0; i <= cols_no; ++i){
        aux->field[i] = malloc(strlen(arr[i])+1);
        strcpy(aux->field[i],arr[i]);
    }

            aux->next = NULL;
    aux->next = buf;
    return(aux);
            }           

}

This is causing my trouble:

struct record * DeleteBuffer(struct record * buf, int cols_no)
{
int i;

struct record * tmp;

while(buf != NULL)
{

    tmp = buf; 
    buf = buf->next;
    }
free(tmp);
}

}

This works without any problem but does nothing. Only a small amount of memory is freed(I suppose beacause I only free the pointer I have allocated in OrderedInsert and not the field[] array) as I can see from the System Monitor

Then I tried this:

struct record * DeleteBuffer(struct record * buf, int cols_no)
{
int i;


struct record * tmp;
while(buf != NULL)
{

    tmp = buf; 
    buf = buf->next;
    for (i = 0; i <= cols_no; ++i){

        free(tmp->field[i]);
    }

free(tmp);
}
}
}

And this is what i got:
{

*** glibc detected *** ./myjoin: double free or corruption (out): 0x088b3010 ***
======= Backtrace: =========
/lib/libc.so.6(+0x6c501)[0xb766e501]
/lib/libc.so.6(+0x6dd70)[0xb766fd70]
/lib/libc.so.6(cfree+0x6d)[0xb7672e5d]
./myjoin[0x8048bee]
./myjoin[0x80495b4]
./myjoin[0x8048a10]
/lib/libc.so.6(__libc_start_main+0xe7)[0xb7618ce7]
./myjoin[0x8048791]
======= Memory map: ========
08048000-0804b000 r-xp 00000000 fb:03 2999566    /home/giorgos/Desktop/test11/myjoin
0804b000-0804c000 r--p 00002000 fb:03 2999566    /home/giorgos/Desktop/test11/myjoin
0804c000-0804d000 rw-p 00003000 fb:03 2999566    /home/giorgos/Desktop/test11/myjoin
088a0000-088c1000 rw-p 00000000 00:00 0          [heap]
b74d3000-b74ed000 r-xp 00000000 fb:03 3940432    /lib/libgcc_s.so.1
b74ed000-b74ee000 r--p 00019000 fb:03 3940432    /lib/libgcc_s.so.1
b74ee000-b74ef000 rw-p 0001a000 fb:03 3940432    /lib/libgcc_s.so.1
b7500000-b7521000 rw-p 00000000 00:00 0 
b7521000-b7600000 ---p 00000000 00:00 0 
b7601000-b7602000 rw-p 00000000 00:00 0 
b7602000-b7759000 r-xp 00000000 fb:03 3940764    /lib/libc-2.12.1.so
b7759000-b775b000 r--p 00157000 fb:03 3940764    /lib/libc-2.12.1.so
b775b000-b775c000 rw-p 00159000 fb:03 3940764    /lib/libc-2.12.1.so
b775c000-b775f000 rw-p 00000000 00:00 0 
b776d000-b7772000 rw-p 00000000 00:00 0 
b7772000-b7773000 r-xp 00000000 00:00 0          [vdso]
b7773000-b778f000 r-xp 00000000 fb:03 3940761    /lib/ld-2.12.1.so
b778f000-b7790000 r--p 0001b000 fb:03 3940761    /lib/ld-2.12.1.so
b7790000-b7791000 rw-p 0001c000 fb:03 3940761    /lib/ld-2.12.1.so
bfb1a000-bfb3b000 rw-p 00000000 00:00 0          [stack]
Aborted

}

What should I do. Is it a problem with my malloc??
Thank you in advance!

  • 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-20T17:41:30+00:00Added an answer on May 20, 2026 at 5:41 pm

    I see three problems. You malloc/calloc:

          buf = (struct record *) malloc (sizeof(struct record)+1);
          buf->field=(char **)calloc(cols_no, sizeof(char*));
    

    Why sizeof(struct record)+1? What’s the +1 for?

    You calloc() cols_no items of size sizeof(char*), yet your two for-loop’s go from 0…cols, when it should go from 0…cols-1.

       for (i = 0; i <= cols_no; ++i){
    

    should be:

       for (i = 0; i < cols_no; ++i){
    

    You should be allocating (strlen(arr[i])+1)*sizeof(char) — using sizeof(char*) is probably 4-8x too much, assuming sizeof(char) == 1 and sizeof(char*) == 4 or 8 depending on 32-bit or 64bit ptrs.

          buf->field[i] = (char*)malloc(((strlen(arr[i])+1))*sizeof(char*));
    

    should be:

          buf->field[i] = (char*)malloc(((strlen(arr[i])+1))*sizeof(char));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello im trying to update google admboads to V6 but im having some trouble
Hello trying to get my website to display correctly having trouble. I am using
I have created a paid application and I'm trying to create the free with
hello i am trying to load a list of filenames from a folder into
Hello I was trying to remove objects from object array that I have and
Hello I'm trying something .. I was reading Jquery traversing documentations some forums, this
Hello I am trying to setup a Padrino project using DataMapper and MySQL on
I'm having a hell of a time trying to debug some kind of memory
Hello Trying to sort this list by Month and day. Can not get it
Hello I´m trying using the next piece of code: import pickle object = Object()

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.