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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:18:35+00:00 2026-06-17T13:18:35+00:00

Alternatively, duplicate of Facing an error — glibc detected free invalid next size (fast)

  • 0

Alternatively, duplicate of Facing an error — glibc detected free invalid next size (fast).

I have been struggling with this, I tried using valgrind to track it down but can’t seem to pin down the exact source of the error. I can call the function 4 times, but after that it throws the realloc invalid next size error.

The exact error: * glibc detected * ./matrix: realloc(): invalid next size: 0x0000000001a46010 ***

Here’s the code:

char line[101];
int nMatrix = -1;
Dims *dimensions;
List *vals = NULL;
int **values;
int **columns;
int **rowPointer;
int *lineCount;
int *highestRow;

void newMatrix()
{
    nMatrix++;
    values = realloc(values, sizeof(int*));
    columns = realloc(columns, sizeof(int*));
    rowPointer = realloc(rowPointer, sizeof(int*));
    dimensions = realloc(dimensions, sizeof(Dims));
    lineCount = realloc(lineCount, sizeof(int));
    highestRow = realloc(highestRow, sizeof(int));
}

void readIn(char* inputFile, int transpose)
{
    FILE *fr;
    int a = 0;
    int b = 0;
    int c = 0;
    newMatrix();
    if((fr = fopen(inputFile, "r")) != NULL)
    {
        while(fgets(line, 100, fr) != NULL)
        {
            if(lineCount[nMatrix] == 0)
            {
                if(transpose)
                    sscanf(line, "%d,%d", &dimensions[nMatrix].n, &dimensions[nMatrix].m);
                else
                    sscanf(line, "%d,%d", &dimensions[nMatrix].m, &dimensions[nMatrix].n);
                printf("nMatrix = %d, n%d,m%d\n", nMatrix, dimensions[nMatrix].n, dimensions[nMatrix].m);
            }
            else
            {
                sscanf(line, "%d,%d,%d", &a,&b,&c);
                //printf("a = %d, b = %d, c = %d\n", a,b,c);
                //rows[a] = insertList(c,b,rows[a]);c
                if(transpose)
                    vals = insertList(c, a, b, dimensions[nMatrix].m, dimensions[nMatrix].n, vals);
                else
                    vals = insertList(c, b, a, dimensions[nMatrix].m, dimensions[nMatrix].n, vals);
            }
            lineCount[nMatrix]++;
        }
        values[nMatrix] = calloc(lineCount[nMatrix], sizeof(int));
        columns[nMatrix] = calloc(lineCount[nMatrix], sizeof(int));
        rowPointer[nMatrix] = calloc(((dimensions[nMatrix].m)+1), sizeof(int));
        values[nMatrix][lineCount[nMatrix]] = 0;
        columns[nMatrix][lineCount[nMatrix]] = 0;
        int i = 0;
        int lastRow = -1;
        while(i < dimensions[nMatrix].m)
        {
            rowPointer[nMatrix][i] = -1;
            i++;
        }
        i = 0;
        List *temp = NULL;
        while(vals != NULL)
        {
            temp = vals;
            //printf("pos = %d, row = %d, col = %d, val = %d, i=%d", temp->position, temp->row, temp->column, temp->value, i);
            if(lastRow != temp->row)
            {
                rowPointer[nMatrix][temp->row] = i;
                lastRow = temp->row;
                highestRow[nMatrix] = i;
            }
            values[nMatrix][i] = temp->value;
            columns[nMatrix][i] = temp->column;
            i++;
            vals = temp->next;
            free(temp);

        }
        rowPointer[nMatrix][dimensions[nMatrix].m] = lineCount[nMatrix]-1;
        fclose(fr);
        return;
    }
    fclose(fr);
    printf("File not found\n");
    return;
}
  • 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-17T13:18:36+00:00Added an answer on June 17, 2026 at 1:18 pm

    With each new matrix you “allocate”, you should be expanding your global pointer lists. you don’t. You just reallocate them to the same size the were before:

    This:

    void newMatrix()
    {
        nMatrix++;
        values = realloc(values, sizeof(int*));
        columns = realloc(columns, sizeof(int*));
        rowPointer = realloc(rowPointer, sizeof(int*));
        dimensions = realloc(dimensions, sizeof(Dims));
        lineCount = realloc(lineCount, sizeof(int));
        highestRow = realloc(highestRow, sizeof(int));
    }
    

    Should be this:

    void newMatrix()
    {
        nMatrix++;
        values = realloc(values, (nMatrix+1)*sizeof(int*));
        columns = realloc(columns, (nMatrix+1)*sizeof(int*));
        rowPointer = realloc(rowPointer, (nMatrix+1)*sizeof(int*));
        dimensions = realloc(dimensions, (nMatrix+1)*sizeof(Dims));
        lineCount = realloc(lineCount, (nMatrix+1)*sizeof(int));
        highestRow = realloc(highestRow, (nMatrix+1)*sizeof(int));
    }
    

    Note: the (nMatrix+1) value is used because you start with nMatrix as (-1), and upon first increment it is (0), the next is (1), etc… I.e. it always indexes the last row inserted, but your vector magnitudes needs to be +1 to that for hopefully obvious reasons.

    I would strongly suggest you consider what happens when realloc() fails as well, as it will return NULL and in the process leak whatever memory was pointed to by the pointer you passed in.

    There may be other issues, but that was the first one that jumped out at me.

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

Sidebar

Related Questions

Alternatively: duplicate of Facing an error — glibc detected free invalid next size (fast)
Possible Duplicate: C++ Error: free(): invalid next size (fast): That's a C++ question (albeit
I may have either misunderstood how to use the ON DUPLICATE KEY syntax, alternatively
Possible Duplicate: C# - Is there a better alternative than this to ‘switch on
Possible Duplicate: Viewing Unpushed Git Commits How do I list all commits which have
Ok this is not duplicate of Alternative to Process.Start() because my question is something
... or alternatively an Array which prevents duplicate entries. Is there some kind of
Possible Duplicate: Getting error org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named ‘springSecurityFilterChain’ is defined In my Spring
Before I get flagged for duplicate, I have the code from Dynamic json object
Possible Duplicate: Alternatives to the MVC I have searched around for alternatives just to

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.