stack overflow unless printf under recursion - c

This code below is the code for finding the determinant for 3x3 matrix (this code is intended for nxn matrix, but for the sample, I used 3x3), using recursive
The result is working fine, but I wonder what errors in this code make this must be the printf("\n") before calling the sub-function (itself) or else it will return the error 0xc0000fd (stack overflow).
#include "stdio.h"
#include "stdlib.h"
#include "conio.h"
#define size 3
void trimarray(int**rrayrc,int**rrayout, int dim,int cuti,int cutj)
{
int i, j;
int ti = 0,tj;
for(i = 0; i<dim; i++)
{
tj = 0;
for(j = 0; j< dim; j++)
{
if(!((i==cuti)||(j==cutj)))
{
rrayout[ti][tj] = rrayrc[i][j];
}
if(j!=cutj) {tj++;}
}
if(i!=cuti) {ti++;}
}
}
void initializearray(int** rray,int dim)
{
int i, j;
for(i = 0; i<dim; i++)
{
for(j = 0; j<dim; j++)
{
rray[i][j] = 0;
}
}
}
int det(int** rray, int dim)
{
int i,j;
int cut[dim-1][dim-1];
int* cutp[i];
int mul = 1,sum=0;
if(dim >1)
{
for(i = 0; i<dim-1; i++)
{
cutp[i] = cut[i];
}
initializearray(cutp,dim-1);
for(i = 0; i<dim; i++)
{
printf("\n",dim); //<< Without this the program won't work
trimarray(rray,cutp,dim,0,i);
sum+=det(cutp,dim-1)*mul*rray[0][i];
mul = 0-mul;
}
return sum;
}
else
{
return rray[0][0];
}
}
int main()
{
int test[size][size] = {2,-3,-2,-6,3,3,-2,-3,-2};
int* testpntr[size];
int i,deter;
for(i = 0; i<size; i++)
{
testpntr[i] = test[i];
}
deter = det(testpntr,size);
printf("[%d]",deter);
getch();
return 0;
}
The answers will be dearly appreciated.

int* cutp[i]; is undefined behavior since i is uninitialized at this stage. You have no idea what is the size of cutp array.

Related

Basic square function done with array & malloc

#include <stdio.h>
#include <stdlib.h>
int *squares(int max_val) {
int *result = malloc(max_val * sizeof(int));
int i;
for(i = 1; i <= max_val; i++) {
result[i-1] = i*i;
}
return(result);
}
int main() {
int *sq = squares(10);
int i;
for(i = 0; i < 10; i++) {
printf("%d\t", sq[i]);
}
printf("\n");
return(0);
}
Basically take's an integer and returns a integer array of its squares. (Above works)
How would I do this without malloc or pointers?
#include <stdio.h>
#include <stdlib.h>
int[] squares(int max_val) {
int result[max_val];
int i;
for(i = 1; i <= max_val; i++) {
result[i-1] = i*i;
}
return(result);
}
int main() {
int sq[] = squares(10);
int i;
for(i = 0; i < 10; i++) {
printf("%d\t", sq[i]);
}
printf("\n");
return(0);
}
Above errors because of the function call. Is this possible? Or do we have to do it with pointers?
How about something like this.. ?
#include <stdio.h>
#include <stdlib.h>
void squares(int values[], int max_val) {
for(int i = 1; i <= max_val; i++) {
values[i - 1] = i * i;
}
}
int main() {
int sq[10];
squares(sq, 10);
int i;
for(i = 0; i < 10; i++) {
printf("%d\t", sq[i]);
}
printf("\n");
return(0);
}
This
int[] squares(int max_val) {
int result[max_val];
int i;
for(i = 1; i <= max_val; i++) {
result[i-1] = i*i;
}
return(result);
}
is not valid C:
a.c:3:4: error: expected identifier or ‘(’ before ‘[’ token
int[] squares(int max_val)
It should be
int *squares(int max_val) {
...
}
Putting that aside, your second squares returns a pointer to a local array.
This array ceases to exist once squares ends it's execution, so you are
returning an pointer that become invalid the moment it returns.
Also
int sq[] = squares(10);
is invalid C as well
a.c:9:13: error: invalid initializer
int sq[] = squares(10);
^~~~~~~
you cannot assign a function value to an array. The correct
version is
int *sq = squares(10);
So, if you don't want squares to allocate memory with malloc for the result, then you can
either allocate the memory in main or create an array in main and pass it to
squares:
int squares(int *result, int max_val) {
if(result == NULL || max_val <= 0)
return 0;
int i;
for(i = 1; i <= max_val; i++) {
result[i-1] = i*i;
}
return 1;
}
// version 1
int main() {
int *sq = malloc(10 * sizeof *sq);
// NEVER forget to check the return value of malloc
if(sq == NULL)
{
fprintf(stderr, "not enough memory\n");
return 1;
}
squares(sq, 10);
int i;
for(i = 0; i < 10; i++) {
printf("%d\t", sq[i]);
}
printf("\n");
free(sq); // NEVER forget to free
return 0;
}
// version 2
int main() {
int sq[10];
squares(sq, sizeof sq / sizeof sq[0]);
int i;
for(i = 0; i < 10; i++) {
printf("%d\t", sq[i]);
}
printf("\n");
return 0;
}

C pointer dereference error

Suppose I have the following code (example):
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
void Ex(void *ret, int ret_len, int choice){
if(choice==1){
int *ret = ret;
for(int i=0; i<ret_len; i++){
*(ret+i) = i;
}
} else {
int **ret = ret;
for(int i=0; i<ret_len; i++){
for(int j=0; j<ret_len; j++){
*(*(ret+i)+j) = i*j;
}
}
}
}
int main()
{
int m[10];
Ex(m,10,1);
printf("%i",m[3]);
return 0;
}
The goal of the function is to take a pointer to preallocated memory of a one or two dimensional array.
When compiled and executed the code works when choice==1, otherwise, if I run
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
void Ex(void *ret, int ret_len, int choice){
if(choice==1){
int *ret = ret;
for(int i=0; i<ret_len; i++){
*(ret+i) = i;
}
} else {
int **ret = ret;
for(int i=0; i<ret_len; i++){
for(int j=0; j<ret_len; j++){
*(*(ret+i)+j) = i*j;
}
}
}
}
int main()
{
int m[100];
Ex(m,10,2);
printf("%i", m[3][5]);
return 0;
}
no output is ever produced and the code seems to run forever (note choice is now not 1 and m is the proper size)
I am under the impression that the casting in Ex should change the shape of the array and allow main to index into it as if it were 2d but neither indexing into it as if it were 1d nor 2d work.
Having looked at the comments on the question and some answers this is what I have gathered for your requirements.
You need a generic function that takes an array of choice dimensions and initializes it with some values.
This can suit your purpose.
void Ex(void *_ret, int ret_len, int choice){
if(choice==1){
int *ret = _ret;
int i;
for(i=0; i<ret_len; i++){
ret[i] = i;
}
} else {
int (*ret)[ret_len] = _ret;
int i, j;
for(i=0; i<ret_len; i++){
for(j=0; j<ret_len; j++){
ret[i][j] = i*j;
}
}
}
}
Now this is how you can call it
int main(void) {
int first[10];
Ex(first, 10, 1);
int second[20][20];
Ex(second, 20, 2);
printf("first[4] = %d\n", first[4]);
printf("second[3][4] = %d\n", second[3][4]);
}
You can see the Demo here
because ret if of type int *, but the else part is:
for(int i=0; i<ret_len; i++){
for(int j=0; j<ret_len; j++){
*(*(ret+i)+j)=i*j;
}
}
which needs ret of type int **. and this causes the error.
The way to resolve the problem is to use void *. that is one way to use generics in C.
Take a look at qsort in C.
void Ex(void *_ret, int ret_len, int choice){
if(choice==1){
int *ret = (int *)_ret; // <---- type conversion
for(int i=0; i<ret_len; i++){
*(ret+i) = i;
}
} else {
int **ret = (int **)_ret; // <---- type conversion
for(int i=0; i<ret_len; i++){
for(int j=0; j<ret_len; j++){
*(*(ret+i)+j) = i*j;
}
}
}
}
The closest thing with least change of your existing code I came up with is:
#include <stdio.h>
void Ex(int * ret, int ret_len, int choice){
if(choice==1){
for(int i=0; i<ret_len; i++){
*(ret+i)=i;
}
}else{
for(int i=0; i<ret_len; i++){
for(int j=0; j<ret_len; j++){
*(ret + i*ret_len + j) = i*j;
}
}
}
}
int main() {
int anint[10][10];
Ex(anint, 10, 2);
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {printf("%d\n", anint[i][j]);}
}
}
Basically, I mannually manipulate the offset within the Ex function. and I take the assumption that the length of the two dimensions are always the same.
A more "conventioal way" would be a true 2-dimentional array in which each element is malloc independently. Thus, the element of the first level array can be casted by force to a pointer and be dereferenced in the second level. But I am reluctant to call malloc too many times.
I hope this can solved your problem.

multithreading - occasional segfault in matrix multiplication in c

I'm working on a small program of multithreaded matrix multiplication. My first job is to fill the entry of matrices with a random integer. I met some segment faults after I tried to pass a function pointer to pthread_create. And I think the problem is in function pthread_join.
But there are two issues in general.
The first one is the segment fault does not happen every time. Sometimes the code works, but most of the times it doesn't. So it really confuses me.
The other one is when the code is working, there are always several entries still not initialized, especially for matrix[0][0], it is never initialized. And I don't quite know where to debug that one.
Here's my code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <pthread.h>
#define N 5
#define MAX 10
int A[N][N];
int B[N][N];
int C[N][N];
pthread_t pid[N][N];
typedef struct {
int row, col;
} Pos;
typedef void* (*thread_func)(void*);
void print_matrix(int M[][N]) {
int i, j;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
printf("%3d", M[i][j]);
if (j < N - 1) {
printf(", ");
}
}
printf("\n");
}
}
void join_threads(void) {
int i, j;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
pthread_join(pid[i][j], NULL);
}
}
}
void* fill_entry(void* arg) {
Pos* pos = (Pos*)arg;
A[pos->row][pos->col] = rand() % MAX;
B[pos->row][pos->col] = rand() % MAX;
return NULL;
}
void dispatch_jobs(thread_func job_func) {
int i, j;
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
Pos pos;
pos.row = i;
pos.col = j;
if (pthread_create(&pid[i][j], NULL, job_func, (void*)&pos)) {
perror("pthread_create");
exit(-1);
}
}
}
}
int main(void) {
srand(time(NULL));
dispatch_jobs(&fill_entry);
join_threads();
printf("Matrix A:\n");
print_matrix(A);
printf("Matrix B:\n");
print_matrix(B);
return 0;
}
Pos pos;
pos.row = i;
pos.col = j;
if (pthread_create(&pid[i][j], NULL, job_func, (void*)&pos)) {
perror("pthread_create");
exit(-1);
}
You are passing a pointer to a local variable to the threads. Once the thread tries to access the data, i.e. dereferences the pointer, the variable is long gone, reused, and contains garbage data.

Create an array of random numbers and return the array in C

I want to create a function called get_lotto_draw that will create an array of 6 random numbers and return them to main.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
get_lotto_draw()
{
int lottery[50];
int u,i,j,temp;
int lotto[6];
srand(time(NULL));
for (i =0; i<49; i++)
lottery[i] = i+1;
for (i =0; i<49; i++)
{
j = (rand()%49)+1;
temp = lottery[i];
lottery[i] = lottery[j];
lottery[j] = temp;
}
for (i =0; i<6; i++)
{
lotto[i] = lottery[i];
}
return lotto;
}
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void get_lotto_draw(int lotto[])
{
int lottery[50];
int u,i,j,temp;
for (i =0; i<49; i++)
lottery[i] = i+1;
for (i =0; i<49; i++)
{
j = (rand()%49)+1;
temp = lottery[i];
lottery[i] = lottery[j];
lottery[j] = temp;
}
for (i =0; i<6; i++)
{
lotto[i] = lottery[i];
}
return ;
}
int _tmain(int argc, _TCHAR* argv[])
{
int lotto[6];
srand(time(NULL));
get_lotto_draw(lotto);
for (int i = 0; i < 6; i ++)
printf ("%d ", lotto[i]);
printf ("\n");
return 0;
}
The function get_lotto_draw will take the array 'lotto' as an argument - unsized array.
If you declare lotto as auto variable inside the function then when the function ends - the auto variable lotto would have been removed - check this link for details.
So you declare lotto in main and pass it to the function.
Other options include
use malloc (to allocate memory for 'lotto') inside the get_lotto_draw and free in main (unless you are very careful- this will lead to memory leaks - I do not recommend this)
use malloc in main and pass the allocated memory to the function and free it in main later
create a static and use it.
My recommendation is in such situations to use the stack (auto variable as used above) otherwise use malloc/free.
I fixed some of the bugs others pointed out
Thank you very much for everyone who helped. I spent a few hours reading and learning about the things you mentioned. I have now managed to create a piece of code that will do what I wanted.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int *get_lotto_draw(int n)
{
int i;
int *lotto;
lotto = malloc(n * sizeof(*lotto));
if (lotto == NULL)
return NULL;
int lottery[50];
int u,j,temp;
for (i =0; i<49; i++)
lottery[i] = i+1;
for (i =0; i<49; i++)
{
j = (rand()%49)+1;
temp = lottery[i];
lottery[i] = lottery[j];
lottery[j] = temp;
}
for (i =0; i<6; i++)
{
lotto[i] = lottery[i];
}
return lotto;
}
int main(int argc, char *argv[])
{
int i, n = 6;
int *lotto;
srand(time(NULL));
lotto = get_lotto_draw(n);
if (lotto == NULL)
return -1;
printf("Here is the array: ");
for(i = 0 ; i < n ; i++) {
printf("%d ", lotto[i]);
}
free(lotto);
printf("\n\n");
return 0;
}
I also found that I can do this by using static int as I have an array of set length.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int *get_lotto_draw(int n)
{
int i;
static int lotto[6];
int lottery[50];
int u,j,temp;
for (i =0; i<49; i++)
lottery[i] = i+1;
for (i =0; i<49; i++)
{
j = (rand()%49)+1;
temp = lottery[i];
lottery[i] = lottery[j];
lottery[j] = temp;
}
for (i =0; i<6; i++)
{
lotto[i] = lottery[i];
}
return lotto;
}
int main(int argc, char *argv[])
{
int i, n = 6;
int *lotto;
srand(time(NULL));
lotto = get_lotto_draw(n);
printf("Here is the array: ");
for(i = 0 ; i < n ; i++) {
printf("%d ", lotto[i]);
}
printf("\n\n");
return 0;
}

4-dimensional array allocation: access violation

I wrote this simple piece of code to dynamically allocate a 4-dimensional array:
#include <stdlib.h>
#include <stdio.h>
int**** alloc() {
int i,j,k;
int ****matrix;
int x,y,z,n_pairs;
x= 62;
y= 45;
z= 28;
n_pairs = 4;
matrix = (int ****) malloc(x*sizeof(int***));
for (i=0; i<x; i++) {
matrix[i] = (int ***) malloc(y*sizeof(int**));
if(matrix[i]==NULL)
return NULL;
for (j=0; j<y; j++) {
matrix[i][j] = (int **) malloc(z*sizeof(int*));
if (matrix[i][j] == NULL)
return NULL;
for (k=0; k<n_pairs; k++) {
matrix[i][j][k] = (int *)calloc(n_pairs,sizeof(int));
if (matrix[i][j][k] == NULL)
return NULL;
}
}
}
return matrix;
}
void freeMatrix(int ****m) {
int i,j,k;
int x,y,z;
x= 62;
y= 45;
z= 28;
for(i=0; i<x; i++) {
for(j=0; j<y; j++) {
for(k=0; k<z; k++)
free(m[i][j][k]);
free(m[i][j]);
}
free(m[i]);
}
free(m);
}
int main() {
int i,j,k,h;
int ****m = NULL;
m = alloc();
for(i=0;i<62;i++)
for(j=0;j<45;j++)
for(k=0;k<28;k++)
for(h=0;h<4;h++)
printf("%d\t",m[i][j][k][h]);
system("pause");
return 0;
}
the problem is that I this code results in an Access Violation when I try to execute it.
Isn't it the correct way to allocate/free a multidimensional array? If yes, then what is the problem?
One problem is here:
for (k=0; k<n_pairs; k++) { //<----- This should read `k<z'
matrix[i][j][k] = (int *)calloc(n_pairs,sizeof(int));
You probably meant to loop to z, not to n_pairs.
if u make a matrix the correct way is to allocate all the memory u need, then dived it up.
for 2.dim
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int i;
int ** matrix;
int rows = 10;
int colums =10;
matrix = (int**)malloc(rows*sizeof(int*));
matrix[0] = (int*)malloc(rows*colums*sizeof(int));
for(i =1; i<rows;i++)
matrix[i] = &matrix[i-1][colums];
free(matrix[0]);
free(matrix);
return 0;
}
this is for having a continues memory area for the matrix. this can be faster

Resources