int main()
{
int r;
scanf("%d", &r);
int **arr = (int *)malloc(r * r * sizeof(int));
*(*(arr + r) + r);
for (int i = 1; i <= r; i++)
{
for (int j = 1; j <= r; j++)
{
printf("Enter element %d.%d: \n", i,j);
scanf("%d", &arr[i-1][j-1]);
}
}
getch();
}
so this recently happened, basically what I want is to append matrix elements to 2d array, but it says
'Exception thrown at 0x0F1B97AE (ucrtbased.dll) in Matrix.exe: 0xC0000005: Access violation writing location 0xCDCDCDCD'
don`t know what to do :( help please
You're using a malloc'ed block of memory as a C multi-dimensional array. Instead, you need to use a single set of square brackets.
Instead of arr[i-1][j-1]
, you need something like arr[i * r + j]
.
I'm surprised that most compilers would accept this by default because you use an int *
to initialize and int **
.
The problem is that you don't allocate or create a "two-dimensional" array. Your memory allocation allocates one array of r * r
elements.
This single array can't be used as an array of arrays.
The usual solution to create a dynamic array of arrays is to create a jagged array:
int **arr = malloc(r * sizeof(int *));
for (unsigned i = 0; i < r; ++i)
{
arr[i] = malloc(r * sizeof(int));
}
You can use a large single array in a "two-dimensional" way, but you need to use some other arithmetic to access the elements of it:
arr[i * r + j]
[Note that the above requires zero-based indexes i
and j
]
int **arr = (int *)malloc(r * r * sizeof(int));
You can't create a 2D array with a single allocation like that, at least not one you can access with a int **
. What you have instead is space for r * r
, objects of type int
which can be accessed via a int *
and some additional arithmetic.
When you later do this:
*(*(arr + r) + r);
The first dereference is fine, since arr
points to an allocated buffer. The second one is not, however, because you read an uninitialized value from that allocated buffer and attempt to use to as a valid address. This invokes undefined behavior which in this cause results in a crash.
You need to allocate space for an array of int *
, they for each of those allocate an array of int
:
int **arr = malloc(r * sizeof(int *));
for (int i=0; i<r; i++) {
arr[i] = malloc(r * sizeof(int));
}
Just forget all about this int**
nonsense and allocate a 2D array instead:
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
int r;
scanf("%d", &r);
int (*arr)[r] = malloc( sizeof(int[r][r]) );
for (int i = 0; i<r; i++)
{
for (int j = 0; j<r; j++)
{
arr[i][j] = 1; // some sort of input here
printf("%d ", arr[i][j]);
}
printf("\n");
}
free(arr);
}
Output example with input 5:
5
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
For this to work, you need a standard-compliant C compiler from this millennium.
More information: Correctly allocating multi-dimensional arrays.
User contributions licensed under CC BY-SA 3.0