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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:03:41+00:00 2026-05-28T04:03:41+00:00

I have a static pointer static int **2dArr; and then I allocate memory for

  • 0

I have a static pointer

   static int **2dArr;

and then I allocate memory for 2d array.
How can I free that memory and replace it with another array?

   //////////////////////////////////////////////////////////////////////////
  void func(int **arr) {
    int i,j,k,tmp;
    int **destMatrix = NULL;
destMatrix = (int**) malloc(N * sizeof(int *));

if ((destMatrix == NULL)) {
    fprintf(stderr, "out of memory\n");
    exit(2);
}

for (i = 0; i < N; i++) {
    destMatrix[i] = (int*) malloc(N * sizeof(int));

    if (destMatrix[i] == NULL) {
        fprintf(stderr, "out of memory\n");
        exit(2);
    }
}



for(i = 0; i < N; ++i) {
    free(arr[i]);
}
free(arr);
arr = NULL;
arr = destMatrix;

}
  //////////////////////////////////////////////////////////////////////////
  int main() {
   2dArr = (int**) malloc(N * sizeof(int *));
   if (2dArr== NULL) {
    fprintf(stderr, "out of memory\n");
    exit(2);
     }


for (i = 0; i < N; i++) {
    2dArr[i] = (int*) malloc(N * sizeof(int));

    if (2dArr[i] == NULL) {
            fprintf(stderr, "out of memory\n");
            exit(2);
    }
}
    func(2dArr);
    // try to print new array, access violation
 }

I free memory and try to replace the pointer. But then I have a access violation. How can I do it correctly?

  • 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-28T04:03:41+00:00Added an answer on May 28, 2026 at 4:03 am

    Couple things:

    • You need to pass the argument by reference in func:
      void func(int ***arr) {
    • Then change the way you free things:
      free((*arr)[i]);
      and
      free(*arr);
    • and finally change how you reassign arr:
      *arr = destMatrix;
    • also, don’t name variables starting with a number — it doesn’t work.
    • (also, you had ++i instead of i++ at the for loop at the end of func ) this doesn’t actually matter — see the comment below

    I debugged these issues using Valgrind, check it out.

    Working code is below:

    #include <stdio.h>
    #include <stdlib.h>
    
    #define N 10 
    //////////////////////////////////////////////////////////////////////////
    void func(int ***arr) {
      int **destMatrix = NULL;
      destMatrix = malloc(N * sizeof(int *));
    
      if ((destMatrix == NULL)) {
        fprintf(stderr, "out of memory\n");
        exit(2);
      }
    
      int i;
      for (i = 0; i < N; i++) {
        destMatrix[i] = malloc(N * sizeof(int));
    
        if (destMatrix[i] == NULL) {
          fprintf(stderr, "out of memory\n");
          exit(2);
        }
      }
    
      for(i = 0; i < N; i++) {
        free((*arr)[i]);
      }
      free(*arr);
      *arr = destMatrix;
    
    }
    //////////////////////////////////////////////////////////////////////////
    int main() {
      int **tdArr = malloc(N * sizeof(int *));
      if (tdArr== NULL) {
        fprintf(stderr, "out of memory\n");
        exit(2);
      }
    
      int i;
      for (i = 0; i < N; i++) {
        tdArr[i] = malloc(N * sizeof(int));
    
        if (tdArr[i] == NULL) {
          fprintf(stderr, "out of memory\n");
          exit(2);
        }
      }
    
      func(&tdArr);
      // try to print new array, does not get access violation
    
      //assign numbers 0-99
      int j;
      for (i=0; i<N; i++){
        for (j=0; j<N; j++){
          tdArr[i][j] = i*10+j;
        }
      }
      //print numbers
      for (i=0; i<N; i++){
        for (j=0; j<N; j++){
          printf("%d\n", tdArr[i][j]);
        }
      }
    
      //free internal arrays
      for (i=0; i<N; i++){
        free(tdArr[i]);
      }
      //free outer array
      free(tdArr);
    
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have some pointer, which I want to reinterpret as static dimension array
I have a C++ API prototype void Func(int& size); How can I translate it
Say I have a function pointer in C- library int (*fptr)(void); And I have
Let's say I have the following signature: static extern void External(int foo, IntPtr bar);
So, I have a function pointer defined as: unsigned static int (*current_hash_function)(unsigned int); And
I have several service classes that have static methods and offer a service to
Suppose I have static ip in a subnet that has DHCP server. If i
Why can we have static final members but cant have static method in an
I have several static factory patterns in my PHP library. However, memory footprint is
I have a std::vector that holds a Point struct (x,y,z and some other non-pointer

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.