reading and printing matrix using pointer to pointer method - c

I wrote a program for reading and printing the matrix without making use of array of pointers. The program is reading and printing matrix correctly, but it crashes after the execution. There are no warnings in the program. Unable to find what is wrong with the program. I am using Codeblock+mingw
Also is this method of using pointer to pointer for two dimensional matrix is OK or any better method?
#include <stdio.h>
#include <malloc.h>
int main()
{
int numCols=2;
int numRows=2;
int *cols;
int rowCount;
int colCount;
cols=(int*) malloc(numCols*sizeof(int));
int **rows;
rows= (int**) malloc(numRows*sizeof(cols));
printf("Filling the rows and Columns\n");
for(rowCount=0;rowCount<numRows;rowCount++)
{
printf("Fill Row Number %d\n",rowCount);
for(colCount=0;colCount<numCols;colCount++)
{
printf("Enter the value to be read\n");
scanf("%d",(*(rows+rowCount)+colCount));
}
}
// Printing the values
for(rowCount=0;rowCount<numRows;rowCount++)
{
printf("Print Row Number %d\n",rowCount);
for(colCount=0;colCount<numCols;colCount++)
{
printf("%d\t",*(*(rows+rowCount)+colCount));
}
printf("\n");
}
free(rows);
free(cols);
return 0;
}

You're not allocating memory in the right manner. Your program crashes because of illegal memory access it makes.
What's Cols array for; as you're never storing any integer in it and its extra. Second rows is a an array int* not int. One of the ways to allocate a 2D array will be
int** matrix;
matrix = (int **)malloc(sizeof(int *));
matrix[0] = (int *)malloc(sizeof(int) * c * r);
//To access elements
for(rowCount=0;rowCount<numRows;rowCount++)
{
printf("Fill Row Number %d\n",rowCount);
for(colCount=0;colCount<numCols;colCount++)
{
printf("Enter the value to be read %d %d \n", rowCount, colCount);
scanf("%d",(*matrix+rowCount*numCols)+colCount);
}
}
//free it as
free(matrix);
Its always a good practice to allocate and access row-wise in C to so that memory fetches are not bottle-neck.
Update
Yes you can allocate memory like this.
rows= (int**) malloc(numRows * numCols * sizeof(int)); //Yes sizeof(int).
Its perfectly legal statement in C. It will allocate numRows * numCols sized array of integer pointers with each element of size equal to sizeof(int).
It may cause trouble on 64bit platform where pointers are 8 byte long.
Even assuming it to be 32bit platform there is another problem. How will you dereference rows for your intended purpose? rows[i] will be a pointer to integer i.e. int* type; but doing scanf on that will give you a segmentation fault as row[i] will contain some garbage value and might lead you to some unwanted area in memory.

Related

Arrays and malloc in C

Hello I am new to C and I need someone to explain concepts to me. I am a JAVA programmer and I am trying to write a program in C. My current issue is trying to initialize an array with an unknown number. I know in C an array has to be initialized with a number instead of a variable like you can in Java. My question is if I can do this in Java:
int i = 0;
char array [i];
void f(){
\\some code
i++;
}
How can I do this in C? I'm trying to fill an array with certain strings that I get from a file. I don't know how many I will be getting from the file however. I have tried reading about malloc but in one tutorial it says:
int *pointer;
pointer=malloc(2*sizeof(int));
is equivalent to
int array[2];
But I'm looking for a way to do this while increment the array.
First to mention, malloc() and family is used for dynamic (runtime) memory allocation whereas int arr[2] usually denotes compile time memory allocation. They are not exactly equivalent.
However, if you want to resize the allocated memory on-the-fly, you're on right track. What you need to do next is to use realloc() to re-size the previously allocated memory location.
You can read the man page for more details.
Also, while using dynamic memory in C, you need to keep in mid that there is no garbage collector in C. You need to free() up every bit of memory allocated by you.
I know in C an array has to be initialized with a number instead of a variable like you can in Java
In C99 and beyond, variable initiated arrays are available.
My current issue is trying to initialize an array with an unknown number.
and:
But I'm looking for a way to do this while increment the array.
If you have an unknown number of elements at run-time, you can write a function to create (and free) memory, passing the relevant arguments as you need them. Here is an example of a function to create (and free) a 2 dimensional array of ints:
int ** Create2Dint(int **arr, int cols, int rows)
{
int space = cols*rows;
int y;
arr = calloc(space, sizeof(int));
for(y=0;y<cols;y++)
{
arr[y] = calloc(rows, sizeof(int));
}
return arr;
}
void free2DInt(int **arr, int cols)
{
int i;
for(i=0;i<cols; i++)
if(arr[i]) free(arr[i]);
free(arr);
}
If, during execution, you need to change the allocation of memory (change the size of the array) you can use realloc(), implemented here in similar fashion:
int ** Realloc2D(int **arr, int cols, int rows)
{
int space = cols*rows;
int y;
arr = realloc(arr, space*sizeof(int));
for(y=0;y<cols;y++)
{
arr[y] = calloc(rows, sizeof(int));
}
return arr;
}
Usage example:
(execute with two integer command line arguments, both > 0)
int main(int argc, char *argv[])
{
int **array = {0};
int cols, rows;
cols = atoi(argv[1]);
rows = atoi(argv[2]);
array = Create2Dint(array, cols, rows);
//Do stuff here to use array
//Memory requirements change during runtime:
cols += 20;
rows += 50;
array = Realloc2D(array, cols, rows);
//use array again...
//When you are finished with the memory, free it:
free2DInt(array, cols);
return 0;
}

Crash while re-organizing 1d buffer as 2d array

I have a 1d buffer which i have to re-organize to be accessed as a 2d array. I have pasted my code below:
#include <stdlib.h>
#include <stdio.h>
void alloc(int ** buf, int r, int c)
{
int **temp=buf;
for(int i=0; i<r; i++)
buf[i]=(int *)temp+i*c;
}
void main()
{
int *buffer=(int *)malloc(sizeof(int)*100);
int **p = (int**) buffer;
alloc(p, 4, 4);
//for(int i=0;i<r;i++)
//for(int j=0;j<c;j++)
// printf("\n %p",&p[i][j]);
p[0][3]=10;
p[2][3]=10;
p[3][2]=10; //fails here
printf("\n %d", p[2][3]);
}
The code is crashing when i make the assignment.
I have ran the code for different test cases. I have observed that the code crashes when there is an assignment to p[0][x] followed by assignment to p[x][anything] with the code crashing at the second assignment. This crash is seen only when the first index of the first assignment is 0 and for no other indices with the crash happening at the second assignment having the first index equal to the second index of the first assignment.
For example, in the above code crash happens at p[3][2] after p[0][3] has been executed. If i change the first assignment to p[0][2] then crash would happen at p[2][3]( or p[2][anything] for that matter).
I have checked the memory pointed to by p, by uncommenting the double for loop, and it seems to be fine. I was suspecting writing at illegal memory locations but that has been ruled out by the above observation.
The problem is that your 2D array is actually an array of pointers to arrays. That means you need to have space for the pointers. At the moment you have your pointers in positions 0-3 in the array, but p[0] is also pointing to position 0. When you write to 'p[0,3]' you are overwriting p[3].
One (tempting) way to fix it is to allow the pointers room at the start of the array. So you could change your alloc method to allow for some space at the front. Something like:
buf[i] = (int *)(temp+r) + i*c;
Note the +r adding to the temp. It needs to be added to temp before it is cast as you can't assume int and int * are the same type.
I would not recommend this method as you still have to remember to allocate extra space in your original malloc to account for the array of pointers. It also means you aren't just converting a 1D array to a 2D array.
Another option would be to allocate your array as an array of pointers to individually allocated arrays. This is the normal way to allocate 2D arrays. However this will not result in a contiguous array of data as you have in your 1D array.
Half way between these two options, you could allocate an extra array of pointers to hold the pointers you need, and then point them to the data. Change your alloc to something like:
int **alloc(int * buf, int r, int c)
{
int **temp = (int **)malloc(sizeof (int *)* r);
for (int i = 0; i<r; i++)
temp[i] = buf + i*c;
return temp;
}
then you call it like:
int **p = alloc(buffer, 4, 4);
you also need to free up the extra buffer.
This way your data and the pointers you need to access it are kept separate and you can keep your original 1D data contiguous.
Note that you don't need to cast the result of malloc in c, in fact some say that you shouldn't.
Also note that this method removes all of the requirement for casting pointers, anything that removes the need for a cast is a good thing.
I think that your fundamental problem is a misconception about 2D arrays in C (Your code is C, not C++).
A 2D array is a consecutive memory space , and the size of the inner array must be known in advance. So you basically cannot convert a 1D array into a 2D array unless the size of the inner array is known at compile time. If it is known, you can do something like
int *buffer=(int *)malloc(sizeof(int)*100);
typedef int FourInts[4];
FourInts *p = (FourInts *)buffer;
And you don't need an alloc function, the data is already aligned correctly.
If you don't know the size of the inner array in advance, you can define and allocate an array of arrays, pointing into the 1D buffer. Code for that:
int ** alloc(int * buf, int r, int c)
{
int **array2d = (int **) malloc(r*sizeof(int *));
for(int i=0; i<r; i++)
array2d[i] = buf+i*c;
return array2d;
}
void _tmain()
{
int *buffer=(int *)malloc(sizeof(int)*100);
int **p = alloc(buffer,4,4);
p[0][3]=10;
p[2][3]=10;
p[3][2]=10; //fails here
printf("\n %d", p[2][3]);
free(buffer);
free(p);
}
But it would have been easier to simply build an array of arrays without using the buffer. If you could use C++ instead of C, then everything could be easier.
If you already have a 1D block of data, the way to make it accessible as a 2D array is to create an array of pointers - one for each row. You point the first one to the start of the block, the next one is offset by the number of columns, etc.
int **b;
b = malloc(numrows*sizeof(int*));
b[0]=temp; // assuming temp is 1D block
for(int ii=1; ii<numrows;ii++)
b[ii]=b[0]+ii*numcols;
Now you can access b[i][j] and it will point to your original data. As long as number of rows and columns are known at run time this allows you to pass variable length 2D arrays around. Remember that you have to free the vector of pointers as well as the main data block when you are done or you will get a memory leak.
You will find examples of this if you google nrutil.c - this is derived from the trick Numerical Recipes in C uses.
This function prototype should be:
void alloc(int *buf[][], int r, int c) //buf[][] <=> **buf, but clearer in this case
{
//*(buf[i]) =
...
}
If you want to work on the same array you have to pass a pointer to this 2D array (*[][]).
The way you do it now is just working on a copy, so when you return it's not modified.
You should also initialize your array correctly :
p = malloc(sizeof(int *[]) * nb of row);
for each row
p[row] = malloc(sizeof(int []) * nb of col);

Allocate a matrix in C using malloc

I wrote a C code that usea a matrix of double:
double y[LENGTH][4];
whith LENGTH=200000 I have no problem.
I have to increase the numbers of rows to LENGTH=1000000 but when I enter this value and execute the program it returns me segmentation fault.
I tried to allocate more memory using malloc:
double **y = (double **)malloc(LENGTH * sizeof(double*));
for(int i = 0; i < LENGTH; i++){
y[i] = (double *)malloc(4 * sizeof(double));
}
I run the the code above and after some second of calculations it still gives me "segmentation fault".
Could anyone help me?
If you want a dynamic allocated 2D array of the specified row-width, just do this:
double (*y)[4] = malloc(LENGTH * sizeof(*y));
There is no need to malloc each row in the matrix. A single malloc and free will suffice. Only if you need dynamic row width (each row can vary in width independent of others) or the column count is arbitrary should a nested malloc loop be considered. Neither appears to be your case here.
Notes:
Don't cast malloc in C programs
Be sure to free(y); when finished with this little tryst.
The reason your statically allocated array is segfaulting with a million elements is (presumably) because it's being allocated on the stack. To have your program have a larger stack, pass appropriate switches to your compiler.
ProTip: You will experience less memory fragmentation and better performance if you flip your loop around, allocating
(double *)malloc(LENGTH * sizeof(double));
four times. This will require changing the order of your indices.
I ran the the code with this definition and after some second of calculations it still gives me "segmentatin fault"
If you're getting a segmentation fault after allocating the memory, you're writing outside of your memory bounds.
I run this code
#include <stdio.h>
#include <stdlib.h>
// We return the pointer
int **get(int N, int M) /* Allocate the array */
{
/* Check if allocation succeeded. (check for NULL pointer) */
int i, **table;
table = malloc(N*sizeof(int *));
for(i = 0 ; i < N ; i++)
table[i] = malloc( M*sizeof(int) );
return table;
}
void free2Darray(int** p, int N) {
int i;
for(i = 0 ; i < N ; i++)
free(p[i]);
free(p);
}
int main(void)
{
const int LENGTH = 1000000;
int **p;
p = get(LENGTH, 4);
printf("ok\n");
free2Darray(p ,LENGTH);
printf("exiting ok\n");
return 0;
}
and it was executed normally.
I got the code from my pseudo-site.
You should not cast what malloc returns. Why?
Also notice, that since you need a dynamic allocation only for the number of rows, since you know the number of columns. So, you can modify the code yourself (so that you have some fun too. :) )
I hope you didn't forget to **free** your memory.

Runtime error in dynamically allocating 2D array in C

I am trying to dynamically allocate a 2D array, put some values, and print output. However it seems that I am making mistake in getting input to program in atoi() function.
Basically when we assign a static 2D array, we declare it as say int a [3][3]. So 3*3 units if int, that much memory gets allocated. Is same thing holds for allocating dynamic array as well?
Here is my code:
#include<stdio.h>
#include<stdlib.h>
int main(int arg,char* argv)
{
int rows = atoi(argv[1]);
int col = atoi(argv[2]);
int rows =3;
int col=3;
int i,j;
int (*arr)[col] = malloc(sizeof (*arr)*rows);
int *ptr = &(arr[0][0]);
int ct=1;
for (i=0;i<rows;i++)
{
for(j=0;j<col;j++)
{
arr[i][j]=ct;
ct++;
}
}
printf("printing array \n");
for (i=0;i<rows;i++)
{
for(j=0;j<col;j++)
{
printf("%d \t",arr[i][j]);
}
printf("\n");
}
free(arr);
return (0);
}
Program crashes in runtime. Can someone comment?
The first issue I see is this line:
int (*arr)[rows][col] = malloc(sizeof (*arr) * rows);
This is not problematic at all because you are in fact allocating more memory than you need. This would suffice:
int (*arr)[rows][col] = malloc(sizeof (*arr));
sizeof *arr is enough because *arr is of type int [rows][cols]; the memory you want is exactly the size of that array. The sizeof operator, when applied to arrays, gives you the count for the whole array.
The main problem with your code, however, is how you use arr. You are indexing it with arr[i][j], but instead, you should be using (*arr)[i][j], because arr is not an array, it's a pointer to an array. You need to dereference it before any further indexing - as simple as that. arr[i][j] is equivalent to *(*(arr+i)+j). Note that i should be an offset into *arr, not an offset on arr. That's why you need to dereference arr before indexing.
Since you're already using variable-length arrays, you may take advantage of that:
int (*arr)[col] = malloc(sizeof *arr * rows);
This way you can simply access elements with the usual syntax arr[i][j] without worrying about pointers and dereferences, pointer arithmetic will do all the work for you.
Also since indexes start from 0 your tests should look like i < rows and j < col.
And you have some minor errors for the wrong main declaration and the second printf.

Attempting to dynamically allocate a multidimensional array in C results in a crash

For a multitude of reasons, I'd like to allocate multidimensional arrays in contiguous chunks of memory. I can do this by allocating them manually, eg:
t.versions=(char***)malloc(sizeof(char**)*4);
t.versions[0]=(char**)malloc(sizeof(char*)*t.size*4);
t.versions[0][0]=(char*)calloc(t.size*t.size*4,sizeof(char));
for (i=1; i<t.size*4; ++i)
t.versions[0][i]=t.versions[0][i-1]+t.size;
for (i=1; i<4; ++i)
t.versions[i]=t.versions[i-1]+t.size;
Among other benefits, this solution simplifies freeing the allocated memory:
void contiguous_array_free(void** ptr, int depth)
{
int *ptr_d;
ptr_d=(int*)*ptr;
if (depth>1)
contiguous_array_free((void**)ptr_d, depth-1);
free(ptr);
}
//(elsewhere in the code)
contiguous_array_free((void**)(*tile).versions, 3);
Now, I've got a small problem with allocating those arrays - while the approach posted above does work, ideally I'd like to have a generic solution that allows me to allocate those arrays with a single function call.
However, my attempt to achieve that goal results in the program crashing every time the array contents are used.
//dimension points to a 1-dimensional array of integers
//specifying the size in each array dimension
void* contiguous_array_alloc(int* dimension, int depth, int size)
{
int i;
char** ptr;
if (depth==1)
{
ptr=(char**)malloc(*dimension*size);
return ptr;
}
ptr=(char**)malloc(*dimension*sizeof(char*));
*(dimension+1)*=*dimension;
ptr[0]=(char*)contiguous_array_alloc(dimension+1, depth-1, size);
*(dimension+1)/=(*dimension);
for (i=1; i<*dimension; ++i)
ptr[i]=ptr[i-1]+(*(dimension+1)*size);
return (void*)ptr;
}
//(later in the code) (
int dimension[3];
dimension[0]=4;
dimension[1]=t.size;
dimension[2]=t.size;
t.versions=(char***)contiguous_array_alloc(&dimension[0], 3, sizeof(char));
Adding some debug messages into the code seems to indicate the elements are allocated correctly:
Allocating [4][9][9] array of size 1 elements;
malloc()ating 16 byte array for 4 pointers;
Allocated pointer array to level 2 at 003E29E8;
Allocating [36][9] array of size 1 elements;
malloc()ating 144 byte array for 36 pointers;
Allocated pointer array to level 1 at 003E5728;
Allocating [324] array of size 1 elements;
324 byte data array at 003E57C0;
Pointed data at 003E57C0;
increasing every pointer by 9;
Returning allocated array;
Pointed data at 003E5728;
increasing every pointer by 9;
Returning allocated array;
Allocated contiguous array at 003E29E8;
What is causing this behavior? I've checked the code several times and have no idea what I've done wrong.
I think there is something wrong for ptr[i]=ptr[i-1]+(*(dimension+1)*size); this kind of pointer operation usage does not make sense. I modified the code as below which passed the test for 4-dimension array.
//dimension points to a 1-dimensional array of integers
//specifying the size in each array dimension
void* contiguous_array_alloc(int* dimension, int depth, int size) {
int i;
if (depth==2) {
char ** ptr=(char **)malloc(*dimension * sizeof(void*));
ptr[0]=(char *)malloc(*dimension * dimension[1] * size);
for (i=1; i<*dimension; ++i) {
ptr[i]=ptr[i-1]+(*(dimension+1) * size);
}
return (void*)ptr;
} else {
void ***ptr=(void***)malloc(*dimension * sizeof(void*));
*(dimension+1)*=(*dimension);
ptr[0]=contiguous_array_alloc(dimension+1, depth-1, size);
*(dimension+1)/=(*dimension);
for (i=1; i<*dimension; ++i) {
ptr[i]=ptr[i-1]+(*(dimension+1));
}
return (void*)ptr;
}
}
All you need for an a ⨉ b ⨉ c ⨉ d array of int is:
int (*p)[b][c][d] = calloc(a, sizeof *p);

Resources