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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:53:01+00:00 2026-05-24T12:53:01+00:00

I am writing a C-program where I need 2D-arrays (dynamically allocated) with negative indices

  • 0

I am writing a C-program where I need 2D-arrays (dynamically allocated) with negative indices or where the index does not start at zero. So for an array[i][j] the row-index i should take values from e.g. 1 to 3 and the column-index j should take values from e.g. -1 to 9.

For this purpose I created the following program, here the variable columns_start is set to zero, so just the row-index is shifted and this works really fine.

But when I assign other values than zero to the variable columns_start, I get the message (from valgrind) that the command “free(array[i]);” is invalid.
So my questions are:

  1. Why it is invalid to free the memory that I allocated just before?
  2. How do I have to modify my program to shift the column-index?

Thank you for your help.

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

main()
{

int **array, **array2;
int rows_end, rows_start, columns_end, columns_start, i, j;

rows_start = 1;
rows_end = 3;

columns_start = 0;
columns_end = 9;

  array = malloc((rows_end-rows_start+1) * sizeof(int *));

  for(i = 0; i <= (rows_end-rows_start); i++) {
    array[i] = malloc((columns_end-columns_start+1) * sizeof(int));
  }

  array2 = array-rows_start;                          //shifting row-index

  for(i = rows_start; i <= rows_end; i++) {
    array2[i] = array[i-rows_start]-columns_start;    //shifting column-index
  }

  for(i = rows_start; i <= rows_end; i++) {
    for(j = columns_start; j <= columns_end; j++) {
      array2[i][j] = i+j;                             //writing stuff into array
      printf("%i %i %d\n",i, j, array2[i][j]);
    }
  }

  for(i = 0; i <= (rows_end-rows_start); i++) {
    free(array[i]);
  }

  free(array);

}
  • 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-24T12:53:02+00:00Added an answer on May 24, 2026 at 12:53 pm

    When you shift column indexes, you assign new values to original array of columns: in

    array2[i] = array[i-rows_start]-columns_start;
    

    array2[i] and array[i=rows_start] are the same memory cell as array2 is initialized with array-rows_start.

    So deallocation of memory requires reverse shift. Try the following:

    free(array[i] + columns_start);
    

    IMHO, such modification of array indexes gives no benefit, while complicating program logic and leading to errors. Try to modify indexes on the fly in single loop.

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

Sidebar

Related Questions

I'm writing a program and I need to determine if bits 3 and 6
I'm writing a program in C# that will need to store a few Data
I'm writing a program (for Mac OS X, using Objective-C) and I need to
I am writing a program that needs to run a java.jar server. I need
I'm writing a 'Sudoku' program, and I need to write a method to check
I am writing an application program using swing. I need to exit from the
I'm writing a program which creates a large number of large arrays to store
I'm writing a program that essentially stores a series of scenarios. These scenarios need
I'm (re)writing a program that does generic unit test scheduling. The current program is
I need to create a 2 column array in ABAP so that a program

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.