free() causes SEG fault C - c

int ** ARR;
int LENGTH = 1;
int DEPTH = 1;
void loadt(int ** terr)
{
terr = (int**)malloc(sizeof(int*) * (LENGTH + 1));
int i, j;
for(i = 1; i <= LENGTH; i++)
terr[i] = (int*)malloc(sizeof(int) * (DEPTH + 1));
for(i = 1; i <= LENGTH; i++)
for(j = 1; j <= DEPTH; j++)
scanf("%d", &terr[i][j]);
}
void freet(int ** terr)
{
int i;
for(i = 1; i <= LENGTH; i++){
free(terr[i]);
}
free(terr);
}
int main(int argc, char* argv[])
{
loadt(ARR);
freet(ARR);
return 0;
}
Hello. I probably miss sth really basic here but after I run the program it crashes."Segmentation fault (core dumped)" Why?

Because in c arguments to functions are always passed by value, so your unecessarily global variable is not getting reassigned inside the function since you are passing it as a parameter and hence a copy of it is made, which is the one that is actually reassigned.
So you are calling free() on an uninitialized poitner.
Try like this
int **loadt(int LENGTH, int DEPTH)
{
int **terr;
int i, j;
terr = malloc(sizeof(int*) * (LENGTH + 1));
if (terr == NULL)
return NULL;
for (i = 1; i <= LENGTH; i++) {
terr[i] = malloc(sizeof(int) * (DEPTH + 1));
if (terr[i] == NULL) {
for (j = i ; j >= 0 ; --j) {
free(terr[j]);
}
free(terr);
return NULL;
}
}
for (i = 1; i <= LENGTH; i++) {
for (j = 1; j <= DEPTH; j++) {
scanf("%d", &terr[i][j]);
}
}
return terr;
}
void freet(int **terr)
{
int i;
if (terr == NULL)
return; // `free()' also accepts NULL pointers
for (i = 1; i <= LENGTH; i++) {
free(terr[i]);
}
free(terr);
}
int main(int argc, char* argv[])
{
int **ARR;
int LENGTH = 1;
int DEPTH = 1;
ARR = loadt(LENGTH, DEPTH);
freet(ARR);
return 0;
}
Another problem, is that you start your loops at i = 1, which is fine because you are allocating enough space, but it's not the way you should do it. Instead for (i = 0 ; i < LENGTH ; ++i) would be how a c programmer would do it. Note that you are wasting the first element of your array.

Related

The pointer variables overflows when they store integers larger than 1024 and some adresses seem to be locked.in C

How do I get to write to 2D pointers where I have pnumber[2%4][2%4] and how can I get pnumber with more than 3 ciphers to be displayed?
I'm making a program to write pascals triangle in C.
When the pointer pnumbers[i][j] have both i and j = 2 mod 4, except for when i and j = 2, then my program won't write to the address and give the error message:
pascals triangle: malloc.c:2406: sysmalloc: Assertion '{old_top == initial_top (av) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.
Aborted.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int factorial(int p) {
if (p>=1) {
return p*factorial(p-1);
}
else {
return 1;
}
}
int NchooseM(int n, int m) {
return factorial(n)/(factorial(n-m)*factorial(m));
}
int main() {
int n =7;
int x = n-2;
int i, j, k;
/*
printf("How many rows of Pascals triangle do you want to write?\n");
scanf("%d", &n);
*/
int **pnumbers;
pnumbers = (int **) malloc(n *sizeof(int *));
/* Allocate memory for storing the individual elements in a row */
for (i = 0; i < n; i++) {
pnumbers[i] = (int *) malloc(i * sizeof(int));
}
pnumbers[0][1] = 1;
/* Calculating the value of pnumbers[k][l] */
for (i = 0; i < n; i++) {
for (j = 0; j <= i; j++) {
pnumbers[i][j] = NchooseM(i,j);
}
/*
if (!(i % 4 == 2 && i != 2))
for (j = 0; j <= i; j++) {
pnumbers[i][j] = NchooseM(i,j);
} else if (i > 2) {
for (j = 0; j <= i-1; j++) {
pnumbers[i][j] = NchooseM(i,j);
}
}
*/
}
/* Writing out the triangle */
for (i = 0; i < n; i++) {
for (k = 0; k <= x; k++){
printf(" ");
}
for (j = 0; j <= i; j++) {
printf("%d ", pnumbers[i][j]);
}
x = x-1;
printf("\n");
}
for (i = 0; i < n; i++) {
free(pnumbers[i]);
}
free(pnumbers);
return 0;
}
When I avoid writing to these addresses and just print them out I get some seemingly random integer at these memory addresses.
Also when avoid these addresses and just print out so many rows that I get some spots with a higher integer with more than 3 siphers, it seems to overflow - and I don't see the logic behind it.
The result of running the second code
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int factorial(int p) {
if (p>=1) {
return p*factorial(p-1);
}
else {
return 1;
}
}
int NchooseM(int n, int m) {
return factorial(n)/(factorial(n-m)*factorial(m));
}
int main() {
int n =20;
int x = n-2;
int i, j, k;
/*
printf("How many rows of Pascals triangle do you want to write?\n");
scanf("%d", &n);
*/
int **pnumbers;
pnumbers = (int **) malloc(n *sizeof(int *));
/* Allocate memory for storing the individual elements in a row */
for (i = 0; i < n; i++) {
pnumbers[i] = (int *) malloc(i * sizeof(int));
}
pnumbers[0][1] = 1;
/* Calculating the value of pnumbers[k][l] */
for (i = 0; i < n; i++) {
/*
for (j = 0; j <= i; j++) {
pnumbers[i][j] = NchooseM(i,j);
}
*/
if (!(i % 4 == 2 && i != 2))
for (j = 0; j <= i; j++) {
pnumbers[i][j] = NchooseM(i,j);
} else if (i > 2) {
for (j = 0; j <= i-1; j++) {
pnumbers[i][j] = NchooseM(i,j);
}
}
}
/* Writing out the triangle */
for (i = 0; i < n; i++) {
for (k = 0; k <= x; k++){
printf(" ");
}
for (j = 0; j <= i; j++) {
printf("%d ", pnumbers[i][j]);
}
x = x-1;
printf("\n");
}
for (i = 0; i < n; i++) {
free(pnumbers[i]);
}
free(pnumbers);
return 0;
}
But row number 13 is still quite messed up.
Code is experiencing int overflow and thus undefined behavior (UB).
With 32-bit int and int factorial(int p), p > 12 oveflows the int range.
Code could use a wider integer type (long long works up to p==20), but improvements can be made at NchooseM() to avoid overflow for higher values.
Something like the below. Works up to int n = 30;
int NchooseM(int n, int m) {
// return factorial(n)/(factorial(n-m)*factorial(m));
int nm = 1;
int den = 1;
for (int i = m+1; i <= n; i++) {
assert(INT_MAX/i >= nm);
nm *= i;
assert(nm % den == 0);
nm /= den++;
}
return nm;
}
Tried unsigned long long and works up to int n = 62;
Edit: Another bug:
I "fixed" by initializing all to 1, yet I suspect something remains amiss in /* Calculating the value of pnumbers[k][l] */ for (i = 0; i < n; i++) { code.
pnumbers[i] = malloc((i + 1) * sizeof pnumbers[i][0]);
for (int j = 0; j < i + 1; j++) {
pnumbers[i][j] = 1;
}
Aside: rather than pnumbers[i] = (int *) malloc((i+1) * sizeof(int));, consider below with no unneeded cast nor trying to match the right type.
pnumbers[i] = malloc(sizeof pnumbers[i][0] * (i+1));

Counting Sort displays a weird behavior

I have implemented a Counting Sort in an assignment given to us by a teacher but sometimes it doesn't work for large arrays.
Here is the code:
void countingSort(int *t, int n) {
int min = findMin(t, n);
int max = findMax(t, n);
int range = max - min + 1;
int *count, *output;
int i;
count = (int *)malloc(range * sizeof(int));
output = (int *)malloc(n * sizeof(int));
for (i = 0; i < range; i++) {
count[i] = 0;
}
for (i = 0; i < n; i++) {
count[t[i] - min]++;
}
for (i = 1; i < range; i++) {
count[i] += count[i - 1];
}
for (i = n - 1; i >= 0; i--) {
output[count[t[i] - min] - 1] = t[i];
count[t[i] - min]--;
}
for (i = 0; i < n; i++) {
t[i] = output[i];
}
}
What's wrong with my code?
Your code seems to work for small values of range, but might fail if min and max are too far apart, causing the computation of range to overflow the range of int and malloc() to fail.
You should check for overflow in range and check memory allocation success. Note too that calloc() is more appropriate than malloc() for the count array. Finally, you must free the allocated arrays.
Here is a modified version:
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
int findMax(const int *t, int n) {
int max = INT_MIN;
while (n-- > 0) {
if (max < *t) max = *t;
t++;
}
return max;
}
int findMin(const int *t, int n) {
int min = INT_MAX;
while (n-- > 0) {
if (min > *t) min = *t;
t++;
}
return min;
}
int countingSort(int *t, int n) {
int min, max, range, i;
int *count, *output;
if (n <= 0)
return 0;
min = findMin(t, n);
max = findMax(t, n);
if (min < 0 && max >= 0 && (unsigned)max + (unsigned)(-min) >= INT_MAX) {
fprintf(stderr, "countingSort: value range too large: %d..%d\n", min, max);
return -1;
}
range = max - min + 1;
if ((count = (int *)calloc(range, sizeof(int))) == NULL) {
fprintf(stderr, "countingSort: cannot allocate %d element count array\n", range);
return -1;
}
if ((output = (int *)malloc(n * sizeof(int))) == NULL) {
fprintf(stderr, "countingSort: cannot allocate %d element output array\n", n);
free(count);
return -1;
}
for (i = 0; i < n; i++) {
count[t[i] - min]++;
}
for (i = 1; i < range; i++) {
count[i] += count[i - 1];
}
for (i = n; i-- > 0;) {
output[count[t[i] - min] - 1] = t[i];
count[t[i] - min]--;
}
for (i = 0; i < n; i++) {
t[i] = output[i];
}
free(count);
free(output);
return 0;
}
You can avoid the cumbersome and potentially inefficient downward loop by replacing the second and third for loops with this:
/* compute the first index for each value */
int index = 0;
for (i = 0; i < range; i++) {
incr = count[i];
count[i] = index;
index += incr;
}
/* copy each value at the corresponding index and update it */
for (i = 0; i < n; i++) {
output[count[t[i] - min]++] = t[i];
}

Creating a new array without duplicate integers from an array in C

I was asked to create a new array without duplicate integars from an array.
I think I made a mistake in my code but can't notice anything wrong.
The output for "1, 2, 1, 2, 1, 4" is "1, 2, 2, 4".
It's supposed to be "1, 2, 4"
Would like to learn about my mistake.
// Exercise 2 -
void Ex2() {
int i, counter = 1, size = -1;
int* array = input_dynamic_array(&size);
int* newArr = (int*)malloc((size)* sizeof(int));
newArr[0] = array[0];
assert(array);
for (i = 1; i < size; i++) {
if (!find_num_in_newArr(newArr, size, array[i])) {
newArr[counter++] = array[i];
}
}
newArr = (int*)realloc(newArr, (counter)*sizeof(int));
printArray(newArr, counter);
free(array);
free(newArr);
}
bool find_num_in_newArr(int *newArr, int size, int num) {
int i;
for (i = 0; i < size; i++) {
if (newArr[i] == num) {
return true;
}
return false;
}
}
int* input_dynamic_array(int *size)
{
int *array;
int ii;
printf("Enter array size: ");
scanf("%d", size);
array = (int*)malloc((*size) * sizeof(int));
assert(array);
printf("Enter %d integer numbers: ", *size);
for (ii = 0; ii < *size; ii++)
scanf("%d", array + ii);
return array;
}
I see a problem here:
for (i = 0; i < size-1; i++)
{
if (newArr[i] == num)
{
return true;
}
return false;
}
This will never have another iteration. It will either return true or false from the first iteration. Thats not what you are planning for when you used a loop.
Based on your design, you might want to move return false; outside the loop.
Another advise, Don't cast the return value of malloc, its pointless.
Also,
int* newArr = (int*)malloc((size)* sizeof(int));
This needs to be followed by a check. You need to check if malloc returned NULL. If yes, then memory is not allocated and anything with modification based on newArr would be horrible.
A relative cleaner way of doing it will include using a function that looks like below:
int RemoveDuplicates(int* Arr, int length)
{
int i = 0, j = 0;
int LengthChanged = 0;
for (i = 1; i < length; i++)
{
for(j = 0; j < LengthChanged ; j++)
{
if(Arr[i] == Arr[j])
break;
}
// Copy as is if there is not duplicate element in the array
if (j == LengthChanged )
Arr[LengthChanged++] = Arr[i];
}
return LengthChanged;
}
There are 2 problems in your code:
find_num_in_newArr exits the loop with a return false; at the first iteration. move the return statement outside the loop body.
Ex2 check the whole array for duplicates instead of just the portion already copied: find_num_in_newArr(newArr, size, array[i]) should be find_num_in_newArr(newArr, i, array[i]). As posted, this code has undefined behavior.
Here is a corrected version with a few extra assertions:
// Exercise 2 -
bool find_num_in_newArr(const int *newArr, int size, int num) {
int i;
for (i = 0; i < size; i++) {
if (newArr[i] == num) {
return true;
}
}
return false;
}
int *input_dynamic_array(int *size) {
int *array;
int ii;
printf("Enter array size: ");
assert(scanf("%d", size) == 1);
assert(*size > 0);
array = (int*)malloc((*size) * sizeof(int));
assert(array != NULL);
printf("Enter %d integer numbers: ", *size);
for (ii = 0; ii < *size; ii++) {
assert(scanf("%d", array + ii) == 1);
}
return array;
}
void Ex2(void) {
int i, counter, size;
int *array = input_dynamic_array(&size);
int *newArr = (int*)malloc(size * sizeof(int));
assert(newArr != NULL);
newArr[0] = array[0];
counter = 1;
for (i = 1; i < size; i++) {
if (!find_num_in_newArr(newArr, i, array[i])) {
newArr[counter++] = array[i];
}
}
newArr = (int*)realloc(newArr, counter * sizeof(int));
assert(newArr != NULL);
printArray(newArr, counter);
free(array);
free(newArr);
}
You over complicate it a bit.
Use the correct types for the indexes (size_t)
int CheckValue(int *arr, int value, size_t size)
{
while (size--)
{
if (arr[size - 1] == value) return -1;
}
return 0;
}
int main()
{
int arr[] = {1,4,5,8,3,2,1,-1,9,-1,-1,6,8,1,5,4,2,3};
int newarr[sizeof(arr) / sizeof(arr[0])];
size_t size = 0;
for (size_t index = 0; index < sizeof(arr) / sizeof(arr[0]); index++)
{
if (!CheckValue(newarr, arr[index], size))
{
newarr[size++] = arr[index];
}
}
return 0;
}

dynamically memory allocation of a matrix in an array of structure

I would like to make an array of structure with dynamic allocation of memory for the structure and the two matrix
typedef struct {
int **cont;
double **doubmat;
} libra;
int main(int argc, char *argv[]) {
libra *arra = calloc(Nfr,sizeof(libra));
for (i = 0; i < Nfr; i++) {
arra[i].doubmat = calloc(Nmol, sizeof (libra));
}
for (i = 0; i < Nfr; i++) {
for (j = 0; j < Nmol; j++) arra[i].doubmat[j] = calloc(Nmol, sizeof (libra));
}
for (i = 0; i < Nfr; i++) {
arra[i].cont = calloc(Nmol, sizeof (libra));
}
for (i = 0; i < Nfr; i++) {
for (j = 0; j < Nmol; j++) arra[i].cont[j] = calloc(Nmol, sizeof (libra));
}
}
but i have some trouble with memory, the numbers during the calculation depends on the number of structure in the array. I think that I'm making some mistake with the allocation.
Anyone have some tips ?
Thanks in advance for your help.
You have specified incorrect sizeof(type) to allocate memory for matrices.
You need to do something like that:
typedef struct {
int **cont;
double **doubmat;
} libra;
int main(int argc, char *argv[]) {
libra *arra = calloc(Nframe,sizeof(libra));
for (i = 0; i < Nfr; i++) {
arra[i].doubmat = calloc(Nmol, sizeof(*arra[i].doubmat));
for (j = 0; j < Nmol; j++)
arra[i].doubmat[j] = calloc(Nmol, sizeof(**arra[i].doubmat));
}
for (i = 0; i < Nfr; i++) {
arra[i].cont = calloc(Nmol, sizeof(*arra[i].cont));
for (j = 0; j < Nmol; j++)
arra[i].cont[j] = calloc(Nmol, sizeof(**arra[i].cont));
}
}
Supposing that NFrame is declared, I am going to show the allocation for an array of consisting of Nframe structs, each of which contains a NrowsxNcols doubmat:
typedef struct {
int **cont;
double **doubmat;
} libra;
int main(int argc, char *argv[]) {
int i, j, Nrows = ..., Ncols = ...;
libra *arra = calloc(Nframe, sizeof(libra));
for (i = 0; i < Nframe; i++) {
arra[i].doubmat = calloc(Nrows, sizeof(double*));
if (arra[i].doubmat == NULL)
return;
for (j = 0; j < Nmol; j++){
arra[i].doubmat[j] = calloc(Ncols, sizeof(double));
if (arra[i].doubmat[j] == NULL)
return;
}
}
}

free() crashes the code

#include <stdio.h>
#include <stdlib.h>
void printingArr(int** arr, int rows);
void sortingEachOneOfThem(int** pArr, int rows);
void sortingTheWholeArray(int** pArr, int rows);
void bubbleSort(int* arr);
void freeArray(int **a, int m);
int main(void)
{
int** pArr = 0;
int numOfRows = 0;
int sizes = 0;
printf("Enter number of rows: ");
scanf("%d", &numOfRows);
pArr = (int**) malloc(sizeof(int*) * numOfRows);
if (pArr == NULL)
{
printf("Unsuccessful malloc!\n");
return 1;
}
for (int i = 0; i < numOfRows; i++)
{
printf("Enter array length for row %d: ",i);
scanf("%d", &sizes);
pArr[i] = (int*) malloc(sizeof(int) * sizes + 1);
if (pArr[i] == NULL)
{
printf("Unsuccessful malloc!\n");
return 1;
}
pArr[i][0] = sizes;
for (int k = 1; k < sizes + 1; k++)
{
printf("Enter value for array: ");
scanf("%d", &pArr[i][k]);
}
}
printingArr(pArr, numOfRows);
sortingEachOneOfThem(pArr, numOfRows);
printingArr(pArr, numOfRows);
sortingTheWholeArray(pArr, numOfRows);
printingArr(pArr, numOfRows);
for (int i = 0; i < numOfRows; i++)
{
if (pArr[i] != NULL)
{
free(*(pArr + i));
}
}
//free(pArr);
system("pause");
return 0;
}
/*
this amazing, wonderfull piece of program prints the array given
input: int** arr, int rows
output: none
*/
void printingArr(int** arr, int rows)
{
int i = 0;
int k = 0;
for (i = 0; i < rows; i++)
{
for (k = 0; k <= arr[i][0]; k++)
{
printf("%d ", arr[i][k]);
}
printf("\n");
}
printf("\n");
}
/*
This beautiful function sorts the whole array, but its length of rows like a pyramid
input: int** arr, int rows
output: none
*/
void sortingTheWholeArray(int** pArr, int rows)
{
int* temp = 0;
int i = 0, k = 0;
for (i = 0; i < rows - 1; i++)
{
for (k = 0; k < rows - 1; k++)
{
if (pArr[k][0] > pArr[k + 1][0])
{
temp = pArr[k];
pArr[k] = pArr[k + 1];
pArr[k + 1] = temp;
}
}
}
}
/*
This little small function sorts every row of the array of arrays given to it
input: int** arr, int rows
output: none
*/
void sortingEachOneOfThem(int** pArr, int rows)
{
int i = 0;
for (i = 0; i < rows; i++)
{
bubbleSort(pArr[i]);
}
}
/*
This little piece of a code is a bubble sort, sorts the array given to it :)
input: int* arr, int rows
output: none
*/
void bubbleSort(int* arr)
{
int i = 1, k = 0;
for (i = 1; i < arr[0] - 1; i++)
{
for (k = 1; k <= arr[0] - i; k++)
{
if (arr[k] > arr[k + 1])
{
arr[k] += arr[k + 1];
arr[k + 1] = arr[k] - arr[k + 1];
arr[k] -= arr[k + 1];
}
}
}
}
the free at the end crashes my code, showing this error:
https://i.stack.imgur.com/nqxBG.png
the same usage of the function free() on another code worked good, but not here. I have tried running through it in step by step mode, it crashes at the first free. Dr. memory shows this: https://i.stack.imgur.com/rSZJr.png
another link: https:// i.stack. imgur.com/ZX2Ne.png (paste it without the spaces in the middle, Can't post more than 2 links)
what can I do?
This:
pArr[i] = (int*) malloc(sizeof(int) * sizes + 1);
under-allocates. Adding a single byte to the size of an array of int makes little sense. You probably meant:
pArr[i] = malloc((sizes + 1) * sizeof *pArri[i]);
Don't cast the return value of malloc(), and use sizeof on the left-hand side.

Resources