Segmentation fault, 2-Dimensional Matrix, malloc - c

I'm trying to create a 2-Dimensional array using malloc.
My code seems correct but when I try to set values, I receive "Segmentation Fault" message.
#include <stdio.h>
#include <stdlib.h>
int main(){
int i, j;
int **m = (int **) malloc(5 * sizeof(int));
if(m == NULL){
printf("Error");
getchar();
exit(-1);
}
for(i = 0; i < 5; i++){
m[i] = (int *) malloc(5 * sizeof(int));
if(m[i] == NULL){
printf("Error");
getchar();
exit(-1);
}
}
for(i = 0; i < 5; i++){
for(j = 0; j < 5; j++){
printf("%d %d\n", i, j);
m[i][j] = 0;
}
}
for(i = 0; i < 5; i++){
for(j = 0; j < 5; j++){
printf("%d ", m[i][j]);
}
printf("\n");
}
}
Thanks.

Change
int **m = (int **) malloc(5 * sizeof(int));
to
//---------------------------------------v
int **m = (int **) malloc(5 * sizeof(int *));
Your code will fail where size of int is not equal to size of pointer variable.

some systems might have pointer size not equal to size of int. In your case your assuming that pointer is of size int
int **m = (int **) malloc(5 * sizeof(int));
change it to
int **m = malloc(5 * sizeof(int *));
always free memory after use. it will lead to memory leak.
Also don't cast when using malloc Check here

Related

Dynamic memory allocation for two-dimensional arrays

I want to allocate memory for a two-dimensional array (matrix) and write the sums of the diagonals in a separate one-dimensional array. So my code has an array of pointers to pointers,
int N, ** matrix = NULL;
matrix = (int**) malloc(sizeof(int*) * N);
I fill it and then I create an array to store the sums of the diagonals,
int diag = 2 * N - 1;
int *diagonals = NULL;
diagonals = (int*)malloc(sizeof(int) * diag);
but when I want to write a value into an array, something goes wrong, the values just don't get written into the array; I don't know why.
Here is my code:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main() {
srand(time(NULL));
int N, ** matrix = NULL;
printf("Input the number of rows\n");
scanf_s("%d", &N);
printf("\n");
// Memory allocation for the array of pointers to pointers
matrix = (int**) malloc(sizeof(int*) * N);
if (matrix != NULL)
{
for (int i = 0; i < N; i++)
*(matrix + i) = (int*)malloc(sizeof(int) * N);
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
matrix[i][j] = rand() % 14 - 4;
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
printf("\n");
int diag = 2 * N - 1;
int *diagonals = NULL;
diagonals = (int*)malloc(sizeof(int) * diag);
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
diagonals[i+j] += matrix[i][j];;
}
}
for (int i = 0; i < diag; i++) {
printf("diagonals[%d] - %d\n",i, *(diagonals+i));
}
}
else
printf("Not enough memory.. oops..\n");
}
The content of diagonals is allocated with malloc() which does not initialize the memory. You should use calloc() which initializes the memory with zeros:
diagonals = calloc(diag, sizeof *diagonals);
The following loop assumes that each element of diagonals was initialized to zero, but you performed no such initialization. As a result, they are uninitialized and will contain whatever value happens to be sitting in the newly-allocated memory.
diagonals = malloc(sizeof(int) * diag);
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
diagonals[i+j] += matrix[i][j];;
}
}
You have several options for zero-initialization:
Use memset to zero the memory, after allocation:
diagonals = malloc(sizeof(int) * diag);
memset(diagonals, 0, sizeof(int) * diag);
Initialize values in a loop:
diagonals = malloc(sizeof(int) * diag);
for (int i = 0; i < diag; i++) diagonals[i] = 0;
Allocate with calloc:
diagonals = calloc(diag, sizeof(int));
Note that in all cases, you should be checking the result of allocation. If it fails and returns NULL, you should not attempt to access memory via that pointer.
It does not answer the question but I would suggest using real 2D arrays instead of arrays of pointers. To dynamically allocate the 2D array you need to use a pointer to array.
int (*matrix)[N] = malloc(N * sizeof(*matrix));
You will have only one allocation and one free. Fever levels of indirection.

Not sure why I am getting a segmentation fault

#include <stdio.h>
#include <stdlib.h>
int ** zeros(int rows, int cols) {
int ** array;
array = (int **)malloc(rows * sizeof(int));
for (int i = 0; i < rows;i++) {
array[i] = (int *)malloc(cols * sizeof(int));
}
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
array[r][c] = 0;
}
}
return array;
}
int main(void) {
// declare variables
int rows = 3;
int cols = 3;
int ** zeroArray = zeros(rows, cols);
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
printf("%d ",zeroArray[r][c]);
}
printf("\n");
}
return 0;
}
I have this code and when the value of rows is greater than 4 I get a segmentation fault. cols can be anything and it works, I've tested where the pointers are adressed to and everything seems fine but it just breaks when rows is 5 or more.
This:
array = (int **)malloc(rows * sizeof(int));
for (int i = 0; i < rows;i++) {
array[i] = (int *)malloc(cols * sizeof(int));
}
is wrong, it assumes sizeof(int) to be equal to sizeof (int *) which is not a good move on modern systems.
Make it:
array = malloc(rows * sizeof *array);
that removes the manual "guessed" type, and simply uses the size of whatever array points at, which is of course int *.

Confusing Segmentation Fault When Reading From File

So, I'm trying to read a file of integers into 2 separate matrices. The first matrix is read in perfectly fine. Then the second one tries to read in from the file, gets to the last line and gets to a segfault. I have looked over the code about a bazillion times and can't figure out why I'm getting this segfault. Any help would be helpful!
Relevant code is pasted below:
int** allocation_matrix;
int** request_matrix;
allocation_matrix = (int **) malloc(num_processes * sizeof(int));
request_matrix = (int **) malloc(num_processes * sizeof(int));
for (i = 0; i < num_processes; i++)
{
allocation_matrix[i] = (int *) malloc(num_resources * sizeof(int));
request_matrix[i] = (int *) malloc(num_resources * sizeof(int));
}
for (i = 0; i < num_processes; i++)
{
for (j = 0; j < num_resources; j++)
{
fscanf(fp, "%d", &allocation_matrix[i][j]);
}
}
for (i = 0; i < num_processes; i++)
{
for (j = 0; j < num_resources; j++)
{
fscanf(fp, "%d", &request_matrix[i][j]);
printf("%d ", request_matrix[i][j]);
}
printf("\n");
}
num_processes * sizeof(int) is the wrong size as the wrong type was used.
Instead of trying to use the right type with the pointer, determine the size based on the de-referenced pointer. Easier to code right, review and maintain.
// allocation_matrix = (int **) malloc(num_processes * sizeof(int));
allocation_matrix = malloc(sizoef *allocation_matrix * num_processes);

how to implement matrix functions in C

I am trying to make some simple functions that does some operations on matrixes. But i can`t undestand why the following code is not working!. Can you please help me to understand what is wrong here and what should i do???
void create(int*** p, const int n)
{
*p = (int**)calloc(n, sizeof(int));
if(*p == NULL){
printf("Error1");
}
int i;
for(i = 0; i < n; i++){
(*(p[i]) = (int*)calloc(n, sizeof(int)));
if(*(p[i]) == NULL){
printf("Error2");
}
}
}
void initializeMatrix(int*** p, const int n)
{
int i, j;
for(i = 0; i < n; i++){
for(j = 0; j < n; j++){
scanf("%d", &((p[i][j])));
}
}
}
void show(const int** p, const int n)
{
int i, j;
for(i = 0; i < n; i++){
for(j = 0; j < n; j++){
printf("%d ", ((p)[i][j]));
}
printf("\n");
}
}
int main()
{
int** p = NULL;
create(&p, 2);
initializeMatrix(&p, 2);
show(p, 2);
return 0;
}
Problem 1
You are using *(p[i]) in create. it should be (*p)[i].
void create(int*** p, const int n)
{
*p = (int**)calloc(n, sizeof(int));
if(*p == NULL){
printf("Error1");
}
int i;
for(i = 0; i < n; i++){
// FIXED
(*p)[i] = (int*)calloc(n, sizeof(int)));
// FIXED
if( (*p)[i] == NULL){
printf("Error2");
}
}
}
Problem 2
You are passing the wrong size to first calloc in the function. You need to use sizeof(int*). After all, you want the returned value to be a pointer to n pointers to int, not a pointer to n objects of type int.
This is a critical error that leads to undefined behavior unless sizeof(int) is the same as sizeof(int*) on your platform.
Change the line to
*p = (int**)calloc(n, sizeof(int*));
More importantly, you can make that function a bit easier by returning a pointer. It will be easier to write such a function.
int** create(int n)
int **p = calloc(n, sizeof(*p));
if(p == NULL){
printf("Error1");
}
int i;
for(i = 0; i < n; i++){
p[i] = calloc(n, sizeof(*p[i])));
if( p[i] == NULL){
printf("Error2");
}
}
return p;
}
and use it as:
int** p = create(2);

int** matrix segmentation fault

I am trying to pass a matrix by reference and then initialize it dynamically. This is just an example:
int main(int argc, char* argv[]) {
int** matrix = NULL;
initialize(&matrix, 8);
return 0;
}
void initialize(int*** matrix, int size) {
*matrix = (int**) calloc(size, sizeof(int*));
for (int i = 0; i < size; ++i) {
*matrix[i] = (int*) calloc(size, sizeof(int)); // cashes here with segmentation fault
}
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
*matrix[i][j] = 5; // some number, doesn't matter for now
}
}
}
I have also tried the alternative, saving the matrix in a contiguous memory space:
*matrix = (int**) calloc(size, sizeof(int*));
*matrix[0] = (int*) calloc(size * size, sizeof(int));
for (int i = 1; i < size; ++i) {
*matrix[i] = *matrix[0] + size * i; // crashes here, segmentation fault
}
Yet the same error pops. Never on index 0, always on index 1. I don't understand, what am I doing wrong?
Any kind of help will be greatly appreciated!
Kind regards,
Raul.
*matrix[i] = (int*) calloc(size, sizeof(int));
is interpreted as:
*(matrix[i]) = (int*) calloc(size, sizeof(int));
That's why you are seeing segmentation fault.
Use:
(*matrix)[i] = (int*) calloc(size, sizeof(int));
You have a similar error in the line:
*matrix[i][j] = 5;
That should be:
(*matrix)[i][j] = 5;
You can avoid some of the confusion by using a temporary variable in the function.
void initialize(int*** matrix, int size) {
int** m = calloc(size, sizeof(int*));
for (int i = 0; i < size; ++i) {
m[i] = calloc(size, sizeof(int));
}
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
m[i][j] = 5; // some number, doesn't matter for now
}
}
*matrix = m;
}

Resources