Dynamic reallocation of a square matrix - c

i'm tryng to create a function which adds dynamically 1 row and 1 column to a square matrix everytime i need. I'm posting the code as an example, there i start with a "1x1 matrix" of integers and i try to add a row and a column 5 times to obtain a fianl 5x5 matrix, i don't understand why the OS stops immediately the execution. In the for cycle first i reallocate the "column" array of pointers adding a new one (so a new row) then for each block of it (so for each row) i reallocate others N blocks of memory. It seems that i try to access to a forbidden address of memory but i don't understand why, what's wrong?
P.S: my ennglis could not be perfect so if you don't understand what i'm trying to say i will explaind better.
#include <stdlib.h>
#include <malloc.h>
#include <stdbool.h>
#include <stdio.h>
int **M,N;
int main(int argc, char** argv) {
N = 1;
M = (int**)malloc(sizeof(int*));
M[0] = (int*)malloc(sizeof(int));
for (int i = 0; i < 5; i++) {
N++;
M = (int**)realloc(M, N * sizeof(int*));
for (int k=0; k<N; k++)
M[k] = (int*)realloc(M[k], N * sizeof(int));
}
}

Before entering the loop, you have M pointing to a single int * and M[0] pointing to a single int.
On the first iteration of the loop, you use realloc to modify M to point to an array of 2 int *. The first one still points to a single int, but the second one is uninitialized. When you then try to call realloc on M[1], it reads an uninitialized pointer invoking undefined behavior. In this case it manifests in a crash.
You need to initialize the newly added element of M to NULL so that realloc will work properly.
M = realloc(M, N * sizeof(int*));
M[N-1] = NULL;
for (int k=0; k<N; k++) {
M[k] = realloc(M[k], N * sizeof(int));
}
Also, don't cast the return value of malloc/realloc

When you use realloc to expand an array, the new expanded elements in the array are not initialized. So when you realloc M, the additional pointers to memory are undefined, not NULL, so you cannot reference the expanded elements of M[] in the second realloc, until you initialize them to NULL.
#include <stdlib.h>
#include <malloc.h>
#include <stdbool.h>
#include <stdio.h>
int **M,N;
int main(int argc, char** argv) {
N = 1;
M = (int**)malloc(sizeof(int*));
M[0] = (int*)malloc(sizeof(int));
for (int i = 0; i < 5; i++) {
N++;
M = (int**)realloc(M, N * sizeof(int*));
// Ensure the last M[] is NULL
M[N-1] = NULL;
for (int k=0; k<N; k++)
M[k] = (int*)realloc(M[k], N * sizeof(int));
}
}

Related

C how to move through 2 dimensional array based on address

I'm trying to figure out how to loop through a 2 dimensional array as a single dimensional array. Since a 2 dimensional array will occupy continuous memory, is there a way to address the two dimensional array as a single dimensional array by changing the index by 4 bytes. I'm assuming an integer array. Could some one provide an example? I tried the following but it doesn't work:
for (int i = 0; i < 2; i++){
for (int j = 0;j < 2; j++){
z[i][j] = count;
count++;
}
}
for (int i = 0; i < 4; i++)
printf("%d\n", z[i]);
2D arrays can be iterated through in a single loop like this:
#include <stdio.h>
int main()
{
int a[2][2], *p;
a[0][0] = 100;
a[0][1] = 200;
a[1][0] = 300;
a[1][1] = 400;
p = &a[0][0];
while(p!=&a[0][4])
printf("%d\n", *p++);
return 0;
}
Remember that an array index is just an offset from the first element of the array, so there is no real difference between a[0][3] and a[1][1] - they both refer to the same memory location.
Access 2D arrays like this
int *array; // note one level of pointer indirection
array = malloc(width * height * sizeof(int)); / allocate buffer somehow
for(y=0;y<height;y++)
for(x=0;x<width;x++)
array[y*width+x] = 0; // address the element by calculation
In three dimensions
int *array; // note one level of pointer indirection
array = malloc(width * height * depth * sizeof(int)); / allocate buffer somehow
for(z=0;z<depth;z++)
for(y=0;y<height;y++)
for(x=0;x<width;x++)
array[z*width*height + y*width+x] = 0; // address the element by calculation
Generally it's easier to use flat buffers than to mess around with C and C++ convoluted rules for multi-dimensional arrays. You can also of course
iterate through the entire array with a single index. If you want to set
up a 2D array, then cast the array to a single pointer, it behaves the
same way.
#define HEIGHT 50
#define WIDTH 90
int array2D[HEIGHT][WIDTH}:
int * array = reinterpret_cast<int *>(array2D):
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int arr[4][3]={{1,2,3},{4,5,6},{7,8,9},{10,11,12}};
void my_fun(int(*a)[],int m,int n)
{
for(int i=0;i<m*n;i++)
{
printf("%d\n",(*a)[i]);
}
}
int main()
{
my_fun(arr,3,4);
return 0;
}

Strange behavior while filling dinamically allocated 2D char array in C

I need to create a program that plays the game Hex on a 14x14 board.
So I created, allocated and filled the board with '-' (our pattern for empty spaces).
When I try to print the board's coordinates, I don't always get '-' but some random characters.
Also, if I try to printf array[i][j] on the createBoard function after the line "board[i][j] = '-';" I get a segmentation fault right after it prints tab[8][0].
What is causing this and how can I fix it?
My code:
#include <stdio.h>
#include <stdlib.h>
char **createBoard()
{
/*Allocates a 14x14 matrix and fills it
*with '-' to create the board.*/
int i, j;
char **board;
board = malloc(14);
if (!board) exit(1);
for (i = 0; i < 14; i++){
board[i] = malloc(14);
if (!board[i]) exit (1);
for (j = 0; j < 14; j++)
board[i][j] = '-';
}
return board;
}
int main()
{
int i, j;
char **board = createBoard();
for (i = 0; i < 14; i++)
for (j = 0; j < 14; j++)
printf("tab[%d][%d]: %c\n",i, j, board[i][j]);
return 0;
}
For starters it is not clear why you don't want to declare an array instead of allocating dynamically numerous one-dimensional arrays.
As for the code then this memory allocation
board = malloc(14);
is invalid. You have to write
board = malloc( 14 * sizeof( char * ));
Also you should free all the allocated memory in the reverse order relative to its allocation before the program ends.
Take into account that it is always better to use named constants instead of magic numbers. At least you could write either
#define N 14
before main
or
const int N = 14.
and use the variable N everywhere where you are using magic number 14.
By the way according to the C Standard function main without parameters shall be declared like
int main( void )
The variable *board is a pointer, but you only allocate one byte for each array element, which should be
#define DIM 14
board = malloc(DIM * sizeof *board);
Following that up with the second allocation
board[i] = malloc(DIM * sizeof **board);
This also allows (a) that the dimension 14 is hard coded in only one place in the program and (b) the allocation will survive if you later make the board's element a different type, for example a struct, as the program develops.

Freeing a 2-D array in C

Please bear with me as this is probably a very simple question but I am very new to C.
I am trying to malloc a specific array and then free it. However, the line:
M = malloc(N*sizeof(double *));
...doesn't work. Can somebody please explain to me why this is not working and what the solution would be? Many thanks in advance.
You should free all positions, not just the first. See this example I have wrote.
As mentioned from #Grijesh, the allocation is also wrong. The example covers allocation too. Moreover, I suggest you not to cast the return of malloc (more).
You have to think the 2D array, as a 1D array, where every cell of it is a pointer to a 1D array. A picture might help:
http://gsamaras.files.wordpress.com/2014/04/array2d-n.png
Here, 1D array that holds the pointers is to the left and every cell of it, points to another 1D array.
How many 1D arrays to the left? As many cells as you have in the left array.
Btw, Nelly I think this is not a silly question, it's something that gets beginners into trouble. ;)
EDIT:
About your new code, you had to have the same definition and declaration for matrix_free, as well as, call it as you should. What's definition, etc. ?? Answer.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#define Nmax 9000
double **makeMatrix(int N);
void free_matrix(double **M,int N);
int main(void) {
int N;
for (N = 2; N < Nmax; N *= 2) {
double **L = makeMatrix(N);
printf("yes \n");
free_matrix(L, N);
printf("woo \n");
}
return 0;
}
double **makeMatrix(int N) {
int i, j;
double **M;
M = malloc(N * sizeof(double *));
for (i = 0; i < N; i++)
M[i] = malloc(N * sizeof(double));
for (i = 1; i < N; i++) {
for (j = 1; j < N; j++) {
M[i][j] = (i) * (j) * M_PI / N;
}
}
return (M);
}
void free_matrix(double **M, int N) {
int i;
for (i = 1; i <= N; i++) {
free(M[i]);
}
free(M);
}
And then I receive the youwho output. :) But, it will stop at a certain point, because NMAX is too big! Not only NMAX is too big, but N grows really fast ( N *=). Have you done the math in a piece of paper? Too big numbers. For example, if I do N +=, then, I can go until NMAX = 9000.
Debug tip:
How do I know in which loop it reaches?
I printed out the counter of the loop, like this
printf("woo %d\n",N);
Of course, if you feel sure for yourself, then I suggest you learning the debugger.

Using 3d array outside a function

I have a function in which I make a 3D array and fill in all the values. I also have to pass a pointer to the function which will assign the memory location of the 3D array to that function so that it can be used outside of that function. Currently, I am doing something which does not seem to work, can someone guide me to the best possible resolution?
int (*arr)[4];
void assign_3D(int (*arr)[4])
{
int local[2][3][4]; //probably we should pass *local?
memset(local, 0, sizeof(int)*2*3*4); // fill the local array with numbers
arr = local;
}
printf("%d\n", arr[1][2][3]);
I know I have written horrible code above. But I am learning :).
It is not possible to assign arrays. You are also using the wrong type for the argument (int (*)[5] is not what a int [2][3][4] decays into, use int (*)[3][4] as the argument type). Once you have the correct type, you can use memcpy() to do the assignment:
#include <string.h>
#include <stdio.h>
int arr[2][3][4];
void assign_3D(int (*arr)[3][4]) {
int local[2][3][4];
memset(local, 0, sizeof(local)); //pass local here, because it is not a pointer but an array. Passing *local would only initialize the first element of the array, i. e. the first 2D slice of it.
// fill the local array with numbers
memcpy(arr, local, sizeof(local));
}
int main() {
assign_3D(arr);
printf("%d\n", arr[1][2][3]);
}
But you can also return a newly allocated array from your function:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
typedef int arrayType[2][3][4];
arrayType* create_3D() {
arrayType* result = malloc(sizeof(*result)); //here we need to dereference because result is a pointer and we want memory for the array, not the pointer.
memset(result, 0, sizeof(*result));
(*result)[1][2][3] = 7; // fill the local array with numbers
return result; //that's easy now, isn't it?
}
int main() {
arrayType* array = create_3D();
printf("%d\n", (*array)[1][2][3]);
free(array); //cleanup
}
Edit:
You mention that the size of the first dimension is not know before the function is run. In that case, you have to use the malloc() approach, but a bit differently:
#include <stddef.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
typedef int sliceType[3][4];
sliceType* create_3D(size_t* firstDimSize) {
*firstDimSize = 2;
size_t arraySize = *firstDimSize*sizeof(sliceType);
sliceType* result = malloc(arraySize);
memset(result, 0, arraySize);
result[1][2][3] = 7; // fill the local array with numbers
return result;
}
int main() {
size_t firstDim;
sliceType* array = create_3D(&firstDim);
printf("%d\n", array[1][2][3]);
free(array); //cleanup
}
There are two different ways to allocate a 3D array. You can allocate it either as a 1D array of pointers to a (1D array of pointers to a 1D array). This can be done as follows:
int dim1, dim2, dim3;
int i,j,k;
int *** array = (int ***)malloc(dim1*sizeof(int**));
for (i = 0; i< dim1; i++) {
array[i] = (int **) malloc(dim2*sizeof(int *));
for (j = 0; j < dim2; j++) {
array[i][j] = (int *)malloc(dim3*sizeof(int));
}
}
Sometimes it is more appropriate to allocate the array as a contiguous chunk. You'll find that many existing libraries might require the array to exist in allocated memory. The disadvantage of this is that if your array is very very big you might not have such a large contiguous chunk available in memory.
const int dim1, dim2, dim3; /* Global variables, dimension*/
#define ARR(i,j,k) (array[dim2*dim3*i + dim3*j + k])
int * array = (int *)malloc(dim1*dim2*dim3*sizeof(int));
To access your array you just use the macro:
ARR(1,0,3) = 4;

How to malloc a 2d jagged array using a pointer passed by reference to a function in C

I have a 2D jagged array declared in my main() block. This is to be passed to a function to have memory allocated to it. The following is the most reduced case which compiles but crashes when it runs. Where am I going wrong?
#include <stdio.h>
#include <stdlib.h>
void alloc2d(double ***p);
int main () {
double **data;
alloc2d(&data);
printf("Before assign to data\n");
data[0][0] = 0.1;
printf("After assign to data\n");
free(data);
}
void alloc2d(double ***p) {
int i, n, m;
// Get some dynamically assigned sizes
printf("Enter size: ");
scanf("%d %d", &n, &m);
// Now allocate
*p = malloc(n * sizeof(double*));
for (i = 0; i < n; i++) {
*p[i] = malloc(m * sizeof(double));
}
printf("End of alloc2d\n");
}
This reads the values but crashes when I enter low numbers (i.e. '1 1') but crashes when I enter high numbers (i.e. '10 10').
You made a very simple syntax error
*p[i] = (double*)malloc(m * sizeof(double));
should really be
(*p)[i] = (double*)malloc(m * sizeof(double));
This is because in C, [] operator has higher precedence than *.
So when you type *p[i],
it is translated into **(p + i).
This means: you are asking the compiler to calculate the address by offsetting the address of p by i * sizeof(double**), which is clearly not what you actually want.
So, in order to force the compiler to dereference p first, simply surroud *p with brackets.
Operator precedence is the answer. *p[i] is equivalent to *(p[i]). This makes you access memory that lies right after the data pointer, which will either corrupt some other variables on the stack, or crash completely.
You were looking for (*p)[i], which will be the i-th entry in the newly allocated array.
What your alloc2d() allocates is not really a 2D array, but:
1 1D n-long array of pointers to double
n 1D m-long arrays of doubles
Multi-dimensional arrays in C are only possible, if all but the last of the dimensions are known at compile-time:
double a[5][11];
Maybe, this program can help you understand... Note, how COLUMNS is a compile-time constant, even if rows is a run-time variable:
#include <stdio.h>
#include <stdlib.h>
#include <sysexits.h>
#include <err.h>
typedef double myrow_t[11]; /* 11 columns */
#define COLUMNS (sizeof(myrow_t)/sizeof(double))
static unsigned
alloc2d(myrow_t **pd)
{
unsigned int rows;
printf("Enter the number of rows: ");
while (scanf("%u", &rows) != 1)
printf("\ninvalid input, please, try again: ");
*pd = malloc(rows * sizeof(**pd));
if (*pd == NULL)
err(EX_TEMPFAIL, "Out of memory");
return rows;
}
int
main()
{
myrow_t *d;
unsigned int row, column, rows;
rows = alloc2d(&d);
for (row = 0; row < rows; row++)
for (column = 0; column < COLUMNS; column++)
d[row][column] = row * column;
for (row = 0; row < rows; row++) {
printf("Row %3d:\t", row);
for (column = 0; column < COLUMNS; column++)
printf("%.0f\t", d[row][column]);
puts("");
}
free(d);
return 0;
}

Resources