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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:54:25+00:00 2026-06-01T11:54:25+00:00

Can someone wiser than I please explain to me why the following code segment

  • 0

Can someone wiser than I please explain to me why the following code segment faults? There is no problem allocating the memory by reference, but as soon as I try to assign anything or free by reference, segfault occurs.

I’m sure I’m missing some fundamental concept about pointers and passing by reference, hopefully some light can be shed.

#include <stdlib.h>
#include <stdio.h>

void allocateMatrix(float ***);
void fillMatrix(float ***);
void freeMatrix(float **);

int main() {
    float **matrix;

    allocateMatrix(&matrix);        // this function calls and returns OK
    fillMatrix(&matrix);            // this function will segfault
    freeMatrix(matrix);             // this function will segfault

    exit(0);
}

void allocateMatrix(float ***m) {
    int i;
    m = malloc(2*sizeof(float*));
    for (i = 0; i < 2; i++) {
        m[i] = malloc(2*sizeof(float));
    }
    return;
}

void fillMatrix(float ***m) {
    int i,j;
    for (i = 0; i < 2; i++) {
        for (j = 0; j < 2; j++) {
            (*m)[i][j] = 1.0;        // SEGFAULT
        }
    }
    return;
}

void freeMatrix(float **m) {
    int i; 
    for (i = 0; i < 2; i++) {
        free(m[i]);                  // SEGFAULT
    }
    free(m);
    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-01T11:54:26+00:00Added an answer on June 1, 2026 at 11:54 am

    One set of problems is here:

    void allocateMatrix(float ***m) {
        int i;
        m = malloc(2*sizeof(float*));
        for (i = 0; i < 2; i++) {
            m[i] = malloc(2*sizeof(float));
        }
        return;
    }
    

    You need to assign to *m to get the information back to the calling code, and also you will need to allocate to (*m)[i] in the loop.

    void allocateMatrix(float ***m)
    {
        *m = malloc(2*sizeof(float*));
        for (int i = 0; i < 2; i++)
            (*m)[i] = malloc(2*sizeof(float));
    }
    

    There’s at least a chance that the other functions are OK. The fillMatrix() is written and invoked correctly, though it could be simplified by losing the third * from the pointer:

    void fillMatrix(float **m)
    {
        for (int i = 0; i < 2; i++)
        {
            for (int j = 0; j < 2; j++)
                m[i][j] = 1.0;        
        }
    }
    

    It might be advisable to pass the triple-pointer to freeMatrix() so that you can zero the pointer in the calling function:

    void freeMatrix(float ***m)
    {
        for (int i = 0; i < 2; i++)
            free((*m)[i]);
        free(*m);
        *m = 0;
    }
    

    Calling then becomes:

    allocateMatrix(&matrix);
    fillMatrix(matrix);
    freeMatrix(&matrix);   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Could someone please provide some suggestions on how I can decrease the following for
Can someone explain it succinctly? Can it be used with non-Silverlight clients?
Can someone explain to me this odd thing: When in python shell I type
Can someone tell me why the following does not match: >>> re.search(r'(\d{2, 10})', '153')
Can someone please tell me why, even though I've set my Window to SizeToContent
Can someone explain to or link to an article that explains how the parameters
can someone please give me an example of how you detect if the iphone
can someone please help me understand what's going on here lists:dropwhile(fun(X) -> X <
Can someone recommend an up to date library for data Sanitization in PHP ?
Can someone point me to an article that shows the dropdownlist being populated from

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.