Even though I looked through stackoverflow for this issue for a bit, I couldnt figure out how to solve my problem. I know this a very primitive problem and there are many solutions for similar problems but they didnt help to find a solution.
Its fairly simple:
I allocate a three-dimensional array dynamiclly and the I store the number 2 in each field.
But VS gives me an access-violation.
Here is my code:
#include <stdlib.h>
#include <stdio.h>
int main() {
int width = 512;
int height = 512;
int ***colors = (int ***)malloc(width * sizeof(int **));
for (int i = 0; i < height; ++i) {
colors[i] = (int **)malloc(height * sizeof(int *));
for (int j = 0; j < 3; ++j) {
colors[i][j] = (int *)malloc(3 * sizeof(int));
}
}
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++)
for (int z = 0; z < 3; z++)
colors[x][y][z] = 2; //Memory Access Violation happens here
free(colors);
return EXIT_SUCCESS;
}
You got confused with index range for for loops.
That is you are not properly allocating the memory thus you will end up accessing out of bound while using it.
for (int i = 0; i < height; ++i) {
colors[i] = (int **)malloc(height * sizeof(int *));
for (int j = 0; j < 3; ++j) {
colors[i][j] = (int *)malloc(3 * sizeof(int));
}
}
should be
for (int i = 0; i < width; ++i) {
colors[i] = (int **)malloc(height * sizeof(int *));
for (int j = 0; j < height; ++j) {
colors[i][j] = (int *)malloc(3 * sizeof(int));
}
}
Related
#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 *.
The code below works fine when M <= 4, but seems to bugger up if you put in a whole number that's any bigger (in this case, I actually need M to be 10). Does anyone know why this is happening and what can be done about it? Thanks.
/*
"Read all 100 numbers from the text file and store it in a 10x10 array."
*/
#include <stdio.h>
#include <stdlib.h>
FILE *fptr;
int n;
int M = 4; // Length and width of array
int main()
{
// Allocating memory
int **myArray = (int **)malloc(M * sizeof(int));
for (int j = 0; j < M; j++) {
myArray[j] = (int *)malloc(M * sizeof(int));
}
// Loading data into array
fptr = fopen("List of Numbers.txt","r");
for (int i = 0; i < M; i++) {
for (int j = 0; j < M; j++) {
fscanf(fptr,"%d",&n);
// printf("%d ",n);
myArray[i][j] = n;
}
}
fclose(fptr);
// Printing
for (int i = 0; i < M; i++) {
for(int j = 0; j < M; j++) {
printf("%d ",myArray[i][j]);
}
printf("\n");
}
return 0;
}
This line:
int **myArray = (int **)malloc(M * sizeof(int));
should be:
int **myArray = (int **)malloc(M * sizeof(int *));
^
You are allocating an array of pointers, not an array of ints.
I am declaring and printing a simple 2d array or matrix.
I am getting a segmentation fault that is being caused by the nested for loop that sets the values of the matrix.
int rows, columns;
rows = columns = 3;
int **matrix;
matrix = malloc(sizeof(int) * rows);
for (int i = 0; i < columns; i++) {
matrix[i] = malloc(sizeof(int) * columns);
}
This throws a seg fault
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
matrix[i][j] = 1;
}
}
If I set i = 1, there is no seg. fault.
for (int i = 1; i < rows; i++) {
for (int j = 0; j < columns; j++) {
matrix[i][j] = 1;
}
}
However, it does make the first 3 values printed random though.
-------
Entire Code
int main(int argc, char const *argv[]) {
int rows, columns;
rows = 3;
columns = 3;
int **matrix;
matrix = malloc(sizeof(int) * rows);
for (int i = 0; i < columns; i++) {
matrix[i] = malloc(sizeof(int) * columns);
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
matrix[i][j] = 1;
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
matrix[i][j] = 1;
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
printf("%d\n", matrix[i][j]);
}
}
for (int i = 0; i < rows; i++) {
free(matrix[i]);
}
free(matrix);
return 1;
}
Your problem is here:
int **matrix;
matrix = malloc(sizeof(int) * rows);
You want matrix to be an array of pointers to int but you use "sizeof int" instead of "sizeof int pointer". Try:
int **matrix;
matrix = malloc(sizeof(int*) * rows);
or better
int **matrix;
matrix = malloc(rows * sizeof *matrix);
As pointed out by #n.m in a comment, the following:
for (int i = 0; i < columns; i++) {
matrix[i] = malloc(sizeof(int) * columns);
}
is wrong. It shall be:
for (int i = 0; i < rows; i++) { // Notice this change
matrix[i] = malloc(sizeof(int) * columns);
}
I created and allocated a double pointer like this:
int **a;
a = (int**)malloc(10 * sizeof(int *));
for (int i = 0; i < 10; i++)
*(a+i) = (int *)malloc(10 * sizeof(int));
And then I initialized it for example like this:
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
**a = 1;
(*a)++;
}
a++;
}
My problem and question is how can I save the address'es of my double pointer?At this moment I lost them and can't use them anymore.
Don't use explicit pointer arithmetic and dereferencing when array subscripting will do:
int rows = 10, cols = 10
int **a;
// don't cast the return value of malloc
a = malloc(rows * sizeof(*a));
for (int i = 0; i < rows; i++)
a[i] = malloc(cols * sizeof(**a));
...
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
a[i][j] = 1;
}
}
Return a from a function, save it as a class member, it is just like any other variable. If you want to save the contents of the arrays then do that (but that is not what I could call saving a double pointer -- by the way the usual description is "pointer-to-pointer").
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;
}