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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:25:42+00:00 2026-05-22T14:25:42+00:00

I am new to C and totally lost by this problem. This is a

  • 0

I am new to C and totally lost by this problem. This is a homework assignment to implement something like the pagerank algorithm in C.

I am trying to record the links from other pages by way of a 2D pointer-pointer array. My program works just fine and happily calculates pagerank for large sets of links, however, whenever I try to free my links array I get an “invalid free” error.

example code:

struct webpage {
  char name[20];
  int links_out;
  struct webpage **links_in; //to hold pointers to pages.
  int index; //stores the position in the pre-sorted array
             // as I have to print it out in this order
};

static struct webpage *pages = NULL;

This is my data structure. After reading in some basic variables like the number of pages (npages) I then allocate memory

pages = (struct webpage *)calloc(npages, sizeof(struct webpage));

As I read in each webpage I allocate the internal 2d array links_in as follows

pages[counter].links_in = (struct webpage **)calloc(npages, sizeof(struct webpage *));

and then each page inside:

for(i =0; i< npages; ++i)
pages[counter].link_in[i] = (struct webpage *)calloc(1, sizeof(struct webpage));

I sort my array of webpages. Then read in the link information and binary search my array with bsearch to get a pointer to each desired page.

struct webpage *temp_in;
struct webpage *temp_out;

temp_in = bsearch(temp_str, pages, npages, sizeof(struct webpage), struct_cmp_by_name);
temp_out = bsearch(temp_str2, pages, npages, sizeof(struct webpage), struct_cmp_by_name);

Then I assign

temp_in->links_in[temp_out->index] = temp_out;

This all works great and I am able to access all the desired data to calculate pagerank.
Once I am done I try to free the memory as follows:

for(int i = 0; i < npages; ++i){
  for(int k = 0; k < npages; ++k){
     if(pages[i].links_in[k] != NULL){
          free(pages[i].links_in[k]); //this line is causing the error
          pages[i].links_in[k] = NULL;
     }
  }
 free(pages[i].links_in);
}

The call to free inside the k loop throws an invalid free (or a double free corruption out) error the very first time it is called.

I have been over the code with gdb and things seem to be pointing to the right thing. gdb list that line as the point where the program breaks.

valgrind says much the same:

Invalid free() / delete / delete[]
at 0x4A05187: free (vg_replace_malloc.c:325)
by 0x4009C4: memory_dump (pagerank.c: 87)  // this is the line free(pages[i].links_in[k];
by 0x4013F2: seq_check_condition (pagerank.c:355)
by 0x40162F: main (pagerank.c:418)

At no point do I delete/free that (or any memory tbh) memory before that point.

thanks for any suggestions.

  • 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-22T14:25:43+00:00Added an answer on May 22, 2026 at 2:25 pm

    I think you do not need to do the following

    for(i =0; i< npages; ++i)
        pages[counter].link_in[i] = (struct webpage *)calloc(1, sizeof(struct webpage));
    

    This is because for each page you allocate a new webpage pointer. The pages[counter].link_in[i] is used to hold the pointer to a webpage , ie. it should point to an already existing and allocated page. So you need to only assign the address of that webpage instance.

    I think the problem is here:

    Change your this code:

    for(int i = 0; i < npages; ++i){
      for(int k = 0; k < npages; ++k){
         if(pages[i].links_in[k] != NULL){
              free(pages[i].links_in[k]); //this line is causing the error
              pages[i].links_in[k] = NULL;
         }
      }
     free(pages[i].links_in);
    }
    

    To this:

    for(int i = 0; i < npages; ++i){
       free (pages[i].links_in);
    }
    free (pages);
    

    This is because the links the pages[i].links_in[x] holds are pointers to some pages[j], and because a particular pages[j] address can occur in pages[i].links_in[x] for any number of i, so potentially the address of the pages[j] occurs in multiple places, which is attempted to be freed by your routine in the line free(pages[i].links_in[k]);.

    Please let us know if this helps.

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

Sidebar

Related Questions

Totally new to this! As the title says, I'm trying to serve a stream
I'm getting totally lost and confused on how to use the new strongly typed
I'm totally new to GreaseMonkey, but I'm trying to make a little script. //
Being totally new into node.js environment and philosophy i would like answers to few
Totally new to Cake. I have this table named Content and another one named
Im totally new to WCF. Trying to create a Silverlight application with WCF services.
total newbie and totally lost... I'm trying to setup coldfusion(8) with a mysql(5.5) database
I am totally new to emacs and is starting to learn how to use
I'm totally new to how to cache images. I output all images in a
I am totally new to Sharepoint (2007) so please bear with me. I would

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.