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

  • Home
  • SEARCH
  • 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 674791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:44:45+00:00 2026-05-14T00:44:45+00:00

I have a 2D dynamic array. I enter a line of 0’s after line

  • 0

I have a 2D dynamic array.
I enter a line of 0’s after line which has a biggest number:

void InsertZero(int **a, int pos){
    int i, j;
    a = (int**)realloc(a, n * sizeof(*a));
    a[n-1] = (int*)calloc(n, sizeof(**a));
    d = 0;
    for(i = n-1; i > pos; i--){
        for(j = 0; j < n; j++){
            a[i][j] = a[i-1][j];
            printf("%d ", a[i][j]);
        }
    }

    for(i = 0; i < n; i++){
        a[pos][i] = 0;
    }
}

If i make a size of array 3, 5, 7, 9, … it works correctly. But if a number of lines is 2, 4, 6, … , it is an access violation error, when i try to print my array:

void Print(void){
    int i, j;
    for(i = 0; i < (n-d); i++){
        for(j = 0; j < n; j++){
            printf("%d\t", arr[i][j]);
        }
        printf("\n");
    }
}

code: http://codepad.org/JcUis6W4

  • 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-14T00:44:45+00:00Added an answer on May 14, 2026 at 12:44 am

    Looking at this I cannot make sense of…. Look at comment 1, you have n set somewhere to realloc a block of memory which a is of type int ** – a double pointer, how are you calling this function? Secondly, comment 2, Why did you call calloc when the realloc on a double pointer was called previously…? Assume n has value of 5, then, realloc is called on double pointer a, meaning a[0][1]..a[4][1], now calloc is called thus a[4] has a new block of memory…

    void InsertZero(int **a, int pos){
        int i, j;
    
        /* 1. */
        a = (int**)realloc(a, n * sizeof(*a)); 
        /* Bzzzzt....if realloc failed, a gets overwritten! */
    
        /* 2. */
        a[n-1] = (int*)calloc(n, sizeof(**a));
    
        /* 3. */
        d = 0;
    
        /* 4. */
        for(i = n-1; i > pos; i--){
            for(j = 0; j < n; j++){
                a[i][j] = a[i-1][j];
                printf("%d ", a[i][j]);
            }
        }
    
        for(i = 0; i < n; i++){
            a[pos][i] = 0;
        }
    }
    

    Comment 3, what is d used for – useless variable?
    Comment 4, you are under the presumption that the block of memory has array subscripts [0][0] to [4][4] if n had a value of 5!

    Can you clarify all this?

    Edit: Looking at it again… it is likely that a got overwritten when the call to realloc failed! I recommend this section of code to counteract this

        int **tmpA;
        tmpA = (int**)realloc(a, n * sizeof(*a)); 
        if (tmpA != NULL){
            a = tmpA;
            ....
            a[n-1] = (int*)calloc(n, sizeof(**a));
            for(i = n-1; i > pos; i--){
                .... 
            }
    
            for(i = 0; i < n; i++){
                ....
            }
        }   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form with a stage that has a dynamic number of groups
I have dynamic array filled with bytes, which are read from .raw file with
I have a dynamic array of strings, the elements of which I would like
I have an array of colors having dynamic values which depends on database. now
We're looking at doing a MOSS 2007 site which will have a fairly dynamic
I have a dynamic array. But initially I am not knowing the length of
I currently have the dynamic array: char *myData[500][10]; //myData is the name of an
I need to have a dynamic array, so I need to allocate the necessary
I have a dynamic array of structures, so I thought I could store the
I have dynamic array in struct and a method that uses the dynamic array.

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.