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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:37:39+00:00 2026-05-28T03:37:39+00:00

I’m trying to learn C and I’ve gotten to a point where I felt

  • 0

I’m trying to learn C and I’ve gotten to a point where I felt like trying something more advanced than small and easy examples of printing text to the console, using pointers in a very simple way and so on.

The problem is that my program crashes on my call to free and I’m sure it is the pointer(s) somehow haunting me…

So, I have created a two-dimensional array like this:

bool InitializeGrid(struct Grid *g, int rows, int columns)
{
    g->rows = rows;
    g->columns = columns;
    g->grid = malloc(g->rows * sizeof(int *));

    if(g->grid == NULL)
    {
        printf("Could not allocate memory for grid rows.\n");
        return false;
    }

    for (int i = 0; i < g->rows; i++)
    {
        g->grid[i] = malloc(g->columns * sizeof(int));

        if(g->grid[i] == NULL)
        {
            printf("Could not allocate memory for grid columns.\n");
            return false;
        }
    }

    / ... /

    return true;
}

The structure Grid looks like this:

struct Grid
{
    int rows;
    int columns;
    int **grid;
};

I then successfully use the grid throughout the program and everything works great. When the program is about to exit, I need/want to free the memory I allocated. This is how I do that:

void CleanupGrid(struct Grid *g)
{
    for(int i = 0; i < g->rows; i++)
        free(g->grid[i]);

    free(g->grid);
}

But on the first free(g->grid[i]) I receive a segmentation fault.

Why does this not work? How can I fix this?

Edit:

stdbool.h has been included in the program for use of bool/true/false.

I feel really stupid for staring blind at the free() statement. When I scale down the program to the absolute minimum, it works great with the free() statements. It is when I access and write to the grid at one point that the free() greets me with a segmentation fault.

Accepting user1083265’s answer.

  • 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-28T03:37:39+00:00Added an answer on May 28, 2026 at 3:37 am

    I compile your code (just alloc/dealloc) and it doesn’t crash.

    Possibly, when using allocated grid, you corrupt some memory above allocated fragments. Glibc uses canary values to detect such corruption – so you get segfault when freeing corrupted memory.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
In my XML file chapters tag has more chapter tag.i need to display chapters

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.