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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:20:09+00:00 2026-06-08T05:20:09+00:00

I’m developing a small C library with functions for frequently used operations, mainly to

  • 0

I’m developing a small C library with functions for frequently used operations, mainly to rub up my C skills. In a session of debugging my deallocation function, i saw that the value of a pointer which previously pointed at a two-dimensional matrix isn’t NULL.

I know that there is not a way to check the state of a pointer after deallocation (from reading this question), but i assigned a NULL value to this pointer to avoid it to eventually become a dangling pointer.

Function code is:

void matfree(void **M, int m){
int i;

for(i = 0; i < m; i ++){
    free(M[i]);
    M[i] = NULL;
}
free(M);
M = NULL;
}

Test code (into another file):

int matfreetest(){
int **M = NULL;

M = (int **) matmalloc(0, 2, 2);
if(*M == NULL){
    printf("Here 1! \n");
    return 0;
}
matfree(M, 2);
if(*M == NULL){
    return 1;
}
else{
    printf("Here 2! \n");
    return 0;
}
}

int main(){
int status; 

status = matmalloctest();
printf("matmalloctest status = %d \n", status);
status = matfreetest();
printf("matfreetest status = %d \n", status);
system("PAUSE");
return 1;
}

I expected matfreetest to return status 1, but it returns 0. What is done wrong here?

EDIT: Code of matmalloc, as requested:

/* Memory allocator for any type and size of matrix. 
Arguments:
-   int type:    identifier of the matrix' type
-   int m:       number of matrix rows
-   int n:       number of matrix columns
*/
void **matmalloc(int type, int m, int n){
int i;
void **M;

/* Check data type the do the proper allocation. */
if(type == INTEGER){ //int
    M = (int **) malloc(m*sizeof(int *));
    if(M == NULL){
        printf("Allocation failed! \n");
        return NULL;
    }
    for(i = 0; i < m; i ++){
        M[i] = (int *) malloc(n*sizeof(int));
        if(M[i] == NULL){
            printf("Allocation failed! \n");
            return NULL;
        }
    }
    printf("Allocation completed! \n");
    return M;
}
else if(type == SINTEGER){ //short int
    M = (short int **) malloc(m*sizeof(short int *));
    if(M == NULL){
        printf("Allocation failed! \n");
        return NULL;
    }
    for(i = 0; i < m; i ++){
        M[i] = (short int *) malloc(n*sizeof(short int));
        if(M[i] == NULL){
            printf("Allocation failed! \n");
            return NULL;
        }
    }
    printf("Allocation completed! \n");
    return M;
}
else if(type == LINTEGER){ //long int
    M = (long int **) malloc(m*sizeof(long int *));
    if(M == NULL){
        printf("Allocation failed! \n");
        return NULL;
    }
    for(i = 0; i < m; i ++){
        M[i] = (long int *) malloc(n*sizeof(long int));
        if(M[i] == NULL){
            printf("Allocation failed! \n");
            return NULL;
        }
    }
    printf("Allocation completed! \n");
    return M;
}
else if(type == FL){ //float
    M = (float **) malloc(m*sizeof(float *));
    if(M == NULL){
        printf("Allocation failed! \n");
        return NULL;
    }
    for(i = 0; i < m; i ++){
        M[i] = (float *) malloc(n*sizeof(float));
        if(M[i] == NULL){
            printf("Allocation failed! \n");
            return NULL;
        }
    }
    printf("Allocation completed! \n");
    return M;
}
else if(type == DOUB){ //double
    M = (double **) malloc(m*sizeof(double *));
    if(M == NULL){
        printf("Allocation failed! \n");
        return NULL;
    }
    for(i = 0; i < m; i ++){
        M[i] = (double *) malloc(n*sizeof(double));
        if(M[i] == NULL){
            printf("Allocation failed! \n");
            return NULL;
        }
    }
    printf("Allocation completed! \n");
    return M;
}
else if(type == LDOUB){ //long double
    M = (long double **) malloc(m*sizeof(long double *));
    if(M == NULL){
        printf("Allocation failed! \n");
        return NULL;
    }
    for(i = 0; i < m; i ++){
        M[i] = (long double *) malloc(n*sizeof(long double));
        if(M[i] == NULL){
            printf("Allocation failed! \n");
            return NULL;
        }
    }
    printf("Allocation completed! \n");
    return M;
}
else{
    printf("Incorrect type of matrix data. Only numeric types are accepted. \n");
    return NULL;
}
}
  • 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-08T05:20:11+00:00Added an answer on June 8, 2026 at 5:20 am

    The “M” that is parameter to matfree is not the same as the one you have in the calling program. It would be better to name it differently to avoid ambiguity.

    You should have:

    M = matfree(M, 2); // Frees M and reassigns it to NULL
    
    void **matfree(void **X, int m)
    {
       int i;
       for(i = 0; i < m; i ++)
       {
           free(X[i]);
            X[i] = NULL;
       }
       free(X);
       return NULL;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.